view_counter.py 1.38 KB
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()