-
Notifications
You must be signed in to change notification settings - Fork 0
/
firestore.py
67 lines (49 loc) · 1.49 KB
/
firestore.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
63
64
65
66
67
import firebase_admin
from datetime import datetime
from firebase_admin import credentials
from firebase_admin import firestore
# Use a service account.
cred = credentials.Certificate(r"token.json")
app = firebase_admin.initialize_app(cred)
db = firestore.client()
def insert_book(title, url, text, emotions, hate_speech, author, main_img):
data = {
u'title': title,
u'url': url,
u'text': text,
u'emotions': emotions,
u'hate_speech': hate_speech,
u'date_uploaded': datetime.utcnow(),
u'author': author,
u'main_img': main_img
}
db.collection("books").document(title).set(data)
def get_book(title):
doc_ref = db.collection("books").document(title)
doc = doc_ref.get()
if doc.exists:
return doc.to_dict()
else:
return {}
def get_all_books():
docs = db.collection("books").stream() # .where(u'', u'==', True)
return docs
def insert_user_book(title, text, emotions, hate_speech, book_id):
data = {
u'title': title,
u'text': text,
u'emotions': emotions,
u'hate_speech': hate_speech,
u'date_uploaded': datetime.utcnow(),
u'book_id': book_id
}
db.collection("user_books").document(book_id).set(data)
def get_user_book(book_id):
doc_ref = db.collection("user_books").document(book_id)
doc = doc_ref.get()
if doc.exists:
return doc.to_dict()
else:
return {}
if __name__ == '__main__':
get_book("test title")