Skip to content

Commit

Permalink
Now downloads won't fail in middle and will continue with the next file
Browse files Browse the repository at this point in the history
in the list.

Download audio file by directly from first youtube search result,
so to get predictable results use this only when you are sure about you search.
  • Loading branch information
pr0PM committed Dec 13, 2020
1 parent 269c18e commit 930cfd9
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 40 deletions.
102 changes: 65 additions & 37 deletions mymusic_dl/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@
from mymusic_dl.functions import get_youtube_url
from mymusic_dl.functions import download_audio

"""This module verifies the user input before proceeding further.
"""This block of code verifies the user input before proceeding further.
"""

guide = """
To download a playlist, try
$ mymusic_dl https://open.spitify.com/playlist/18EuC3nWTRg10DD35jfZkk
To search a song and download the first result from youtube, try
$ mymusic_dl -s "ncs top 10"
"""

error = """
Expand All @@ -35,60 +38,85 @@ def input_validator(cliArgs):
prGreen(guide)
return 0

if not "open.spotify.com" in cliArgs[1] or not "https://" in cliArgs[1]:
prRed(error)
prGreen(guide)
return 0
# if not "open.spotify.com" in cliArgs[1] or not "https://" in cliArgs[1]:
# prRed(error)
# prGreen(guide)
# return 0

if "playlist" in cliArgs[1]:
prGreen("^_^ playlist link detected...\n continuing to web scraping...")
return 1

elif cliArgs[1] == "-s":
prGreen("^_^ proceeding to search youtube...\n")
return 2
elif "track" in cliArgs[1] or "album" in cliArgs[1]:
prGreen("\n^_^ Support for tracks and albums Coming Soon! Your " + chr(127775)
+ "'s on GitHub will motivate me complete it faster :)\n")

+ "'s on GitHub will motivate me to ship updates faster :)\n")
return 0

else:
prRed(error)
prGreen(guide)
return 0



"""This block is the entrypoint of the program
"""

def mymusic_dl():
"""Entrypoint of the scripts"""

if(input_validator(cliArgs)):
url = cliArgs[1]
# remove the excessive parts from the link
if "?si=" in url:
url = url[:url.index("?si=")]

song_count, pl_name, track_artists_album = scrape_spotify(url)

# might replace all this with log later
prGreen("Info")
prGreen("Playlist name: {0}\nSong Count: {1}\n" .format(pl_name, song_count))
prGreen("Getting video id from youtube...")

# search the strings on yotube and store the videoID
videoID_list = [ get_youtube_url(query) for query in track_artists_album ]

if None in videoID_list:
prRed("Something went wrong")
else:
prGreen("Here is the list of videoIDs")
for i in videoID_list:
print(i)
return_val = input_validator(cliArgs)

if(return_val):
if(return_val == 1):
url = cliArgs[1]
# remove the excessive parts from the link
if "?si=" in url:
url = url[:url.index("?si=")]

song_count, pl_name, track_artists_album = scrape_spotify(url)

# might replace all this with log later
prGreen("Info")
prGreen("Playlist name: {0}\nSong Count: {1}\n" .format(pl_name, song_count))
prGreen("Getting video id from youtube...")

# search the strings on yotube and store the videoID
videoID_list = [ get_youtube_url(query) for query in track_artists_album ]

if None in videoID_list:
prRed("Something went wrong")
else:
prGreen("Here is the list of videoIDs")
for i in videoID_list:
print(i)

prRed("Starting Downloads . . .")

for videoID in videoID_list:

# trying error handling here
try:
download_audio(videoID)
prGreen("Download +1 done")
except Exception: # ya ik it's bad and stuff
pass

elif(return_val == 2):
query = cliArgs[2]
# get url
vid_id = get_youtube_url(query)
# start download retry 3 times if failed
c = 3
while c:
try:
download_audio(vid_id)
prGreen("Done :)")
except Exception as e:
print(e, "\n Due to error will try", c-1, "times more")
c -= 1
break

prRed("Starting Downloads . . .")

for videoID in videoID_list:
download_audio(videoID)
prGreen("Download +1 done")

if __name__ == '__main__':
mymusic_dl()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

setuptools.setup(
name='mymusic_dl',
version='0.0.1a7',
version='0.0.1b0',
python_requires='>=3',
install_requires=requirements,
author='Prateek Mishra',
Expand Down
2 changes: 0 additions & 2 deletions tests/test_all.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"""One for all test file, as of now
"""

import spoti_yt

0 comments on commit 930cfd9

Please sign in to comment.