Skip to content
Snippets Groups Projects
syncfirestore.py 2.85 KiB
Newer Older
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'))
categories = json.load(open('./out_categories.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_categories():

    for c in categories:
        print(c)
        ref = db.collection(u'categories').document(c['id'])
        ref.set({"label": c['label'], "id": c['id']})


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)

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)

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)



if __name__=='__main__':
    #import_projects()
    #import_files()
    # import_comments()
    # import_photos()
    import_categories()