From 91fbd168a2d1b93de6f000d921c949a9543ae7e8 Mon Sep 17 00:00:00 2001 From: Halhadus Date: Mon, 9 Dec 2024 20:33:05 +0300 Subject: [PATCH] Added years --- websitemodule.py | 41 +++++++++++++++++++++++++---------------- 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/websitemodule.py b/websitemodule.py index b0e2038..2b7d02c 100644 --- a/websitemodule.py +++ b/websitemodule.py @@ -3,11 +3,7 @@ import sqlite3 import requests import datetime -def read_music_database(url): - os.remove("assets/localwrapped/music.db") - get_music_database = requests.get(url) - with open("assets/localwrapped/music.db", "wb") as f: - f.write(get_music_database.content) +def read_music_database(): conn = sqlite3.connect("assets/localwrapped/music.db") c = conn.cursor() c.execute('SELECT * FROM music') @@ -15,23 +11,31 @@ def read_music_database(url): conn.close() return music_list -def read_count_database(): - conn = sqlite3.connect('assets/localwrapped/count.db') - c = conn.cursor() - c.execute('SELECT * FROM count') - count_list = c.fetchall() - conn.close() - return count_list +def read_count_database(year: int: datetime.datetime.now().year): + if year == datetime.datetime.now().year: + conn = sqlite3.connect(f'assets/localwrapped/count.db') + c = conn.cursor() + c.execute('SELECT * FROM count') + count_list = c.fetchall() + conn.close() + return count_list + else: + conn = sqlite3.connect(f'assets/localwrapped/count-{year}.db') + c = conn.cursor() + c.execute('SELECT * FROM count') + count_list = c.fetchall() + conn.close() + return count_list -def total_play_time(url): +def total_play_time(year: int): total_time = 0 for countvar in read_count_database(): - for musicvar in read_music_database(url): + for musicvar in read_music_database(year): if countvar[0] == musicvar[0]: total_time += float(musicvar[-1]) * countvar[1] return total_time -def generatehtmlcode(url): +def generatehtmlcode(year: int): html = '\n\n' html += '\n\n\n\n' html += '\n\n' @@ -41,9 +45,14 @@ def generatehtmlcode(url): html += '\n' html += '

Halhadus\' Local Wrapped

\n' html += 'Source Code\n' + html += '

Hi guys. I am too lazy to add links to song names. To find which song is it, check this link.

\n' + html += '

' + str(year) + '

\n' html += '
\n\n
\n' html += '

Total Play Time: ' + str(int(total_play_time(url)/60)) + ' minutes

\n' - html += '

Last Updated: ' + datetime.datetime.utcfromtimestamp(os.path.getmtime("assets/localwrapped/music.db")).strftime('%Y-%m-%d %H:%M:%S') + ' (server time)

\n' + if year == datetime.datetime.now().year: + html += '

Last Updated: ' + datetime.datetime.utcfromtimestamp(os.path.getmtime('assets/localwrapped/count.db').strftime('%Y-%m-%d %H:%M:%S') + ' (UTC +0, I hope.)

\n' + else: + html += '

Last Updated: ' + datetime.datetime.utcfromtimestamp(os.path.getmtime(f'assets/localwrapped/count-{year}.db').strftime('%Y-%m-%d %H:%M:%S') + ' (UTC +0, I hope.)

\n' html += '

Most Played Musics

\n' html += '
    \n' count_list_formatted = []