diff --git a/.gitignore b/.gitignore index a71f275..34bd0ba 100644 --- a/.gitignore +++ b/.gitignore @@ -133,3 +133,6 @@ spdconfig.dat log.txt .cache +ffprobe.exe +ffplay.exe +ffmpeg.exe diff --git a/spotidownload lite nogui.py b/spotidownload lite nogui.py new file mode 100644 index 0000000..8230b03 --- /dev/null +++ b/spotidownload lite nogui.py @@ -0,0 +1,106 @@ +from spotipy import Spotify +from spotipy.oauth2 import SpotifyClientCredentials +from pytube import YouTube,Search +from ytmusicapi import YTMusic +from os import remove,getcwd +from os import path as ospath +from mutagen.mp4 import MP4, MP4Cover +from urllib import request +from pydub import AudioSegment + +ytm=YTMusic() + +client_credentials_manager = SpotifyClientCredentials( #Client credentials for spotify python api + client_id="", + client_secret="", +) +sp = Spotify(client_credentials_manager=client_credentials_manager) + +def remove_sus_characters(name: str): + converted = "".join( + i + for i in name + if i + not in ("/", "\\", "?", "%", "*", ":", "|", '"', "<", ">", ".", ",", ";", "=") + ) + return converted + +def searchytm(song,query): + vid_id=ytm.search(query) + try: + for i in vid_id:#to go thru list of returned results and check for result with matching artist + spduration=int(song['duration_ms']/1000) + if (i["duration_seconds"] in range(spduration-5,spduration+5)):#atleast one artist should be common to both and name should match + vid_url = "https://youtu.be/" + i["videoId"] + vid = YouTube(vid_url) + return vid + except Exception as e: + return None + +def m4atagger(webm, m4a, song, path): + convert = AudioSegment.from_file(webm) + convert.export(m4a, format="mp4", bitrate='192k') + remove(webm) + icon_url= '' + imax=0 + for i in song["album"]["images"]: + if imax100 + playlist = sp.next(playlist) + tracks.extend(playlist["items"]) + + name = sp.playlist(link)["name"] + print('Downloading playlist {} with {} songs'.format(name,len(tracks))) #show info to user + + for k in tracks: + try: + song = k["track"] + except KeyError: # Keyerror happens when albums are being download so this try except loop is necessary do not remove + song = k + + isrc_code = str(song["external_ids"]["isrc"].replace("-", "")) + vid=searchytm(song,isrc_code) + + if vid==None:#if isrc search doesnt give correct link + query=song["artists"][0]["name"] + " " + song["name"] + vid=searchytm(song,query) + if vid==None: + query=song['name'] + vid=searchytm(song,query) + path=ospath.join(getcwd(),name) + download_name = str(remove_sus_characters(song["artists"][0]["name"] + "-" + song["name"])) + download_path=ospath.join(path,download_name) + vid.streams.get_by_itag(251).download(path,download_name+'.webm') + m4atagger(download_path+'.webm',download_path+'.m4a',song,path) \ No newline at end of file