|
@@ -1,5 +1,6 @@
|
|
|
#!/usr/bin/env python3
|
|
|
#Playlistalo - simpatico script che legge le cartelle e genera la playlist
|
|
|
+#Requirements: youtube_dl Mastodon.py python-telegram-bot validators python-musicpd
|
|
|
|
|
|
import youtube_dl
|
|
|
import shutil
|
|
@@ -33,7 +34,7 @@ ARCHIVE = True
|
|
|
TELEGRAM_TOKEN = ""
|
|
|
MASTODON_TOKEN = ""
|
|
|
MASTODON_URL = ""
|
|
|
-
|
|
|
+ANNOUNCEREPEAT = 4
|
|
|
|
|
|
#Scrivi la prima configurazione
|
|
|
configfile = 'playlistalo.conf'
|
|
@@ -67,7 +68,8 @@ def addurl(url, user = "-unknown-"):
|
|
|
|
|
|
ydl_opts = {
|
|
|
'format': 'bestaudio[ext=m4a]',
|
|
|
- 'outtmpl': 'cache/%(id)s.m4a',
|
|
|
+ 'outtmpl': 'cache/%(id)s.m4a',
|
|
|
+ 'no-cache-dir': True,
|
|
|
'noplaylist': True,
|
|
|
'quiet': True,
|
|
|
}
|
|
@@ -183,7 +185,9 @@ def listplaylist():
|
|
|
f = open(udir + "/last", "r")
|
|
|
last = f.readline().rstrip()
|
|
|
else:
|
|
|
- last = os.path.basename(sorted(glob(udir + "/*"))[0]).split("|")[0]
|
|
|
+ files = [x for x in sorted(glob(udir + "/*")) if not os.path.basename(x) == "last"]
|
|
|
+ if files:
|
|
|
+ last = os.path.basename(files[0]).split("|")[0]
|
|
|
#print ("LAST: " + last)
|
|
|
|
|
|
#leggi i file nella cartella
|
|
@@ -361,10 +365,30 @@ def mastodon_bot():
|
|
|
def listtot(res = sys.maxsize):
|
|
|
plt = listplaylist()
|
|
|
if len(plt) < res:
|
|
|
- [plt.append(x) for x in listfallback()]
|
|
|
+ announcepos = getlastannounce()
|
|
|
+ for x in listfallback():
|
|
|
+ if announcepos == 0:
|
|
|
+ if os.path.exists("announce/repeat.mp3"):
|
|
|
+ plt.append(["announce/repeat.mp3"])
|
|
|
+ announcepos = (announcepos + 1) % ANNOUNCEREPEAT
|
|
|
+
|
|
|
+ plt.append(x)
|
|
|
return plt[:res]
|
|
|
|
|
|
|
|
|
+def getlastannounce():
|
|
|
+ announcepos = ANNOUNCEREPEAT
|
|
|
+ try:
|
|
|
+ with open("announce/last","r") as f:
|
|
|
+ announcepos=int(f.readline().rstrip())
|
|
|
+ except:
|
|
|
+ pass
|
|
|
+ return announcepos
|
|
|
+
|
|
|
+def setlastannounce(announcepos):
|
|
|
+ with open("announce/last","w") as f:
|
|
|
+ f.write(announcepos = (announcepos + 1) % ANNOUNCEREPEAT)
|
|
|
+
|
|
|
def consume(song):
|
|
|
if os.path.exists(song):
|
|
|
if song.split("/")[0] == "playlist":
|
|
@@ -374,12 +398,17 @@ def consume(song):
|
|
|
else:
|
|
|
with open(os.path.dirname(song) + "/last", "w") as f:
|
|
|
f.write(time.strftime("%Y%m%d%H%M%S"))
|
|
|
+ #resetta il contatore announcelast
|
|
|
+ setlastannounce(0)
|
|
|
|
|
|
elif song.split("/")[0] == "fallback":
|
|
|
fname = time.strftime("%Y%m%d%H%M%S") + "|" + "|".join(os.path.basename(song).split("|")[1:])
|
|
|
fname = os.path.dirname(song) + "/" + fname
|
|
|
os.rename(song, fname)
|
|
|
-
|
|
|
+
|
|
|
+ elif song.split("/")[0] == "announce":
|
|
|
+ announcepos = getlastannounce()
|
|
|
+ setlastannounce(announcepos = (announcepos + 1) % ANNOUNCEREPEAT)
|
|
|
|
|
|
|
|
|
def addstartannounce():
|
|
@@ -422,6 +451,8 @@ def plaympd():
|
|
|
|
|
|
#aggiunge l'annuncio iniziale
|
|
|
addstartannounce()
|
|
|
+ #resetta il lastannounce
|
|
|
+ setlastannounce(ANNOUNCEREPEAT)
|
|
|
|
|
|
#riempe la playlist
|
|
|
plt = listtot(listlen)
|
|
@@ -448,9 +479,9 @@ def plaympd():
|
|
|
# print(client.playlistinfo())
|
|
|
# print()
|
|
|
|
|
|
-
|
|
|
#controlla se il pezzo e' il primo e consuma le precedenti
|
|
|
status = client.status()
|
|
|
+
|
|
|
if int(status['song']) > 0:
|
|
|
#consuma la canzone attuale
|
|
|
song = client.playlistinfo()[int(status['song'])]['file']
|
|
@@ -459,9 +490,13 @@ def plaympd():
|
|
|
|
|
|
if len(client.playlistinfo()) < listlen:
|
|
|
mpdsync(client, listlen)
|
|
|
+ status = client.status()
|
|
|
|
|
|
#controlla se mancano meno di 15 secondi
|
|
|
- timeleft = float(status['duration']) - float(status['elapsed'])
|
|
|
+ #timeleft = float(status['duration']) - float(status['elapsed']) #new mpd
|
|
|
+ timeleft = float(client.currentsong()['time']) - float(status['elapsed']) #old mpd
|
|
|
+
|
|
|
+
|
|
|
if timeleft <= looptime + synctime:
|
|
|
time.sleep(max(timeleft - synctime, 0))
|
|
|
print ("Mancano %d secondi" % (synctime))
|
|
@@ -488,16 +523,18 @@ def mpdsync(client, listlen):
|
|
|
print("Rigenero la playlist")
|
|
|
mpdclean(client)
|
|
|
|
|
|
- #cancella tutto tranne la prima
|
|
|
- for x in client.playlistinfo()[1:]:
|
|
|
- client.delete(1)
|
|
|
- #e rifa la playlist
|
|
|
+
|
|
|
plt = listtot(listlen)
|
|
|
+ #copia i file
|
|
|
for f in plt:
|
|
|
print(f[0])
|
|
|
copyfile(f[0], "mpd/" + f[0])
|
|
|
client.rescan()
|
|
|
+ #cancella tutto tranne la prima
|
|
|
+ for x in client.playlistinfo()[1:]:
|
|
|
+ client.delete(1)
|
|
|
time.sleep(0.5)
|
|
|
+ #e rifa la playlist
|
|
|
for f in plt:
|
|
|
client.add(f[0])
|
|
|
|