diff --git a/download_server.py b/download_server.py index e31dd507ee999165bf5edf1cb6f4fc32d9180540..acbccb6b329924dad7e784c9f72de679d0970f43 100644 --- a/download_server.py +++ b/download_server.py @@ -1,4 +1,4 @@ -# -*- coding: utf-8 -*- +# coding: latin-1 import os from time import time @@ -10,6 +10,60 @@ from credentials import KEY #commande test : "yt-dlp -f 'ba' -x --audio-format mp3 https://www.youtube.com/watch?v=zA2YBT9z6pA -o '%(id)s.mp3' " + +caracteres = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_=+\';,:!?$%&#~<> " +remplacements = {"(":"-" , ")": "-" , "\"" : "\'","�":"e","�":"a","�":"e","�":"u","�":"e","�":"a","�":"o","�":"i","�":"u","�":"U","/":" "} + +def replace(caract,remplacements) : + if caract in remplacements.keys() : + return remplacements[caract] + else : + return caract + +#Supprimer tous les caracteres interdits +def garde(chn,caracteres): + print("filtrage de : "+chn) + res = ("".join([x for x in chn if x in caracteres]) ) + return res + +def remplacer(chn,remplacements): + res = ("".join([replace(x,remplacements) for x in chn])) + print("type(res) : "+ str(type(res)) ) + return res + + + +def addplaylist(playlistid): + os.chdir("/mnt/usb/bleezrdata") + chn = 'https://www.googleapis.com/youtube/v3/playlists?part=snippet&id='+playlistid+'&key='+KEY + dico = get(chn).json() + playlistname = dico["items"][0]["snippet"]["title"] + + + with open("playlistinfo.json",'r') as f : + data = load(f) + + if not playlistid in [x['id'] for x in data] : + data.append({'id':playlistid,'name':playlistname}) + + + with open("playlistinfo.json",'w') as f : + f.write(dumps(data)) + + with open("playlists.json",'r') as f : + data = load(f) + + if not playlistid in data: + data.append(playlistid) + + + with open("playlists.json",'w') as f : + f.write(dumps(data)) + + + +addplaylist("PL8V2Ld0Is1-kUaNtXmvYGtYoyZ0O2aqDt") + os.chdir("/mnt/usb/bleezrdata") t = time() @@ -27,7 +81,8 @@ while True : for playlist in data : if playlist not in os.listdir("musics"): - os.chdir("musics") + print("La playlist " + playlist + " n\'est pas dans le r�pertoire" ) + os.chdir("musics") os.mkdir(playlist) os.chdir(playlist) @@ -40,17 +95,20 @@ while True : os.chdir("../..") - #A supprimer lorsque tout le code aura été fait proprement + #A supprimer lorsque tout le code aura �t� fait proprement if "seen_names.json" not in os.listdir("musics/"+playlist): - with open("musics/"+playlist+"/seen_names.json",'w') as f : + print("seen_names.json n\'est pas dans le r�pertoire de la playlist") + with open("musics/"+playlist+"/seen_names.json",'w') as f : f.write("[]") os.chdir("musics/"+playlist) chn = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId='+playlist+'&key='+KEY + print(chn) requete = get(chn) dico = requete.json() all_videos = [x['snippet']['resourceId']['videoId'] for x in dico['items']] all_videos_names = [x['snippet']['title'] for x in dico['items']] + #print("longueur de all_videos : {}".format(len(all_videos))) with open('seen.json','r') as f2 : seen_videos = load(f2) with open('seen_names.json','r') as f3 : @@ -60,12 +118,17 @@ while True : i = 0 while i < len(all_videos) and all_videos[i] not in seen_videos : vid = all_videos[i] - vid_name = all_videos_names[i] + vid_name = garde(remplacer(all_videos_names[i],remplacements),caracteres) print(vid) - command = "yt-dlp -f 'ba' -x --audio-format mp3 https://www.youtube.com/watch?v={0} -o '%(title)s.mp3' ".format(vid) + print(vid_name) + #command = "yt-dlp -f 'ba' -x --audio-format mp3 https://www.youtube.com/watch?v={0} -o '%(title)s.mp3' ".format(vid) + command = "yt-dlp -f 'ba' -x --audio-format mp3 https://www.youtube.com/watch?v={0} -o \"{1}.mp3\"".format(vid,vid_name) + print(command) os.system(command) - seen_videos.append(vid) - seen_videos_names.append(vid_name) + + if vid_name+".mp3" in os.listdir(".") : + seen_videos.append(vid) + seen_videos_names.append(vid_name) i += 1 os.chdir("../") @@ -77,9 +140,7 @@ while True : os.chdir("../") os.chdir("../") - - - + """ playlist = "PL8V2Ld0Is1-kWGtqHFOJiwwnU1lWbpC1r" chn = 'https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&maxResults=25&playlistId='+playlist+'&key='+KEY @@ -89,4 +150,5 @@ requete = get(chn) dico = requete.json() l = dico['items'] print([x['snippet']['resourceId']['videoId'] for x in l]) -""" \ No newline at end of file +""" +