Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleeping only for missing songs, edit error for Album not found #1020

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 27 additions & 6 deletions TIDALDL-PY/tidal_dl/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,19 @@ def downloadVideo(video: Video, 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 @@ -142,9 +151,11 @@ def downloadVideo(video: Video, album: Album = None, playlist: Playlist = None):

def downloadTrack(track: Track, album=None, playlist=None, userProgress=None, partSize=1048576):
try:
path = getTrackPath(track, album, playlist)
if os.path.exists(path):
Printf.success(aigpy.path.getFileName(path) + " (skip:already exists!)")
return True, ''
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 +167,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 +223,22 @@ def __getAlbum__(item: Track):
if itemAlbum is None:
itemAlbum = __getAlbum__(item)
item.trackNumberOnPlaylist = index + 1
downloadTrack(item, itemAlbum, playlist)

downloadTrack(item, playlist=playlist, 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)

thread_pool.submit(downloadTrack, item, playlist=playlist, 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:
downloadVideo(video, album, playlist)
32 changes: 14 additions & 18 deletions TIDALDL-PY/tidal_dl/paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,10 @@ def __getDurationStr__(seconds):
time_string = time_string[2:]
return time_string


def __getExtension__(stream: StreamUrl):
if '.flac' in stream.url:
return '.flac'
if '.mp4' in stream.url:
if 'ac4' in stream.codec or 'mha1' in stream.codec:
return '.mp4'
return '.m4a'
return '.m4a'

def __getExtension__(track):
if track.audioQuality == "LOSSLESS" or track.audioQuality == "HI_RES":
return ".flac"
return ".m4a"

def getAlbumPath(album):
artistName = __fixPath__(TIDAL_API.getArtistsName(album.artists))
Expand Down Expand Up @@ -92,7 +86,7 @@ def getPlaylistPath(playlist):
return f"{SETTINGS.downloadPath}/{retpath}"


def getTrackPath(track, stream, album=None, playlist=None):
def getTrackPath(track, album=None, playlist=None):
base = './'
number = str(track.trackNumber).rjust(2, '0')
if album is not None:
Expand All @@ -116,12 +110,8 @@ 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)
extension = __getExtension__(track)

retpath = SETTINGS.trackFileFormat
if retpath is None or len(retpath) <= 0:
Expand All @@ -131,8 +121,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