-
Notifications
You must be signed in to change notification settings - Fork 0
/
database.py
39 lines (32 loc) · 934 Bytes
/
database.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
import pyrebase
import requests
import json
from pprint import pprint
config = {
"apiKey": "AIzaSyAZcr4Y7dS97h_ZJ1aCyl2JEUyRecYkhEM",
"authDomain": "newsnowtest.firebaseapp.com",
"databaseURL": "https://newsnowtest.firebaseio.com",
"storageBucket": "newsnowtest.appspot.com",
"serviceAccount": "newsnowtest-firebase-adminsdk-r2185-e476e5c430.json"
}
firebase = pyrebase.initialize_app(config)
db = firebase.database()
# Add all sources
# sources: list of JSON objects
def setSources(sources):
for src in sources:
db.child("sources/" + src["id"]).set(src)
# Add all topics
# topics: list of Strings
def setTopics(topics):
db.child("allTopics").set(topics)
# Clear all articles
def clearAllArticles():
db.child("articles").remove()
# Write an article
# article: dictionary of article data
def addArticle(article):
try:
db.child("articles").push(article)
except TypeError as e:
print(e, type(article))