Skip to content

Instantly share code, notes, and snippets.

@andy0130tw
Last active August 29, 2015 14:27
Show Gist options
  • Save andy0130tw/8d203a1be9233d9b0062 to your computer and use it in GitHub Desktop.
Save andy0130tw/8d203a1be9233d9b0062 to your computer and use it in GitHub Desktop.
Slack backup migration from CodernityDB3
#!/usr/bin/env python3
import json
from os import listdir
from os.path import isfile, join
from CodernityDB3.database import Database
import archv
import peewee
import models as m
dbpath = 'export/'
dirlist = [ f for f in listdir(dbpath) if not isfile(join(dbpath,f)) ]
def init():
print('Init archv')
archv.init()
print('Checking authentication...')
auth_resp = archv.assert_auth()
del auth_resp['ok']
# todo: warn if user use a different database to backup
with m.db.atomic():
for prop in auth_resp:
meta, _ = m.Information.get_or_create(key=prop, value=auth_resp[prop])
if prop == 'team_id' and meta.value != auth_resp['team_id']:
print(' Warning: Team ID is inconsistent.')
def prepare_data():
print('Fetching User list...')
archv.fetch_user_list()
print('Fetching Channel list...')
archv.fetch_channel_list()
print('Fetching Emoji list...')
archv.fetch_emoji_list()
def main():
init()
prepare_data()
with m.db.atomic():
for channel_name in dirlist:
channel_id = (m.Channel
.select(m.Channel.id)
.where(m.Channel.name == channel_name).first().id)
print('Opening channel #{}, id={}...'.format(channel_name, channel_id))
db = Database(join(dbpath, channel_name))
db.open()
cnt = 0
for msg in db.all('id'):
# print(msg)
del msg['_id']
del msg['_rev']
# prevent duplication
if channel_name == 'announce':
ts = msg['ts']
dup = (m.Message.select()
.where(m.Message.channel == channel_id
and m.Message.ts == ts)
).count()
if dup > 0:
continue
msg['channel'] = channel_id
m.Message.api(archv.process_message(msg), True)
cnt += 1
print('count: {}, inserting... '.format(cnt), end='', flush=True)
print('ok')
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment