view_counter.py
1.38 KB
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
from firebase_admin import db
from firebase_admin import credentials
import firebase_admin
import json
from datetime import datetime
cred = credentials.Certificate('wello_firebase_SDKKey.json')
firebase_admin.initialize_app(cred, {'databaseURL': 'https://capstone-vip.firebaseio.com/'})
ref = db.reference()
policies = ref.get()
## compare dict
json_data = dict()
today = str(datetime.now().year) + "-" + str(datetime.now().month) + "-" + str(datetime.now().day)
def view_count_raw_writer():
with open('./view_count_raw.json', 'w', encoding='utf-8') as make_file:
json.dump(json_data, make_file, indent="\t")
def count_update():
for policy in policies:
if policy["Value"] in json_data.keys():
if policy["Value"][datetime.now()] in json_data["Value"].keys():
json_data[policy["Value"]][today] = abs(policy["View"])
else:
json_data[policy["Value"]][today] = dict()
json_data[policy["Value"]][today] = abs(policy["View"])
else:
json_data[policy["Value"]] = dict()
json_data[policy["Value"]][today] = abs(policy["View"])
def view_count_raw_reader():
with open('./view_count_raw.json', 'r', encoding='utf-8') as json_file:
json_data = json.load(json_file)
view_count_raw_reader()
count_update()
print(json.dumps(json_data, indent="\t"))
view_count_raw_writer()