diff --git a/read_blockchain.py b/read_blockchain.py index e6cb092..2e3a5d5 100644 --- a/read_blockchain.py +++ b/read_blockchain.py @@ -4,24 +4,26 @@ from eth_account import Account from web3.middleware import construct_sign_and_send_raw_middleware import asyncio - +from typing import Tuple PRIVATE_KEY = os.getenv('PRIVATE_KEY') CONTRACT_ADDRESS = '0xca733a39b72DA72078DBc1c642e6C3836C5b424E' PROVIDER_HTTP_ENDPOINT = "http://54.153.142.251:8545" -def get_votes_from_blochchain(spec_hash): +def get_votes_from_blochchain(spec_hash: str) -> Tuple[int,int]: + """For interacting with the smart contract and getting the current vote counts of a ballot + + Args: + spec_hash (str): A unique sha256 hash of the ballot info + + Returns: + Tuple[int,int]: (yes count, no count) + """ w3 = Web3(Web3.HTTPProvider(PROVIDER_HTTP_ENDPOINT)) acct = Account.from_key(PRIVATE_KEY) abi = json.load(open("voting.json")) - VotingAlpha = w3.eth.contract(address=CONTRACT_ADDRESS, abi=abi) - w3.middleware_onion.add(construct_sign_and_send_raw_middleware(acct.privateKey)) - print(spec_hash) p_id = VotingAlpha.functions.getProposalId(spec_hash.replace("0x0x","0x")).call() proposalInfo = VotingAlpha.functions.getProposal(p_id).call() - - print("No votes:", proposalInfo[3]) - print("Yes votes:", proposalInfo[4]) - return(proposalInfo[4], proposalInfo[3]) + return(proposalInfo[4], proposalInfo[3]) \ No newline at end of file diff --git a/tag_topics.py b/tag_topics.py index b05b9d7..5f6dcea 100644 --- a/tag_topics.py +++ b/tag_topics.py @@ -1,5 +1,6 @@ import requests from operator import itemgetter +from typing import Dict, List # Topics citizens = ["Education","Skills","Employment","Health", "Veterans", "youth","sports","Social","Indigenous","Disability"] @@ -12,32 +13,40 @@ topics = { "citizens":citizens, "nature":nature, - "national_development":national_development, + "national development":national_development, "borders":borders, "economy":economy, "communications":communications, } -# related terms -rt = open("related_terms.csv","r") -topic_lines = rt.readlines() -headings = topic_lines.pop(0) -headings_list = headings.replace("\n","").lower().split(",") +def tag_bill(bill: Dict) -> List[str]: + """To make tags for a bill based on the bill protfolio, title and text -topic_models = {} -for i in range(len(headings.replace("\n","").split(","))): - terms = [] - for tl in topic_lines: - term = tl.replace("\n","").split(",")[i] - if term != "": - terms.append(term.lower()) - topic_models[headings_list[i]] = terms + Args: + bill (Dict): A dict with the bill info. Need "short_title","summary". "portfolio" is preferred too + Returns: + List[str]: The tags + """ + # * get the related terms + rt = open("related_terms.csv","r") + topic_lines = rt.readlines() + headings = topic_lines.pop(0) + headings_list = headings.replace("\n","").lower().split(",") + + topic_models = {} + for i in range(len(headings.replace("\n","").split(","))): + terms = [] + for tl in topic_lines: + term = tl.replace("\n","").split(",")[i] + if term != "": + terms.append(term.lower()) + topic_models[headings_list[i]] = terms -def tag_bill(bill): t = [] if "portfolio" in bill.keys(): if bill["portfolio"] != "": + # * use portfolio to make the tags portfoilo = bill["portfolio"] for topic in topics.keys(): for domain in topics[topic]: @@ -47,11 +56,11 @@ def tag_bill(bill): if t != []: return(t) else: + # * use p = [] for h in headings_list: for rt in topic_models[h]: if rt in bill["short_title"].lower() or rt in bill["summary"].lower(): if h not in p: p.append(h) - return(p) - + return(p) \ No newline at end of file diff --git a/update_bills_db.py b/update_bills_db.py index 92d450b..c5b826a 100644 --- a/update_bills_db.py +++ b/update_bills_db.py @@ -9,27 +9,17 @@ from tag_topics import tag_bill -# Connection String -client = pymongo.MongoClient(mongosettings[URL]) -db = client[mongosettings[MONGODB]] -bills_collection = db[mongosettings[BILLSCOLLECTION]] - - -# dummy function, waiting for votes to be counted on the blockchain. -def get_votes(id): - return(500 + int(random.random()*1000), 500 + int(random.random()*1000)) - - -naughty_words = ['corona', 'covid'] -# print(all_bills) - -bad_bills = [] - - def run(event, context): + # Connection String + client = pymongo.MongoClient(mongosettings[URL]) + db = client[mongosettings[MONGODB]] + bills_collection = db[mongosettings[BILLSCOLLECTION]] + # ! this is a quick fix for the google ban on covid apps + naughty_words = ['corona', 'covid'] + bad_bills = [] for i in range(len(all_bills)): post = True - print(all_bills[i]["id"]) + # print(all_bills[i]["id"]) url = all_bills[i]["url"] bill = Bill(url) # Standed keys