-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_chapter_images_to_storage.py
53 lines (43 loc) · 1.42 KB
/
replace_chapter_images_to_storage.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
import firebase_admin
from firebase_admin import credentials
from firebase_admin import firestore
from urllib.request import urlopen
from bs4 import BeautifulSoup
import json
import time
# Use a service account
cred = credentials.Certificate('./service.json')
firebase_admin.initialize_app(cred)
db = firestore.client()
# topic = input("Enter Course Name: ")
# if not topic:
# exit('topic can\'t be empty of null')
docs = db.collection('courses').stream()
chapters = list(docs)
for chapter in chapters:
#chapter = list(docs)[0]
print('Chapter: ', chapter.to_dict().get('name'))
if chapter.to_dict().get("topics") is None:
continue
topics = list(chapter.to_dict().get("topics"))
newTopics = list(topics)
for i, item in enumerate(topics):
id = item['id']
print('id: ', id)
doc_ref = db.collection(u'numericals').document(f'{id}')
doc = doc_ref.get()
if doc.exists:
print(doc.to_dict()['title'])
solns = doc.to_dict()['solutions'][0]['images']
# print(solns)
item.update({'images': solns})
newTopics.pop(i)
newTopics.insert(i, item)
else:
print(id, ' doesnt exist!')
print(newTopics)
if len(newTopics) != 0:
chapter_ref = db.collection(u'courses').document(chapter.id)
chapter_ref.update({'topics': newTopics})
else:
print('list cannot empty')