-
Notifications
You must be signed in to change notification settings - Fork 0
/
song_updater.py
95 lines (71 loc) · 2.39 KB
/
song_updater.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
import time
import requests
import sys
import shutil
import os
from mutagen.id3 import ID3
import base64
import radio_config
config = radio_config.get_config()
web_rootdir = config["web-root"]
icecast_admin_creds = config["icecast-admin"]
icecast_address = config["icecast-address"]
stream_start = float(time.time())
def get_stream_time():
return float(time.time()) - stream_start
def update_stream_title(new_title, mountpoint):
creds_bytes = icecast_admin_creds.encode('ascii')
base64_auth_enc = base64.b64encode(creds_bytes)
base64_auth = base64_auth_enc.decode('ascii')
response = 0
headers = {'authorization': 'Basic {}'.format(base64_auth)}
url = "http://{}/admin/metadata.xsl?song={}&mount=%2F{}&mode=updinfo&charset=UTF-8".format(icecast_address, new_title, mountpoint)
while response != 200:
time.sleep(0.5)
r = requests.get(url, headers=headers)
response = r.status_code
return r
def set_song_cover(file, mountpoint):
id3_data = ID3(file)
if "APIC:Album cover" in id3_data:
cover = id3_data["APIC:Album cover"].data
with open("{}img/cover-{}.png".format(web_rootdir, mountpoint), "wb") as coverfile:
coverfile.write(cover)
coverfile.close()
else:
external_cover_file = file.split(".")[0] + ".png"
destination_image = "{}img/cover-{}.png".format(web_rootdir, mountpoint)
if os.path.exists(external_cover_file):
shutil.copy(external_cover_file, destination_image)
else:
shutil.copy("no-cover.png", destination_image)
def title_updater_start(files, songs, mountpoint, proc, config):
indexx = 0
songs.sort(reverse=False, key=lambda x: int(x[0]))
current_song = None
poll = None
for entry in songs:
indexx += 1 # ahead
current_time_seconds = get_stream_time()
curr_song_start = float(songs[indexx - 1][0])
next_song_start = float(songs[indexx][0])
# ^ TODO: fix IndexError when trying to get last (or before last?) song
donotsend = False
poll = proc.poll()
if poll is not None:
sys.exit("FFmpeg process died")
while poll is None:
current_time_seconds = get_stream_time()
if entry[1] == current_song:
donotsend = True
if current_time_seconds <= next_song_start:
if not donotsend:
req = update_stream_title(entry[1], mountpoint)
set_song_cover(files[indexx - 1], mountpoint)
current_song = entry[1]
else:
break
poll = proc.poll()
if poll is not None:
sys.exit("FFmpeg process died")
time.sleep(1)