2021-08-20 09:27:19 +00:00
|
|
|
import json
|
|
|
|
from migration.tables.users import migrate as migrateUser
|
|
|
|
from migration.tables.content_items import migrate as migrateShout
|
|
|
|
from migration.tables.content_item_categories import migrate as migrateTopic
|
|
|
|
from migration.utils import DateTimeEncoder
|
2021-10-04 17:06:05 +00:00
|
|
|
from orm import Community
|
2021-08-20 09:27:19 +00:00
|
|
|
|
2021-08-23 08:44:46 +00:00
|
|
|
def users(limit):
|
2021-08-20 09:27:19 +00:00
|
|
|
print('migrating users...')
|
|
|
|
data = json.loads(open('migration/data/users.json').read())
|
|
|
|
newdata = {}
|
2021-10-04 17:06:05 +00:00
|
|
|
exportData = {}
|
2021-08-20 09:27:19 +00:00
|
|
|
counter = 0
|
2021-10-04 17:06:05 +00:00
|
|
|
# limit = 100
|
2021-08-20 15:10:15 +00:00
|
|
|
#try:
|
|
|
|
for entry in data:
|
|
|
|
oid = entry['_id']
|
2021-10-04 17:06:05 +00:00
|
|
|
user = migrateUser(entry)
|
|
|
|
newdata[oid] = user
|
|
|
|
del user['password']
|
|
|
|
del user['notifications']
|
|
|
|
# del user['oauth']
|
|
|
|
del user['emailConfirmed']
|
|
|
|
del user['username']
|
|
|
|
del user['email']
|
|
|
|
exportData[user['slug']] = user
|
2021-08-20 15:10:15 +00:00
|
|
|
counter += 1
|
2021-08-23 08:44:46 +00:00
|
|
|
if counter > limit:
|
|
|
|
break
|
2021-08-20 15:10:15 +00:00
|
|
|
#except Exception:
|
|
|
|
# print(str(counter) + '/' + str(len(data)) + ' users entries were migrated')
|
|
|
|
# print('try to remove database first')
|
2021-08-20 09:27:19 +00:00
|
|
|
open('migration/data/users.dict.json','w').write( json.dumps(newdata, cls=DateTimeEncoder) )
|
2021-10-04 17:06:05 +00:00
|
|
|
open('../src/data/authors.json','w').write( json.dumps(exportData, cls=DateTimeEncoder) )
|
2021-08-20 09:27:19 +00:00
|
|
|
print(str(counter) + ' users entries were migrated')
|
|
|
|
|
|
|
|
|
2021-10-04 17:06:05 +00:00
|
|
|
def topics():
|
2021-08-20 09:27:19 +00:00
|
|
|
print('migrating topics...')
|
|
|
|
data = json.loads(open('migration/data/content_item_categories.json').read())
|
|
|
|
newdata = {}
|
2021-10-04 17:06:05 +00:00
|
|
|
exportData = {}
|
2021-08-20 09:27:19 +00:00
|
|
|
counter = 0
|
|
|
|
try:
|
|
|
|
for entry in data:
|
|
|
|
oid = entry['_id']
|
|
|
|
newdata[oid] = migrateTopic(entry)
|
2021-10-04 17:06:05 +00:00
|
|
|
exportData[entry['slug']] = newdata[oid]
|
2021-08-20 09:27:19 +00:00
|
|
|
counter += 1
|
|
|
|
except Exception:
|
|
|
|
print(str(counter) + '/' + str(len(data)) + ' topics were migrated')
|
|
|
|
print('try to remove database first')
|
|
|
|
open('migration/data/topics.dict.json','w').write( json.dumps(newdata, cls=DateTimeEncoder) )
|
2021-10-04 17:06:05 +00:00
|
|
|
open('../src/data/topics.json','w').write( json.dumps(exportData, cls=DateTimeEncoder) )
|
2021-08-20 09:27:19 +00:00
|
|
|
print(str(counter) + ' topics were migrated')
|
|
|
|
|
2021-08-23 08:44:46 +00:00
|
|
|
def shouts(limit):
|
|
|
|
print('loading shouts...')
|
2021-08-20 09:27:19 +00:00
|
|
|
counter = 0
|
2021-08-23 08:44:46 +00:00
|
|
|
discoursAuthor = 0
|
2021-08-20 09:27:19 +00:00
|
|
|
data = json.loads(open('migration/data/content_items.json').read())
|
|
|
|
newdata = {}
|
2021-10-04 17:06:05 +00:00
|
|
|
print(str(len(data)) + ' entries loaded. now migrating...')
|
2021-08-25 21:20:53 +00:00
|
|
|
errored = []
|
2021-10-04 17:06:05 +00:00
|
|
|
exportData = {}
|
2021-08-20 09:27:19 +00:00
|
|
|
for entry in data:
|
2021-08-25 21:20:53 +00:00
|
|
|
try:
|
|
|
|
oid = entry['_id']
|
2021-10-04 17:06:05 +00:00
|
|
|
shout = migrateShout(entry)
|
|
|
|
newdata[oid] = shout
|
2021-08-25 21:20:53 +00:00
|
|
|
author = newdata[oid]['authors'][0]['slug']
|
|
|
|
line = str(counter) + ': ' + newdata[oid]['slug'] + " @" + str(author)
|
2021-10-04 17:06:05 +00:00
|
|
|
if shout['layout'] == 'article':
|
|
|
|
counter += 1
|
|
|
|
exportData[shout['slug']] = shout
|
|
|
|
print(line)
|
|
|
|
# counter += 1
|
|
|
|
if author == 'discours.io':
|
|
|
|
discoursAuthor += 1
|
2021-08-25 21:20:53 +00:00
|
|
|
open('./shouts.id.log','a').write(line + '\n')
|
|
|
|
if counter > limit:
|
|
|
|
break
|
|
|
|
except Exception:
|
|
|
|
print(entry['_id'])
|
|
|
|
errored.append(entry)
|
|
|
|
raise Exception
|
2021-08-20 15:10:15 +00:00
|
|
|
|
2021-08-20 09:27:19 +00:00
|
|
|
open('migration/data/shouts.dict.json','w').write( json.dumps(newdata, cls=DateTimeEncoder) )
|
2021-10-04 17:06:05 +00:00
|
|
|
open('../src/data/articles.json','w').write( json.dumps(exportData, cls=DateTimeEncoder) )
|
2021-08-20 09:27:19 +00:00
|
|
|
print(str(counter) + ' shouts were migrated')
|
2021-10-04 17:06:05 +00:00
|
|
|
print(str(discoursAuthor) + ' from them by @discours.io')
|
2021-08-25 21:20:53 +00:00
|
|
|
print(str(len(errored)) + ' shouts without authors')
|
2021-08-20 09:27:19 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
import sys
|
|
|
|
if len(sys.argv) > 1:
|
|
|
|
if sys.argv[1] == "users":
|
2021-10-04 17:06:05 +00:00
|
|
|
users(668)
|
2021-08-20 09:27:19 +00:00
|
|
|
elif sys.argv[1] == "topics":
|
2021-10-04 17:06:05 +00:00
|
|
|
topics()
|
2021-08-20 09:27:19 +00:00
|
|
|
elif sys.argv[1] == "shouts":
|
2021-10-04 17:06:05 +00:00
|
|
|
Community.create(**{
|
|
|
|
'slug': 'discours.io',
|
|
|
|
'name': 'Дискурс',
|
|
|
|
'pic': 'https://discours.io/images/logo-min.svg',
|
|
|
|
'createdBy': '0',
|
|
|
|
'createdAt': ts
|
|
|
|
})
|
|
|
|
shouts(3626)
|
2021-08-20 09:27:19 +00:00
|
|
|
elif sys.argv[1] == "all":
|
2021-10-04 17:06:05 +00:00
|
|
|
topics()
|
|
|
|
users(668)
|
|
|
|
shouts(3626)
|
2021-08-20 09:30:52 +00:00
|
|
|
elif sys.argv[1] == "bson":
|
|
|
|
import migration.bson2json
|
|
|
|
bson2json.json_tables()
|
2021-08-20 09:27:19 +00:00
|
|
|
else:
|
2021-10-04 17:06:05 +00:00
|
|
|
print('usage: python migrate.py <all|topics|users|shouts|comments>')
|