Skip to content

Commit

Permalink
Sleeping only for missing songs, edit error for Album not found
Browse files Browse the repository at this point in the history
  • Loading branch information
bloedboemmel committed Nov 13, 2022
1 parent a771c59 commit fb8046c
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
37 changes: 26 additions & 11 deletions TIDALDL-PY/tidal_dl/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,20 @@ def downloadAlbumInfo(album, tracks):
aigpy.file.write(path, infos, "w+")


def downloadVideo(video: Video, album: Album = None, playlist: Playlist = None):
def downloadVideo(video: Video, path, stream, album: Album = None, playlist: Playlist = None):
try:
stream = TIDAL_API.getVideoStreamUrl(video.id, SETTINGS.videoQuality)
path = getVideoPath(video, album, playlist)
if __isSkip__(path, stream.m3u8Url):
Printf.success(aigpy.path.getFileName(path) + " (skip:already exists!)")
return True, ''

Printf.video(video, stream)
logging.info("[DL Video] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.m3u8Url)

# Sleep for human behaviour
if SETTINGS.downloadDelay is not False:
sleep_time = random.randint(500, 5000) / 1000
print(f"Sleeping for {sleep_time} seconds, to mimic human behaviour and prevent too many requests error")
time.sleep(sleep_time)
m3u8content = requests.get(stream.m3u8Url).content
if m3u8content is None:
Printf.err(f"DL Video[{video.title}] getM3u8 failed.{str(e)}")
Expand All @@ -140,11 +146,8 @@ def downloadVideo(video: Video, album: Album = None, playlist: Playlist = None):
return False, str(e)


def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, partSize=1048576):
def downloadTrack(track: Track, album=None, path=None, stream=None, userProgress=None, partSize=1048576):
try:
stream = TIDAL_API.getStreamUrl(track.id, SETTINGS.audioQuality)
path = getTrackPath(track, stream, album, playlist)

if SETTINGS.showTrackInfo and not SETTINGS.multiThread:
Printf.track(track, stream)

Expand All @@ -156,6 +159,12 @@ def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, pa
Printf.success(aigpy.path.getFileName(path) + " (skip:already exists!)")
return True, ''

#Sleep for human behaviour
if SETTINGS.downloadDelay is not False:
sleep_time = random.randint(500, 5000) / 1000
print(f"Sleeping for {sleep_time} seconds, to mimic human behaviour and prevent too many requests error")
time.sleep(sleep_time)

# download
logging.info("[DL Track] name=" + aigpy.path.getFileName(path) + "\nurl=" + stream.url)

Expand Down Expand Up @@ -206,18 +215,24 @@ def __getAlbum__(item: Track):
if itemAlbum is None:
itemAlbum = __getAlbum__(item)
item.trackNumberOnPlaylist = index + 1
downloadTrack(item, itemAlbum, playlist)
stream = TIDAL_API.getStreamUrl(item.id, SETTINGS.audioQuality)
path = getTrackPath(item, stream, album, playlist)
downloadTrack(item, path=path, stream=stream, album=itemAlbum)
else:
thread_pool = ThreadPoolExecutor(max_workers=5)
for index, item in enumerate(tracks):
itemAlbum = album
if itemAlbum is None:
itemAlbum = __getAlbum__(item)
item.trackNumberOnPlaylist = index + 1
thread_pool.submit(downloadTrack, item, itemAlbum, playlist)
stream = TIDAL_API.getStreamUrl(item.id, SETTINGS.audioQuality)
path = getTrackPath(item, stream, album, playlist)
thread_pool.submit(downloadTrack, item, path=path, stream=stream, album=itemAlbum)
thread_pool.shutdown(wait=True)


def downloadVideos(videos, album: Album, playlist=None):
for item in videos:
downloadVideo(item, album, playlist)
for video in videos:
stream = TIDAL_API.getVideoStreamUrl(video.id, SETTINGS.videoQuality)
path = getVideoPath(video, album, playlist)
downloadVideo(video, path, stream, album, playlist)
14 changes: 8 additions & 6 deletions TIDALDL-PY/tidal_dl/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,6 @@ def getTrackPath(track, stream, album=None, playlist=None):
# explicit
explicit = "(Explicit)" if track.explicit else ''

# album and addyear
albumName = __fixPath__(album.title)
year = __getYear__(album.releaseDate)

# extension
extension = __getExtension__(stream)

Expand All @@ -131,8 +127,14 @@ def getTrackPath(track, stream, album=None, playlist=None):
retpath = retpath.replace(R"{ArtistsName}", artists)
retpath = retpath.replace(R"{TrackTitle}", title)
retpath = retpath.replace(R"{ExplicitFlag}", explicit)
retpath = retpath.replace(R"{AlbumYear}", year)
retpath = retpath.replace(R"{AlbumTitle}", albumName)

if album is not None:
# album and addyear
albumName = __fixPath__(album.title)
year = __getYear__(album.releaseDate)
retpath = retpath.replace(R"{AlbumYear}", year)
retpath = retpath.replace(R"{AlbumTitle}", albumName)

retpath = retpath.replace(R"{AudioQuality}", track.audioQuality)
retpath = retpath.replace(R"{DurationSeconds}", str(track.duration))
retpath = retpath.replace(R"{Duration}", __getDurationStr__(track.duration))
Expand Down
6 changes: 0 additions & 6 deletions TIDALDL-PY/tidal_dl/tidal.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@ def __get__(self, path, params={}, urlpre='https://api.tidalhifi.com/v1/'):
for index in range(0, 3):
try:
respond = requests.get(urlpre + path, headers=header, params=params)
if respond.url.find("playbackinfopostpaywall") != -1 and SETTINGS.downloadDelay is not False:
# random sleep between 0.5 and 5 seconds and print it
sleep_time = random.randint(500, 5000) / 1000
print(f"Sleeping for {sleep_time} seconds, to mimic human behaviour and prevent too many requests error")
time.sleep(sleep_time)

if respond.status_code == 429:
print('Too many requests, waiting for 20 seconds...')
# Loop countdown 20 seconds and print the remaining time
Expand Down

0 comments on commit fb8046c

Please sign in to comment.