#!/usr/bin/env python # -*- 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/) import sys sys.path.append('zulip') import os import optparse import codecs usage = """show-last-messages --user= --api-key= --count= Shows the last 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 >>sys.stderr, "Usage:", parser.usage 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,"""

For more, join the conversation on Zulip.

""") 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,'
') uniwrite(f,'' % msg['avatar_url']) uniwrite(f,'
') uniwrite(f,'
%s > %s
' % (color[msg['display_recipient']], msg['display_recipient'], msg['subject'])) uniwrite(f,'%s' % msg['sender_full_name']) uniwrite(f,'
') uniwrite(f,'
%s
' % msg['content']) uniwrite(f,"
") uniwrite(f,"""
""") sys.exit(0)