-
Notifications
You must be signed in to change notification settings - Fork 0
/
firebasestorage.py
56 lines (40 loc) · 1.47 KB
/
firebasestorage.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
import hashlib
import json
import urllib.request
import urllib.error
class FirebaseStorage(object):
url = ""
def __init__(self, logging):
self.logging = logging
def hash(self, article):
bytesarr = bytearray(article["link"], "utf-8")
return hashlib.sha1(bytesarr).hexdigest()
def __prepare(self, article):
ts = article["timestamp"].isoformat()
prepared = article["article"]
prepared["feed"] = article["feed"]
prepared["timestamp"] = ts
prepared["hash"] = self.hash(prepared)
return prepared
def feed_exists(self, article):
url = self.url + "?orderBy=\"hash\"&equalTo=\"{0}\""
url = url.replace("{0}", self.hash(article ))
request = urllib.request.Request(url)
content = urllib.request.urlopen(request)
jsaved = content.read().decode("utf-8")
saved = json.loads(jsaved,"utf-8")
return saved
def __register(self, article):
json_data = json.dumps(article).encode()
request = urllib.request.Request(self.url, data=json_data)
try:
content = urllib.request.urlopen(request)
return True
except urllib.error.URLError:
return False
def save(self, article):
article_prepared = self.__prepare(article)
self.logging.debug("Saving...")
#if not self.__feed_exists(article_prepared):
self.__register(article_prepared)
self.logging.debug("Done")