mirror of https://github.com/zulip/zulip.git
A script to export an entire stream.
(imported from commit 7c8bc0c0c1f9c987bda9503ca53051d2e4ad27dd)
This commit is contained in:
parent
667a3a06cd
commit
ffc75cb074
|
@ -0,0 +1,85 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright © 2014 Dropbox, Inc.
|
||||
#
|
||||
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
# of this software and associated documentation files (the "Software"), to deal
|
||||
# in the Software without restriction, including without limitation the rights
|
||||
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
# copies of the Software, and to permit persons to whom the Software is
|
||||
# furnished to do so, subject to the following conditions:
|
||||
#
|
||||
# The above copyright notice and this permission notice shall be included in
|
||||
# all copies or substantial portions of the Software.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
# THE SOFTWARE.
|
||||
|
||||
import sys
|
||||
import os.path
|
||||
import optparse
|
||||
import codecs
|
||||
import time
|
||||
import json
|
||||
usage = """Export all messages on a given stream to a JSON dump.
|
||||
|
||||
zulip-export --user=<your email address> --api-key=<your api key> --stream=<stream name>
|
||||
|
||||
(You can find your API key on your Settings page.)
|
||||
|
||||
Example: zulip-export --user=wdaher@zulip.com --api-key=a0b1c2d3e4f5a6b7c8d9e0f1a2b3c4d5 --stream=social
|
||||
|
||||
You can omit --user and --api-key arguments if you have a properly set up ~/.zuliprc
|
||||
This script requires the Zulip API bindings to be installed."""
|
||||
sys.path.append(os.path.join(os.path.dirname(__file__), '../../api'))
|
||||
import zulip
|
||||
|
||||
parser = optparse.OptionParser(usage=usage)
|
||||
parser.add_option_group(zulip.generate_option_group(parser))
|
||||
parser.add_option('--stream', default='')
|
||||
(options, args) = parser.parse_args()
|
||||
|
||||
client = zulip.init_from_options(options)
|
||||
|
||||
if options.stream == "":
|
||||
print >>sys.stderr, "Usage:", parser.usage
|
||||
sys.exit(1)
|
||||
|
||||
client.add_subscriptions([{"name": options.stream}])
|
||||
queue = client.register(event_types=['message'])
|
||||
client._register('get_old_messages', method='GET', url='messages')
|
||||
max_id = queue['max_message_id']
|
||||
messages = []
|
||||
print "Fetching messages..."
|
||||
result = client.get_old_messages({'anchor': 0,
|
||||
'num_before': 0,
|
||||
'num_after': max_id,
|
||||
'narrow': [{'operator': 'stream',
|
||||
'operand': options.stream}],
|
||||
'apply_markdown': False})
|
||||
if result['result'] != 'success':
|
||||
print "Unfortunately, there was an error fetching some old messages."
|
||||
print result
|
||||
sys.exit(1)
|
||||
for msg in result['messages']:
|
||||
if msg['type'] != 'stream':
|
||||
continue
|
||||
# Remove extraneous metadata
|
||||
for k in ['flags', 'edit_history', 'subject_links', 'gravatar_hash',
|
||||
'avatar_url', 'recipient_id', 'sender_short_name',
|
||||
'content_type', 'client', 'sender_domain', 'id', 'type']:
|
||||
msg.pop(k, None)
|
||||
messages.append(msg)
|
||||
|
||||
filename = "zulip-%s.json" % options.stream
|
||||
f = codecs.open(filename, encoding='utf-8', mode="wb")
|
||||
f.write(json.dumps(messages, indent=0, sort_keys=False))
|
||||
f.close()
|
||||
print "%d messages exported to %s" % (len(messages), filename,)
|
||||
sys.exit(0)
|
Loading…
Reference in New Issue