From c458c7e095518cead4557cfda20bb846c5f74d2c Mon Sep 17 00:00:00 2001 From: Alexandre Teles Date: Sun, 26 Mar 2023 18:44:01 -0300 Subject: [PATCH] feat: add vote counting script --- poetry.lock | 24 +++++++++++++++++++++++- pyproject.toml | 1 + vote_count.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 vote_count.py diff --git a/poetry.lock b/poetry.lock index 4e320b6..e7ed19d 100644 --- a/poetry.lock +++ b/poetry.lock @@ -1664,6 +1664,28 @@ async-timeout = {version = ">=4.0.2", markers = "python_version < \"3.11\""} hiredis = ["hiredis (>=1.0.0)"] ocsp = ["cryptography (>=36.0.1)", "pyopenssl (==20.0.1)", "requests (>=2.26.0)"] +[[package]] +name = "requests" +version = "2.28.2" +description = "Python HTTP for Humans." +category = "main" +optional = false +python-versions = ">=3.7, <4" +files = [ + {file = "requests-2.28.2-py3-none-any.whl", hash = "sha256:64299f4909223da747622c030b781c0d7811e359c37124b4bd368fb8c6518baa"}, + {file = "requests-2.28.2.tar.gz", hash = "sha256:98b1b2782e3c6c4904938b84c0eb932721069dfdb9134313beff7c83c2df24bf"}, +] + +[package.dependencies] +certifi = ">=2017.4.17" +charset-normalizer = ">=2,<4" +idna = ">=2.5,<4" +urllib3 = ">=1.21.1,<1.27" + +[package.extras] +socks = ["PySocks (>=1.5.6,!=1.5.7)"] +use-chardet-on-py3 = ["chardet (>=3.0.2,<6)"] + [[package]] name = "rfc3986" version = "1.5.0" @@ -2250,4 +2272,4 @@ multidict = ">=4.0" [metadata] lock-version = "2.0" python-versions = "^3.10" -content-hash = "fa4ea468abe4711e4c5f1516228d8b11e72e10876c56f42df98e122676edccb3" +content-hash = "f4b473705cc1919095b7dbf598ca330ebf33ac4c5056a30d802dd853da82bc0f" diff --git a/pyproject.toml b/pyproject.toml index 5acb43b..20c1f7f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,7 @@ hiredis = ">=2.0.0" aiofiles = ">=22.1.0" uvicorn = ">=0.18.3" gunicorn = ">=20.1.0" +requests = "^2.28.2" [tool.poetry.dev-dependencies] mypy = ">=0.991" diff --git a/vote_count.py b/vote_count.py new file mode 100644 index 0000000..3184c17 --- /dev/null +++ b/vote_count.py @@ -0,0 +1,28 @@ +import os +import redis +import requests +from pprint import pprint + +db = redis.StrictRedis( + host=os.environ["REDIS_URL"], port=os.environ["REDIS_PORT"], db=3 +) + +votes: dict = {} + +for key in db.scan_iter("*"): + ballot = db.json().get(key)["votes"] + for entry in ballot: + if entry["vote"]: + if entry["cid"] in votes: + votes[entry["cid"]] += 1 + else: + votes[entry["cid"]] = 1 + +raw_votes: str = pprint.pformat(votes) +sorted_votes: str = pprint.pformat( + dict(sorted(votes.items(), key=lambda item: item[1], reverse=True)) +) + +with open("votes.txt", "w") as f: + f.write(raw_votes) + f.write(sorted_votes)