2015-10-04 18:56:26 +02:00
|
|
|
#!/usr/bin/env python2.7
|
2013-08-10 03:29:41 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright © 2013 Zulip, Inc.
|
|
|
|
|
2013-08-13 16:33:14 +02:00
|
|
|
### NOTE: You must copy the emoji along with this script if you want
|
|
|
|
### them to work properly
|
|
|
|
### (to static/third/gemoji/images/)
|
|
|
|
|
|
|
|
|
2016-03-10 17:15:34 +01:00
|
|
|
from __future__ import print_function
|
2013-08-10 03:29:41 +02:00
|
|
|
import sys
|
|
|
|
sys.path.append('zulip')
|
2013-10-28 15:54:32 +01:00
|
|
|
import os
|
2013-08-10 03:29:41 +02:00
|
|
|
import optparse
|
|
|
|
import codecs
|
|
|
|
|
|
|
|
usage = """show-last-messages --user=<bot's email address> --api-key=<bot's api key> --count=<count>
|
|
|
|
|
|
|
|
Shows the last <count> messages on all public streams.
|
|
|
|
|
2015-09-20 03:12:02 +02:00
|
|
|
Examples: subscribe --user=username@example.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --count=5 --streams=social
|
|
|
|
subscribe --user=username@example.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --streams='foo bar'
|
2013-08-10 03:29:41 +02:00
|
|
|
|
|
|
|
You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc
|
|
|
|
"""
|
2013-10-28 15:54:32 +01:00
|
|
|
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
|
2013-08-10 03:29:41 +02:00
|
|
|
import zulip
|
|
|
|
|
|
|
|
parser = optparse.OptionParser(usage=usage)
|
|
|
|
parser.add_option_group(zulip.generate_option_group(parser))
|
|
|
|
parser.add_option('--streams', default='')
|
|
|
|
parser.add_option('--count', default=5)
|
|
|
|
(options, args) = parser.parse_args()
|
|
|
|
|
|
|
|
client = zulip.init_from_options(options)
|
|
|
|
|
|
|
|
if options.streams == "":
|
2016-03-10 17:15:34 +01:00
|
|
|
print("Usage:", parser.usage, file=sys.stderr)
|
2013-08-10 03:29:41 +02:00
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
client.add_subscriptions([{"name": stream_name} for stream_name in
|
|
|
|
options.streams.split()])
|
|
|
|
queue = client.register(event_types=['message'])
|
|
|
|
client._register('get_old_messages', method='GET', url='messages')
|
|
|
|
|
|
|
|
result = client.get_old_messages({'anchor': queue['max_message_id'],
|
|
|
|
'num_before': int(options.count),
|
|
|
|
'num_after': 0,
|
|
|
|
'apply_markdown': True})
|
|
|
|
|
|
|
|
if result['result'] != 'success':
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
f = codecs.open("output-candidate.html", encoding='utf-8', mode="wb")
|
|
|
|
|
|
|
|
def uniwrite(f, s):
|
|
|
|
f.write(s)
|
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, """
|
2013-08-10 03:29:41 +02:00
|
|
|
<html>
|
|
|
|
|
|
|
|
<head>
|
|
|
|
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
|
|
|
|
<meta http-equiv="refresh" content="120" />
|
|
|
|
<link href='http://fonts.googleapis.com/css?family=Source+Sans+Pro:400,700' rel='stylesheet' type='text/css'>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
|
|
|
|
<body>
|
|
|
|
<script>
|
|
|
|
var t=setTimeout(function(){ window.scrollTo(0,document.body.scrollHeight); },500)
|
|
|
|
</script>
|
|
|
|
|
|
|
|
<style type="text/css">
|
|
|
|
body {
|
|
|
|
font-family: 'Source Sans Pro', 'Helvetica Neue', Helvetica, Arial, sans-serif;
|
|
|
|
font-size: 14px;
|
|
|
|
background-color: #eee;
|
|
|
|
}
|
|
|
|
|
|
|
|
p,ul,code,pre {
|
|
|
|
margin-top: 3px;
|
|
|
|
margin-bottom: 3px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.message {
|
|
|
|
background-color: white;
|
|
|
|
border: 1px solid grey;
|
|
|
|
border-radius: 0.25em;
|
|
|
|
padding: 0.25em;
|
|
|
|
padding-left: 0.5em;
|
|
|
|
margin-bottom: 5px;
|
|
|
|
}
|
|
|
|
|
|
|
|
.avatar {
|
|
|
|
width: 25px;
|
|
|
|
height: 25px;
|
|
|
|
margin-right: 10px;
|
|
|
|
border-radius: 25px;
|
|
|
|
border: 1px solid #CCC;
|
|
|
|
|
|
|
|
float: right;
|
|
|
|
}
|
|
|
|
|
|
|
|
.sender {
|
|
|
|
font-weight: bold;
|
|
|
|
}
|
|
|
|
|
|
|
|
.recipient {
|
|
|
|
margin-right: 0.5em;
|
|
|
|
border-radius: 0.25em;
|
|
|
|
padding: 2px;
|
|
|
|
padding-left: 5px;
|
|
|
|
padding-right: 5px;
|
|
|
|
display: inline;
|
|
|
|
}
|
|
|
|
|
|
|
|
.twitter-tweet {
|
|
|
|
border: 1px solid #ddd;
|
|
|
|
padding: .5em .75em;
|
|
|
|
margin-bottom: 0.25em;
|
|
|
|
}
|
|
|
|
|
|
|
|
.twitter-avatar {
|
|
|
|
float: left;
|
|
|
|
width: 48px;
|
|
|
|
height: 48px;
|
|
|
|
margin-right: .75em;
|
|
|
|
}
|
2013-08-13 16:33:14 +02:00
|
|
|
|
|
|
|
.emoji {
|
|
|
|
width: 1.4em;
|
|
|
|
vertical-align: text-bottom;
|
|
|
|
}
|
2013-08-10 03:29:41 +02:00
|
|
|
</style>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<p>For more, join the conversation on <a href="https://zulip.com/register/customer3.invalid" target="_blank">Zulip</a>.</p>
|
|
|
|
""")
|
|
|
|
|
|
|
|
color = {'event1': '#76ce90', 'hongkong': '#fae589', 'etc': '#a6c7e5'}
|
|
|
|
|
|
|
|
for msg in result['messages']:
|
|
|
|
if msg['type'] != 'stream':
|
|
|
|
# Don't show PMs the bot got
|
|
|
|
continue
|
2013-08-13 16:41:02 +02:00
|
|
|
if msg['sender_full_name'] == 'TwitterBot':
|
|
|
|
# Don't put tweets in the iframe
|
|
|
|
continue
|
2013-08-10 03:29:41 +02:00
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, '<div class="message">')
|
2013-08-10 03:29:41 +02:00
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, '<img class="avatar" src="%s" />' % msg['avatar_url'])
|
2013-08-10 03:29:41 +02:00
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, '<div class="sender-bar">')
|
|
|
|
uniwrite(f, '<div class="recipient" style="background-color: %s;">%s > %s</div>' % (color[msg['display_recipient']], msg['display_recipient'], msg['subject']))
|
|
|
|
uniwrite(f, '<span class="sender">%s</span>' % msg['sender_full_name'])
|
|
|
|
uniwrite(f, '</div>')
|
2013-08-10 03:29:41 +02:00
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, '<div class="content">%s</div>' % msg['content'])
|
|
|
|
uniwrite(f, "</div>")
|
2013-08-10 03:29:41 +02:00
|
|
|
|
2016-03-10 16:33:07 +01:00
|
|
|
uniwrite(f, """
|
2013-08-10 03:29:41 +02:00
|
|
|
</div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
""")
|
|
|
|
|
|
|
|
sys.exit(0)
|