zulip/tools/deprecated/iframe-bot/show-last-messages

171 lines
4.2 KiB
Python
Executable File

#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
# Copyright © 2013 Zulip, Inc.
### NOTE: You must copy the emoji along with this script if you want
### them to work properly
### (to static/third/gemoji/images/)
from __future__ import print_function
import sys
sys.path.append('zulip')
import os
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.
Examples: subscribe --user=username@example.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --count=5 --streams=social
subscribe --user=username@example.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --streams='foo bar'
You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc
"""
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
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 == "":
print("Usage:", parser.usage, file=sys.stderr)
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)
uniwrite(f, """
<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;
}
.emoji {
width: 1.4em;
vertical-align: text-bottom;
}
</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
if msg['sender_full_name'] == 'TwitterBot':
# Don't put tweets in the iframe
continue
uniwrite(f, '<div class="message">')
uniwrite(f, '<img class="avatar" src="%s" />' % msg['avatar_url'])
uniwrite(f, '<div class="sender-bar">')
uniwrite(f, '<div class="recipient" style="background-color: %s;">%s &gt; %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>')
uniwrite(f, '<div class="content">%s</div>' % msg['content'])
uniwrite(f, "</div>")
uniwrite(f, """
</div>
</body>
</html>
""")
sys.exit(0)