First commit

This commit is contained in:
Halhadus 2024-12-07 00:16:47 +03:00
parent 7866015508
commit 1dc3725951
3 changed files with 135 additions and 2 deletions

View file

@ -1,2 +0,0 @@
# my-local-wrapped

83
getmusic.py Normal file
View file

@ -0,0 +1,83 @@
import subprocess
import re
import time
import sqlite3
import os
def get_music():
dumpsys_output = subprocess.check_output("dumpsys media_session", shell=True).decode("utf-8").split("\n")
if not [line for line in dumpsys_output if "PLAYING" in line]:
return None
else:
description = [line for line in dumpsys_output if "description=" in line]
try:
return description[0].split("description=")[1]
except:
return None
def get_db_values():
musicconn = sqlite3.connect("music.db")
musicc = musicconn.cursor()
musicc.execute("SELECT * FROM music")
musicrows = musicc.fetchall()
musicconn.close()
musicvalues = []
for row in musicrows:
musicvalues.append({
"listmusicname": row[0],
"fileartistname": row[4],
"filealbumname": row[5],
"filemusictitle": row[6]
})
return musicvalues
if not os.path.exists("count.db"):
countconn = sqlite3.connect("count.db")
countc = countconn.cursor()
countc.execute("CREATE TABLE count (musicname TEXT, count INTEGER)")
countconn.commit()
countconn.close()
a = 0
last_written = None
while True:
if get_music() == None:
continue
last_music = get_music()
time.sleep(3)
if last_music == get_music():
if last_written == last_music:
continue
a += 1
if a == 2:
for i in get_db_values():
if i["filealbumname"] == None:
full_name = i["filemusictitle"] + ", " + i["fileartistname"] + ", " + "Music"
else:
full_name = i["filemusictitle"] + ", " + i["fileartistname"] + ", " + i["filealbumname"]
if full_name == last_music:
countconn = sqlite3.connect("count.db")
countc = countconn.cursor()
countc.execute("SELECT * FROM count")
countdbrows = countc.fetchall()
countdbvalues = []
for row in countdbrows:
countdbvalues.append({
"musicname": row[0],
"count": row[1]
})
if not [row for row in countdbvalues if row["musicname"] == i["listmusicname"]]:
countc.execute("INSERT INTO count VALUES (?, ?)", (i["listmusicname"], 1))
countconn.commit()
countconn.close()
else:
countc.execute("UPDATE count SET count = count + 1 WHERE musicname = ?", (i["listmusicname"],))
countconn.commit()
countconn.close()
last_written = last_music
a = 0
continue
else:
a = 0
continue

52
websitemodule.py Normal file
View file

@ -0,0 +1,52 @@
import os
import sqlite3
import requests
def read_music_database(url):
conn = sqlite3.connect(requests.get(url).content)
c = conn.cursor()
c.execute('SELECT * FROM music')
music_list = c.fetchall()
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 total_play_time(url):
total_time = 0
for countvar in read_count_database():
for musicvar in read_music_database(url):
if countvar[0] == musicvar[0]:
total_time += float(musicvar[-1]) * countvar[1]
return total_time
def generatehtmlcode(url):
html = '<DOCTYPE html>\n<html>\n'
html += '<head>\n<meta charset="UTF-8">\n<link href="https://fonts.googleapis.com/css2?family=Jetbrains+Mono:wght@400;700&display=swap" rel="stylesheet">\n</head>\n'
html += '<body style="background-color: #1f1f1f; color: #ffffff; font-family: \'Jetbrains Mono\', monospace;">\n<meta name="viewport" content="width=device-width, initial-scale=1.0">\n'
html += '<title>Halhadus\' Local Wrapped</title>\n<link rel="icon" type="image/png" href="assets/favicon.png">\n'
html += '<meta name="description" content="Halhadus\' Local Wrapped">\n<meta property="og:title" content="Halhadus\' Local Wrapped">\n'
html += '<meta property="og:description" content="Halhadus\' Local Wrapped">\n<meta property="og:image" content="assets/favicon.png">\n'
html += '<meta property="og:url" content="https://halhadus.rocks/localwrapped.html">\n'
html += '<h1>Halhadus\' Local Wrapped</h1>\n'
html += '<h2>Total Play Time: ' + str(int(total_play_time(url)/60)) + ' minutes</h2>\n'
html += '<h3>Last Updated: ' + os.path.getmtime('assets/localwrapped/count.db') + '</h3>\n'
html += '<h3>Most Played Musics</h3>\n'
html += '<ol>\n'
count_list_formatted = []
for music in read_count_database():
music_name = music[0]
count = music[1]
count_list_formatted.append([music_name, count])
count_list_formatted.sort(key=lambda x: x[1], reverse=True)
for music in count_list_formatted:
html += '<li>' + music[0] + ' | ' + str(music[1]) + ' times</li>\n'
html += '</ol>\n'
html += '</body>\n</html>'
return html