Skip to content
Snippets Groups Projects
Commit e3c253b1 authored by fibasile's avatar fibasile
Browse files

firebase -> firestory sync scripts

parent 98a9d242
No related branches found
No related tags found
No related merge requests found
#!/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'))
firebase_admin
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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment