-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
117 lines (92 loc) · 3.75 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/python
from flask import Flask, send_file, render_template, abort, jsonify
from flask_cors import CORS
from colorama import Fore
from info import ProgramData
from lightyear import generate_lightyear_stats
ProgramData = ProgramData()
from config.config import UserOptions
UserOptions = UserOptions()
import urllib.parse, os, socket, time
app = Flask(__name__)
CORS(app)
MUSIC_DIR = UserOptions.MUSIC_DIR
CAL_DIR = UserOptions.CAL_DIR
app.config["SECRET_KEY"] = "rubrub123"
@app.route("/music/<path:filename>")
def serve_music(filename):
music_file = os.path.join(MUSIC_DIR, filename)
if not os.path.isfile(music_file):
return abort(404) # File not found
print("User listened to " + music_file)
if music_file.rsplit(".", 1)[1] in UserOptions.MUSIC_FILETYPES:
with open(UserOptions.LIGHTYEAR_PATH, "a") as song_log:
song_log.write(filename + "\n")
return send_file(music_file)
@app.route("/")
def index():
artists = os.listdir(MUSIC_DIR)
PROGRAM_NAME = ProgramData.PROGRAM_NAME
return render_template(
"index.html",
artists=artists,
PROGRAM_NAME=PROGRAM_NAME,
number_of_artists=stats["number_of_artists"],
number_of_albums=stats["number_of_albums"],
number_of_songs=stats["number_of_songs"],
lightyear_songs_listened_to=lightyear_stats[0][0]["songs_listened_to"],
lightyear_artists_listened_to=lightyear_stats[0][0]["artists_listened_to"],
lightyear_albums_listened_to=lightyear_stats[0][0]["albums_listened_to"],
lightyear_artists=lightyear_stats[1],
)
@app.route("/<artist>/")
def artist_albums(artist):
artist_dir = os.path.join(MUSIC_DIR, artist)
if not os.path.isdir(artist_dir):
return abort(404) # Artist directory not found
albums = sorted(
[
item
for item in os.listdir(artist_dir)
if os.path.isdir(os.path.join(artist_dir, item))
]
)
return render_template("artist_albums.html", artist=artist, albums=albums)
@app.route("/lightyear/")
def lightyear():
PROGRAM_NAME = ProgramData.PROGRAM_NAME
print(lightyear_stats)
return render_template(
"lightyear.html",
PROGRAM_NAME=PROGRAM_NAME,
songs_listened_to=lightyear_stats[0][0]["songs_listened_to"],
albums_listened_to=lightyear_stats[0][0]["albums_listened_to"],
artists_listened_to=lightyear_stats[0][0]["artists_listened_to"],
artists=lightyear_stats[1],
)
@app.route("/<artist>/<album>/")
def album_songs(artist, album):
artist_dir = os.path.join(MUSIC_DIR, artist)
album_dir = os.path.join(artist_dir, album)
if not os.path.isdir(album_dir):
return abort(404) # Album directory not found
songs = sorted(os.listdir(album_dir))
return render_template("album.html", artist=artist, album=album, songs=songs)
if __name__ == "__main__":
if UserOptions.SCAN_COLLECTION_ON_STARTUP == True:
from scan_music import scan_music
start_time = time.time()
stats = scan_music(MUSIC_DIR, CAL_DIR)
end_time = time.time()
time_taken = str(round(end_time - start_time, 2))
if time_taken.endswith(".0"):
time_taken = time_taken[:-2]
print(f"✅ Scanned music collection in {time_taken} seconds.")
lightyear_stats = generate_lightyear_stats(UserOptions.LIGHTYEAR_PATH)
from waitress import serve
hostname = socket.gethostname()
IPAddr = socket.gethostbyname(hostname)
print(
f"🌐 Visit the Web Management Interface at:\n➡️ {Fore.LIGHTBLUE_EX}{UserOptions.protocol}://{Fore.WHITE}localhost{Fore.LIGHTBLUE_EX}:6969\n➡️ {UserOptions.protocol}://{Fore.WHITE}{IPAddr}{Fore.LIGHTBLUE_EX}:6969"
)
serve(app=app, host="0.0.0.0", port=6969)