core/migrate.py

89 lines
3.0 KiB
Python
Raw Normal View History

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-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 = {}
counter = 0
2021-08-20 15:10:15 +00:00
#try:
for entry in data:
oid = entry['_id']
newdata[oid] = migrateUser(entry)
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) )
print(str(counter) + ' users entries were migrated')
2021-08-23 08:44:46 +00:00
def topics(limit):
2021-08-20 09:27:19 +00:00
print('migrating topics...')
data = json.loads(open('migration/data/content_item_categories.json').read())
newdata = {}
counter = 0
try:
for entry in data:
oid = entry['_id']
newdata[oid] = migrateTopic(entry)
counter += 1
2021-08-23 08:44:46 +00:00
if counter > limit:
break
2021-08-20 09:27:19 +00:00
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) )
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-08-23 08:44:46 +00:00
print(str(len(data)) + ' entries was loaded. now migrating...')
2021-08-20 09:27:19 +00:00
for entry in data:
oid = entry['_id']
newdata[oid] = migrateShout(entry)
counter += 1
2021-08-23 08:44:46 +00:00
author = newdata[oid]['authors'][0]['slug']
if author == 'discours':
discoursAuthor += 1
line = str(counter) + ': ' + newdata[oid]['slug'] + " @" + author
print(line)
open('./shouts.id.log','a').write(line + '\n')
if counter > limit:
2021-08-20 15:10:15 +00:00
break
2021-08-20 09:27:19 +00:00
open('migration/data/shouts.dict.json','w').write( json.dumps(newdata, cls=DateTimeEncoder) )
print(str(counter) + ' shouts were migrated')
2021-08-23 08:44:46 +00:00
print(str(discoursAuthor) + ' from them by uknown users')
2021-08-20 09:27:19 +00:00
if __name__ == '__main__':
import sys
if len(sys.argv) > 1:
2021-08-23 08:44:46 +00:00
limit = int(sys.argv[2])
2021-08-20 09:27:19 +00:00
if sys.argv[1] == "users":
2021-08-23 08:44:46 +00:00
users(limit)
2021-08-20 09:27:19 +00:00
elif sys.argv[1] == "topics":
2021-08-23 08:44:46 +00:00
topics(limit)
2021-08-20 09:27:19 +00:00
elif sys.argv[1] == "shouts":
2021-08-23 08:44:46 +00:00
shouts(limit)
2021-08-20 09:27:19 +00:00
elif sys.argv[1] == "comments":
2021-08-23 08:44:46 +00:00
comments(limit)
2021-08-20 09:27:19 +00:00
pass
elif sys.argv[1] == "all":
2021-08-23 08:44:46 +00:00
topics(limit)
users(limit)
shouts(limit)
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-08-23 08:44:46 +00:00
print('usage: python migrate.py <all|topics|users|shouts|comments> <stop_index>')