-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload_to_firebase.py
62 lines (48 loc) · 1.35 KB
/
upload_to_firebase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import firebase_admin
from firebase_admin import credentials, firestore
def initialize_firebase(cred_path):
"""
Initialize Firebase with given credentials.
"""
cred = credentials.Certificate(cred_path)
firebase_admin.initialize_app(cred)
return firestore.client()
def load_json_data(file_path):
"""
Load data from a JSON file.
"""
with open(file_path, "r") as f:
return json.load(f)
def add_deck_to_db(db, deck_name, description="Sample description"):
"""
Add a deck to the database.
"""
deck_ref = db.collection("decks").add(
{"name": deck_name, "description": description}
)[1]
return deck_ref
def add_card_to_db(db, deck_ref, card, score=2):
"""
Add a card to the database.
"""
db.collection("cards").add(
{
"deckId": deck_ref.id,
"front": card["front"],
"back": card["back"],
"score": score,
}
)
def main(
cred_path="quiz-app-f9560-firebase-adminsdk-vxdaq-469cd9ab06.json",
data_path="your_data.json",
):
db = initialize_firebase(cred_path)
data = load_json_data(data_path)
for deck in data:
deck_ref = add_deck_to_db(db, deck["deckName"])
for card in deck["cards"]:
add_card_to_db(db, deck_ref, card)
if __name__ == "__main__":
main()