From 7c2e4a52d0caf568bf224b0674f577c93b5e5d77 Mon Sep 17 00:00:00 2001 From: ridwaanhall Date: Sat, 16 Mar 2024 21:09:18 +0700 Subject: [PATCH] =?UTF-8?q?Published=F0=9F=9A=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 3 +- .../real_count_pemilu2024.py | 226 ++++++++++++++++++ 2 files changed, 228 insertions(+), 1 deletion(-) create mode 100644 real_count_pemilu2024/real_count_pemilu2024.py diff --git a/.gitignore b/.gitignore index 2c0d99e96..7a6c7fbad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ __pycache__/ secrets/*.py -real_count_pemilu2024/ +real_count_pemilu2024/__init__.py +real_count_pemilu2024/config.py secrets/data/pemilu/hhcw/ppwp/*.json secrets/data/wilayah/pemilu/ppwp/*.json diff --git a/real_count_pemilu2024/real_count_pemilu2024.py b/real_count_pemilu2024/real_count_pemilu2024.py new file mode 100644 index 000000000..8394e2a01 --- /dev/null +++ b/real_count_pemilu2024/real_count_pemilu2024.py @@ -0,0 +1,226 @@ +import requests +from datetime import datetime +from config import Config + +''' +don't delete this message. +created by ridwaanhall +''' + +class DataFetcher: + @staticmethod + def get_data(url): + try: + response = requests.get(url, headers=Config.headers) + response.raise_for_status() + json_data = response.json() + return json_data + except requests.exceptions.RequestException as e: + print("Error fetching data:", e) + return None + +class RealCountPemilu2024: + def __init__(self): + self.urls = { + "ppwp": f"{Config.base_url}/pemilu/ppwp.json", # capres and cawapres + "hhcw": f"{Config.base_url}/pemilu/hhcw/ppwp.json" + } + + def get_data(self): + ppwp_name = DataFetcher.get_data(self.urls["ppwp"]) + stat_reg = DataFetcher.get_data(self.urls["hhcw"]) + return ppwp_name, stat_reg + + def github_pages(self, output_file): + ppwp_name, stat_reg = self.get_data() + + if ppwp_name is not None and stat_reg is not None: + try: + last_update = FormattedDate(stat_reg["ts"]).get_formatted_date() + except ValueError: + last_update = "Unknown" + progress = stat_reg['progres']['progres'] + total_progress = stat_reg['progres']['total'] + percent = stat_reg['chart']['persen'] + + percent_progress = progress / total_progress * 100 + + total_votes = sum(stat_reg['chart'][key] for key in stat_reg['chart'] if key != 'persen') + + html_code_base = f''' + + + + + + Real Count Pemilu 2024 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+

Real Count of Presidential and Vice Presidential Elections 2024

+ +''' + progress_content = f''' +
+
+ REAL COUNT TABLE - PEMILU 2024 +
+ Last update: {last_update} +
+
+
+''' + + html_code_content = ''' + + + + + + + + + + + +''' + + for key, value in ppwp_name.items(): + percent = stat_reg['chart'][key] / total_votes * 100 + name_parts = value['nama'].split(' - ') + # print(name_parts) + presidential = name_parts[0].strip() + # print(presidential)# H. ANIES RASYID BASWEDAN, Ph.D. + vice_presidential = name_parts[1].strip() + # print(vice_presidential) + html_code_content += f''' + + + + + + + +''' + + html_code_content += ''' + +
#PRESIDENTIAL CANDIDATE'S NAMEVICE PRESIDENTIAL CANDIDATE'S NAMEVOTESPRECENT
{value['nomor_urut']:01d}{presidential}{vice_presidential}{stat_reg['chart'][key]:,} of {total_votes:,}{percent:.4f}%
+''' + + html_code_end = f''' +
+ +
+ +
+ + + + +''' + + with open(output_file, 'w') as f: + f.write(html_code_base) + f.write(last_update_content) + f.write(progress_content) + f.write(html_code_content) + f.write(html_code_end) + else: + print("Failed to fetch data from one of the URLs.") + +class FormattedDate: + def __init__(self, date_string): + self.date_string = date_string + + def get_formatted_date(self): + try: + date_object = datetime.strptime(self.date_string, "%Y-%m-%d %H:%M:%S") + formatted_date = date_object.strftime("%d %B %Y %H:%M:%S WIB") + return formatted_date + except ValueError: + return "Invalid date format" + +if __name__ == "__main__": + pemilu_2024 = RealCountPemilu2024() + pemilu_2024.github_pages('index.html')