From 70237b5a5d82e8425eb5870a975bde497a6def08 Mon Sep 17 00:00:00 2001 From: Pinapelz Date: Tue, 20 Jun 2023 02:14:34 -0700 Subject: Refactored entire codebase - Replaced most code with HTML templates - Fixed some janky SQL queries - Replaced config.py with ini and json --- html_builders/tables.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 html_builders/tables.py (limited to 'html_builders/tables.py') diff --git a/html_builders/tables.py b/html_builders/tables.py new file mode 100644 index 0000000..87d2b4a --- /dev/null +++ b/html_builders/tables.py @@ -0,0 +1,72 @@ +def generate_html_table(server, table, diff_table, headers=["Rank", "Liver", "Subscriber", "Difference (24hr)"], root_url="https://www.nijitracker.com"): + def get_daily_difference_subs(sub_count_str: str): + diff_cursor = server.get_connection().cursor() + diff_cursor.execute(query) + diff_data = diff_cursor.fetchall() + old_sub_count = int(diff_data[0][0]) + current_sub_count = int(sub_count_str) + if old_sub_count > current_sub_count: + return f"-{old_sub_count - current_sub_count}" + else: + return f"+{current_sub_count - old_sub_count}" + cursor = server.get_connection().cursor() + query = f"SELECT id, channel_id, name, subscriber_count, timestamp, profile_pic FROM {table} ORDER by subscriber_count DESC" + cursor.execute(query) + data = cursor.fetchall() + header = "" + for h in headers: + header += f"{h}" + header += "" + table_html = "" + table_html += header + rank = 1 + for row in data: + table_row = "" + table_row += f"" + rank += 1 + for i, col in enumerate(row): + if cursor.description[i][0] == "name": + channel_url = f"{root_url}/stats/{row[2]}" + profile_pic_url = row[5] + table_row += f"" + elif cursor.description[i][0] == "subscriber_count": + formatted_sub_count = "{:,.0f}".format(int(col)) + table_row += f"" + elif cursor.description[i][0] == "timestamp": + query = f"SELECT sub_diff FROM {diff_table} WHERE channel_id = '{row[1]}'" + try: + table_row += f"" + except IndexError: + raise Exception("Are you trying to use a new set of channels?\nPlease delete last_refresh.txt in data folder first!") + elif cursor.description[i][0] not in ["id", "channel_id", "profile_pic"]: + table_row += f"" + table_row += "" + table_html += table_row + table_html += "
{rank}{col}{formatted_sub_count}{get_daily_difference_subs(row[3])}{col}
" + cursor.close() + return table_html + +def generate_individual_table(server, table_name, channel_name, param="LIMIT 7"): + cursor = server.get_connection().cursor() + query = f"SELECT subscriber_count, timestamp FROM {table_name} WHERE name=\"{channel_name}\" GROUP BY DATE(timestamp) ORDER by timestamp DESC " + param + cursor.execute(query) + data = cursor.fetchall() + table = "" + header = "" + header_cols = ["Subscribers", "Timestamp"] + for col in header_cols: + header += f"" + header += "" + table += header + for row in data: + table += "" + for i, col in enumerate(row): + if cursor.description[i][0] == "subscriber_count": + formatted_sub_count = "{:,.0f}".format(int(col)) + table += f"" + else: + table += f"" + table += "" + table += "
{col}
{formatted_sub_count}{col}
" + cursor.close() + return table \ No newline at end of file -- cgit v1.2.3