|
@@ -15,7 +15,7 @@ import subprocess
|
|
|
import random
|
|
|
import configparser
|
|
|
import hashlib
|
|
|
-import urllib
|
|
|
+from urllib.parse import urlparse, parse_qs
|
|
|
|
|
|
from musicpd import MPDClient
|
|
|
|
|
@@ -60,7 +60,27 @@ MASTODON_URL = playlistaloconf.get('Mastodon_url', MASTODON_URL)
|
|
|
|
|
|
|
|
|
|
|
|
-
|
|
|
+def video_id(value):
|
|
|
+ """
|
|
|
+ Examples:
|
|
|
+ - http://youtu.be/SA2iWivDJiE
|
|
|
+ - http://www.youtube.com/watch?v=_oPAwA_Udwc&feature=feedu
|
|
|
+ - http://www.youtube.com/embed/SA2iWivDJiE
|
|
|
+ - http://www.youtube.com/v/SA2iWivDJiE?version=3&hl=en_US
|
|
|
+ """
|
|
|
+ query = urlparse(value)
|
|
|
+ if query.hostname == 'youtu.be':
|
|
|
+ return query.path[1:]
|
|
|
+ if query.hostname in ('www.youtube.com', 'm.youtube.com', 'youtube.com'):
|
|
|
+ if query.path == '/watch':
|
|
|
+ p = parse_qs(query.query)
|
|
|
+ return p['v'][0]
|
|
|
+ if query.path[:7] == '/embed/':
|
|
|
+ return query.path.split('/')[2]
|
|
|
+ if query.path[:3] == '/v/':
|
|
|
+ return query.path.split('/')[2]
|
|
|
+ # fail?
|
|
|
+ return None
|
|
|
|
|
|
def addurl(url, user = "-unknown-"):
|
|
|
#print ('--- Inizio ---')
|
|
@@ -83,36 +103,35 @@ def addurl(url, user = "-unknown-"):
|
|
|
print ('--- URL malformato ---')
|
|
|
return ("Err: url non valido")
|
|
|
|
|
|
- # #trova l'id dall'url
|
|
|
- # u = urllib.parse.urlparse(url)
|
|
|
- # q = urllib.parse.parse_qs(u.query)
|
|
|
- # if q:
|
|
|
- # id=q["v"][0]
|
|
|
-
|
|
|
- # #cerca se ho gia' il json
|
|
|
- # if glob(os.path.join("cache", id + ".json")):
|
|
|
- # #legge le info
|
|
|
- # with open(os.path.join("cache", id + ".json")) as infile:
|
|
|
- # title = __normalizetext(json.load(infile)['title'])
|
|
|
-
|
|
|
- with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
|
|
- try:
|
|
|
- meta = ydl.extract_info(url, download = False)
|
|
|
- except youtube_dl.DownloadError as detail:
|
|
|
- print ('--- Errore video non disponibile ---')
|
|
|
- print(str(detail))
|
|
|
- return ("Err: " + str(detail))
|
|
|
-
|
|
|
- id = meta.get('id').strip()
|
|
|
- title = __normalizetext(meta.get('title'))
|
|
|
+ #trova l'id dall'url
|
|
|
+ id = video_id(url)
|
|
|
+ if id:
|
|
|
+ #cerca se ho gia' il json
|
|
|
+ if not glob(os.path.join("cache", id + ".json")):
|
|
|
+ id = None
|
|
|
+ else:
|
|
|
+ #legge le info
|
|
|
+ with open(os.path.join("cache", id + ".json")) as infile:
|
|
|
+ title = __normalizetext(json.load(infile)['title'])
|
|
|
+
|
|
|
+ if not id:
|
|
|
+ with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
|
|
+ try:
|
|
|
+ meta = ydl.extract_info(url, download = False)
|
|
|
+ except youtube_dl.DownloadError as detail:
|
|
|
+ print ('--- Errore video non disponibile ---')
|
|
|
+ print(str(detail))
|
|
|
+ return ("Err: " + str(detail))
|
|
|
+
|
|
|
+ id = meta.get('id').strip()
|
|
|
+ title = __normalizetext(meta.get('title'))
|
|
|
+ #scrivo il json
|
|
|
+ with open(os.path.join("cache", id + ".json"), 'w') as outfile:
|
|
|
+ json.dump(meta, outfile, indent=4)
|
|
|
|
|
|
print ('id: %s' %(id))
|
|
|
print ('title: %s' %(title))
|
|
|
|
|
|
- #scrivo il json
|
|
|
- with open(os.path.join("cache", id + ".json"), 'w') as outfile:
|
|
|
- json.dump(meta, outfile, indent=4)
|
|
|
-
|
|
|
#ho letto le info, ora controllo se il file esiste altrimenti lo scarico
|
|
|
#miglioria: controllare se upload_date e' uguale a quella del json gia' esistente
|
|
|
filetemp = os.path.join("cache", id + ".m4a")
|
|
@@ -160,8 +179,9 @@ def add(filetemp, user = "-unknown-", title = None, id = None):
|
|
|
else:
|
|
|
print ('--- Converto ---')
|
|
|
print (fileout)
|
|
|
-
|
|
|
subprocess.call([scriptpath + "/trimaudio.sh", filetemp, fileout])
|
|
|
+ print ('--- Fine ---')
|
|
|
+
|
|
|
if not os.path.isfile(fileout):
|
|
|
return("Err: file non convertito")
|
|
|
|
|
@@ -172,10 +192,7 @@ def add(filetemp, user = "-unknown-", title = None, id = None):
|
|
|
|
|
|
#cerca la posizione del pezzo appena inserito
|
|
|
pos = getposition(fileout)
|
|
|
-
|
|
|
- print ('--- Fine ---')
|
|
|
- print ("")
|
|
|
-
|
|
|
+
|
|
|
return ("OK: %s [%s] aggiunto alla playlist in posizione #%s" %(title, id, pos))
|
|
|
|
|
|
|
|
@@ -402,7 +419,7 @@ def listtot(res = sys.maxsize):
|
|
|
return plt
|
|
|
|
|
|
def getlastannounce():
|
|
|
- announcepos = ANNOUNCEREPEAT
|
|
|
+ announcepos = ANNOUNCEREPEAT - 1
|
|
|
try:
|
|
|
with open("announce/last","r") as f:
|
|
|
announcepos=int(f.readline().rstrip())
|
|
@@ -425,7 +442,7 @@ def consume(song):
|
|
|
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(ANNOUNCEREPEAT)
|
|
|
+ setlastannounce(1)
|
|
|
|
|
|
elif song.split("/")[0] == "fallback":
|
|
|
fname = time.strftime("%Y%m%d%H%M%S") + "|" + "|".join(os.path.basename(song).split("|")[1:])
|
|
@@ -483,7 +500,7 @@ def plaympd():
|
|
|
#aggiunge l'annuncio iniziale
|
|
|
#addstartannounce()
|
|
|
#resetta il lastannounce
|
|
|
- setlastannounce(ANNOUNCEREPEAT)
|
|
|
+ setlastannounce(ANNOUNCEREPEAT - 1)
|
|
|
|
|
|
#riempe la playlist
|
|
|
plt = listtot(listlen)
|
|
@@ -580,10 +597,11 @@ def mpdadd(client, listlen):
|
|
|
i = plt.index(f) + 1
|
|
|
#print(f[0] +" - "+ playlist[i]['file'])
|
|
|
if f[0] != playlist[i]['file']:
|
|
|
+ i = i - 1
|
|
|
break
|
|
|
else:
|
|
|
print("Mantengo " + f[0])
|
|
|
-
|
|
|
+ i = i + 1
|
|
|
#print (i)
|
|
|
for x in client.playlistinfo()[i:]:
|
|
|
print("Cancello " + x['file'])
|