diff --git a/scripts/transfer_firestore/parsedb.py b/scripts/transfer_firestore/parsedb.py new file mode 100644 index 0000000000000000000000000000000000000000..8a8b3bb6d8a4687c8dd0e0f2fdfe196cd2c725c0 --- /dev/null +++ b/scripts/transfer_firestore/parsedb.py @@ -0,0 +1,56 @@ +#!/usr/bin/python + +import json + +datafile = open('fabricademy_data.json','r') + +data = json.load(datafile) + +projects_list = data['library']['projects'] + + +projects = [] + +for key in projects_list.keys(): + p = projects_list[key] + for k in p.keys(): + print('%s = %s' % (k, p[k])) + print('---') + project = { + "id": key, + "category": p['category'], + "name": p['name'], + "created": p['created'], + "downloaded": p.get('downloaded'), + "description": p['description'], + 'thumbnail': p['thumbnail'], + 'userAvatar': p['userAvatar'], + 'username': p.get('username',''), + 'userId': p['userId'], + 'downloadsCount': p.get('downloadsCount', 0), + 'commentsCount': p.get('commentsCount',0) , + 'tags': p.get('tags',{}).values() , + 'photos': p.get('photos',{}).values(), + 'files': p.get('files',{}).values(), + 'comments': p.get('comments',{}).values() + } + projects.append(project) + +json.dump(projects, open('out_projects.json','w')) + + +category_list = data['library']['categories'] + + +categories = [] + +for key in category_list.keys(): + c = category_list[key] + categories.append({ + "id": c['_id'], + "label": c['label'] + }) + +json.dump(categories, open('out_categories.json','w')) + + diff --git a/scripts/transfer_firestore/requirements.txt b/scripts/transfer_firestore/requirements.txt new file mode 100644 index 0000000000000000000000000000000000000000..9b59c15ec09e0cb140da4e64cee0f4be05a9d850 --- /dev/null +++ b/scripts/transfer_firestore/requirements.txt @@ -0,0 +1 @@ +firebase_admin diff --git a/scripts/transfer_firestore/syncfirestore.py b/scripts/transfer_firestore/syncfirestore.py new file mode 100644 index 0000000000000000000000000000000000000000..c3050fd27e4ac75953aaf1363e38e0b1dc41e759 --- /dev/null +++ b/scripts/transfer_firestore/syncfirestore.py @@ -0,0 +1,95 @@ +import os +import json +import firebase_admin +from firebase_admin import credentials +from firebase_admin import firestore +from dateutil.parser import parse + +current_dir = os.path.dirname(os.path.realpath(__file__)) + +cert_path = os.path.join(current_dir, "../secret/serviceAccountKey.json") + +cred = credentials.Certificate(cert_path) +firebase_admin.initialize_app(cred) +db = firestore.client() + +projects = json.load(open('./out_projects.json','r')) + + +def delete_collection(coll_ref, batch_size): + docs = coll_ref.limit(batch_size).get() + deleted = 0 + + for doc in docs: + print(u'Deleting doc {} => {}'.format(doc.id, doc.to_dict())) + doc.reference.delete() + deleted = deleted + 1 + + if deleted >= batch_size: + return delete_collection(coll_ref, batch_size) + +def import_projects(): + + for p in projects: + + print p['id'] + + project = { + "id": p['id'], + "name": p.get('name','Untitled'), + "category": p['category'], + "description": p['description'], + "created": parse(p['created']), + "downloaded": p.get('downloaded') and parse(p.get('downloaded')) or None, + 'thumbnail': p['thumbnail'], + 'userAvatar': p['userAvatar'], + 'username': p.get('username',''), + 'userId': p['userId'], + 'downloadsCount': p.get('downloadsCount', 0), + 'commentsCount': p.get('commentsCount',0) , + 'tags': p.get('tags', []) + } + ref = db.collection(u'projects').document(p['id']) + + # delete_collection(db.collection(u'projects').document(p['id']).collection(u'tags'),100) + + ref.set(project,True) + + +def import_files(): + + for p in projects: + ref = db.collection(u'projects').document(p['id']) + files = p.get('files', []) + for f in files: + file_ref = ref.collection(u'files').document() + file_ref.set(f) + print f + +def import_photos(): + + for p in projects: + ref = db.collection(u'projects').document(p['id']) + files = p.get('photos', []) + for f in files: + file_ref = ref.collection(u'photos').document() + file_ref.set(f) + print f + +def import_comments(): + + for p in projects: + ref = db.collection(u'projects').document(p['id']) + files = p.get('comments', []) + for f in files: + file_ref = ref.collection(u'comments').document() + file_ref.set(f) + print f + + + +if __name__=='__main__': + #import_projects() + #import_files() + # import_comments() + import_photos() \ No newline at end of file