57 lines
2.0 KiB
Python
57 lines
2.0 KiB
Python
import os
|
|
import firebase_admin
|
|
import argparse
|
|
from certificate import Certificate
|
|
from csv_converter import convert
|
|
from firebase_admin import credentials
|
|
from firebase_admin import firestore
|
|
|
|
parser = argparse.ArgumentParser(description="Rubbish front for google cloud firestore API")
|
|
parser.add_argument('--source_file', action='store', dest='source_file',
|
|
type=str, required=True,
|
|
help='which file to read from')
|
|
parser.add_argument('--collection_id', action='store', dest='collection_id',
|
|
type=str, require=True,
|
|
help='The id of the collection the contents of the source file should be added to')
|
|
parser.add_argument('--project_id', action='store', dest='project_id',
|
|
type=str, default='navedu-test',
|
|
help='The id of the project worked in')
|
|
|
|
args = parser.parse_args()
|
|
|
|
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="/Users/alexandermunch-hansen/projects/Python/cloud_firestore/navedu-admin-key.json"
|
|
|
|
|
|
def take_huge_file(long_ass_string: str):
|
|
with open(long_ass_string, 'r') as file:
|
|
bunch_of_shit = file.read()
|
|
csv_files = bunch_of_shit.split("$*$")
|
|
csv_files = [file for file in csv_files if file != '\ufeff']
|
|
return csv_files
|
|
|
|
|
|
def main(source_file, collection_id, project_id):
|
|
# Use the application default credentials
|
|
cred = credentials.ApplicationDefault()
|
|
firebase_admin.initialize_app(cred, {
|
|
'projectId': project_id,
|
|
})
|
|
|
|
db = firestore.client()
|
|
|
|
csv_strings = take_huge_file(f'{source_file}')
|
|
for idx, string in enumerate(csv_strings, start=1):
|
|
if idx < 10:
|
|
num = f'0{idx}'
|
|
else:
|
|
num = idx
|
|
|
|
certificate = convert(string)
|
|
|
|
db.collection(u'certifications').document(f'{collection_id}').collection('subjects').document(f'{num}').set(
|
|
certificate.to_dict())
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main(args.source_file, args.collection_id, args.project_id)
|