Browse Source

Fixed plaympd

itec 3 years ago
parent
commit
d815ef2a2e
5 changed files with 162 additions and 31 deletions
  1. 2 1
      pl_list.py
  2. 9 0
      pl_playlocal.py
  3. 0 14
      pl_playloop.py
  4. 9 0
      pl_plaympd.py
  5. 142 16
      playlistalo.py

+ 2 - 1
pl_list.py

@@ -5,5 +5,6 @@ import playlistalo
 
 if __name__ == '__main__':
     pl = playlistalo.listplaylist()
-    print ('\n'.join([", ".join(x) for x in pl]))
+    #print ('\n'.join([", ".join(x) for x in pl]))
+    print ('\n'.join([x[0] for x in pl]))
     

+ 9 - 0
pl_playlocal.py

@@ -0,0 +1,9 @@
+#!/usr/bin/env python3
+#Fa partire il loop musicale
+
+import playlistalo
+
+if __name__ == '__main__':
+    playlistalo.playlocal()
+
+    

+ 0 - 14
pl_playloop.py

@@ -1,14 +0,0 @@
-#!/usr/bin/env python3
-#Fa partire il loop musicale
-
-import playlistalo
-
-if __name__ == '__main__':
-    if playlistalo.SHUFFLEUSERS:
-        playlistalo.shuffleusers()
-    if playlistalo.SHUFFLEFALLBACK:
-        playlistalo.shufflefallback()
-        
-    playlistalo.playloop()
-
-    

+ 9 - 0
pl_plaympd.py

@@ -0,0 +1,9 @@
+#!/usr/bin/env python3
+#Fa partire il loop musicale
+
+import playlistalo
+
+if __name__ == '__main__':
+    playlistalo.plaympd()
+
+    

+ 142 - 16
playlistalo.py

@@ -187,7 +187,7 @@ def listplaylist():
         #print ("LAST: " + last)
 
         #leggi i file nella cartella
-        files = [x for x in glob(udir + "/*") if not os.path.basename(x) == "last"]
+        files = [x for x in sorted(glob(udir + "/*")) if not os.path.basename(x) == "last"]
         seq = 0
         for file in files:
             bn = os.path.splitext(os.path.basename(file))[0]
@@ -214,7 +214,7 @@ def listfallback():
     pl = []
     pl2 = []
     #leggi i file nella cartella
-    files = [x for x in glob("fallback/*") if not os.path.basename(x) == "last"]
+    files = [x for x in sorted(glob("fallback/*")) if not os.path.basename(x) == "last"]
     seq = 0
     for file in files:
         bn = os.path.splitext(os.path.basename(file))[0]
@@ -232,7 +232,12 @@ def listfallback():
     return pl2
 
 
-def playloop():
+def playlocal():
+    if SHUFFLEUSERS:
+        shuffleusers()
+    if SHUFFLEFALLBACK:
+        shufflefallback()
+    
     while True:
         plt = listtot(1)
         if plt:
@@ -361,18 +366,19 @@ def listtot(res = sys.maxsize):
 
 
 def consume(song):
-    if song.split("/")[0] == "playlist":
-        os.remove(song)
-        if not [x for x in glob(os.path.dirname(song) + "/*") if not os.path.basename(x) == "last"]:
-            shutil.rmtree(os.path.dirname(song))
-        else:
-            with open(os.path.dirname(song) + "/last", "w") as f:
-                f.write(time.strftime("%Y%m%d%H%M%S"))
-    
-    elif os.path.split(song)[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)
+    if os.path.exists(song):
+        if song.split("/")[0] == "playlist":
+            os.remove(song)
+            if not [x for x in glob(os.path.dirname(song) + "/*") if not os.path.basename(x) == "last"]:
+                shutil.rmtree(os.path.dirname(song))
+            else:
+                with open(os.path.dirname(song) + "/last", "w") as f:
+                    f.write(time.strftime("%Y%m%d%H%M%S"))
+        
+        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)
 
 
 
@@ -389,6 +395,126 @@ def copyfile(source, dest):
 
 
 
+
+def plaympd():
+    client = MPDClient()
+
+    client.timeout = 10                # network timeout in seconds (floats allowed), default: None
+    client.idletimeout = None          # timeout for fetching the result of the idle command is handled seperately, default: None
+    client.connect("localhost", 6600)  # connect to localhost:6600
+    #print(client.mpd_version)
+
+    looptime = 10
+    synctime = 5
+    listlen = 5
+
+    if SHUFFLEUSERS:
+        shuffleusers()
+    if SHUFFLEFALLBACK:
+        shufflefallback()
+        
+    #stoppa e svuota
+    client.stop()
+    client.clear()
+    #cancella la cartella mpd
+    if os.path.exists("mpd"):
+        shutil.rmtree("mpd")
+
+    #aggiunge l'annuncio iniziale
+    addstartannounce()
+
+    #riempe la playlist
+    plt = listtot(listlen)
+    for f in plt:
+        print(f[0])
+        copyfile(f[0], "mpd/" + f[0])
+    client.rescan()
+    time.sleep(0.5)
+    for f in plt:
+        client.add(f[0])
+
+    #consuma il primo e fa play
+    consume(plt[0][0])
+    client.play()
+
+    while True:
+        # print("Current")
+        # print(client.currentsong())
+        # print()
+        # print("Status")
+        # print(client.status())
+        # print()
+        # print("playlist")
+        # 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']
+            consume(song)
+            mpdclean(client)
+
+        if len(client.playlistinfo()) < listlen:
+            mpdsync(client, listlen)
+
+        #controlla se mancano meno di 15 secondi
+        timeleft = float(status['duration']) - float(status['elapsed'])
+        if timeleft <= looptime + synctime:
+            time.sleep(max(timeleft - synctime, 0))
+            print ("Mancano %d secondi" % (synctime))
+            mpdsync(client, listlen)
+
+        time.sleep(looptime)
+
+def mpdclean(client):
+    #cancella le precedenti
+    for x in range(int(client.status()['song'])):
+        song = client.playlistinfo()[x]['file']
+        consume(song)
+        client.delete(0)
+        #e pulisce anche in mpd
+        if os.path.exists("mpd/" + song):
+            os.remove("mpd/" + song)
+        #se non ci sono + file cancella la cartella
+        if not glob(os.path.dirname("mpd/" + song) + "/*"):
+            shutil.rmtree(os.path.dirname("mpd/" + song))
+
+
+
+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)
+    for f in plt:
+        print(f[0])
+        copyfile(f[0], "mpd/" + f[0])
+    client.rescan()
+    time.sleep(0.5)
+    for f in plt:
+        client.add(f[0])
+
+
+
+
+
+
+
+
+
+
+
+
+
+
 if __name__ == '__main__':
     print ("This is a package, use other commands to run it")
-    addstartannounce()
+
+