Skip to content

Commit

Permalink
Fix #1044
Browse files Browse the repository at this point in the history
Updates the expected return value of the YTM query.
  • Loading branch information
pithuene committed Jan 5, 2021
1 parent b7a517b commit 4d25594
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 41 deletions.
8 changes: 7 additions & 1 deletion spotdl/download/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,11 @@ async def download_song(self, songObj: SongObj) -> None:
youtubeHandler = YouTube(songObj.get_youtube_link())

trackAudioStream = youtubeHandler.streams.get_audio_only()
if not trackAudioStream:
print(f"Unable to get audio stream for \"{songObj.get_song_name()}\" "
f"by \"{songObj.get_contributing_artists()[0]}\" "
f"from video \"{songObj.get_youtube_link()}\"")
return None

downloadedFilePath = await self._download_from_youtube(convertedFileName, tempFolder,
trackAudioStream)
Expand Down Expand Up @@ -327,7 +332,8 @@ def _perform_audio_download(self, convertedFileName, tempFolder, trackAudioStrea
# !
# ! None is again used as a convenient exit
fileName = join(tempFolder, convertedFileName) + '.mp4'
remove(fileName)
if exists(fileName):
remove(fileName)
return None

async def _pool_download(self, song_obj: SongObj):
Expand Down
3 changes: 2 additions & 1 deletion spotdl/download/progressHandlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,8 @@ def backup_to_disk(self):
# remove tracking file if no songs left in queue
#! we use 'return None' as a convenient exit point
if len(self.songObjList) == 0:
remove(self.saveFile)
if self.saveFile:
remove(self.saveFile)
return None


Expand Down
55 changes: 19 additions & 36 deletions spotdl/search/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,39 +219,24 @@ def __query_and_simplify(searchTerm: str, apiKey: str = ytmApiKey) -> List[dict]
for result in resultBlocks:

# Blindly gather available details
availableDetails = []
availableDetails = {}

availableDetails['name'] = result[0]['musicResponsiveListItemFlexColumnRenderer']['text']['runs'][0]['text']
remainingDetailContainer = result[1]['musicResponsiveListItemFlexColumnRenderer']['text']['runs']
availableDetails['type'] = remainingDetailContainer[0]['text']


# Filterout dummies here itself
#! 'musicResponsiveListItmeFlexColumnRenderer' should have more that one
#! sub-block, if not its a dummy, why does the YTM response contain dummies?
#! I have no clue. We skip these.

#! Remember that we appended the linkBlock to result, treating that like the
#! other constituents of a result block will lead to errors, hence the 'in
#! result[:-1]'
for detail in result[:-1]:
if len(detail['musicResponsiveListItemFlexColumnRenderer']) < 2:
continue
# Filterout non-Song/Video results and incomplete results here itself
if availableDetails['type'] in ['Song', 'Video']:

#! if not a dummy, collect all available details
availableDetails.append(
detail['musicResponsiveListItemFlexColumnRenderer'] \
['text'] \
['runs'][0] \
['text']
)

if availableDetails['type'] == 'Song':
availableDetails['artist'] = remainingDetailContainer[2]['text']
availableDetails['album'] = remainingDetailContainer[4]['text']

availableDetails['time'] = remainingDetailContainer[6]['text']

# Filterout non-Song/Video results and incomplete results here itself
#! From what we know about detail order, note that [1] - indicate result type
if availableDetails[1] in ['Song', 'Video'] and len(availableDetails) == 5:

#! skip if result is in hours instead of minuts (no song is that long)
if len(availableDetails[4].split(':')) != 2:
continue
if len(availableDetails['time'].split(':')) != 2:
continue



Expand All @@ -276,7 +261,7 @@ def __query_and_simplify(searchTerm: str, apiKey: str = ytmApiKey) -> List[dict]


# convert length into seconds
minStr, secStr = availableDetails[4].split(':')
minStr, secStr = availableDetails['time'].split(':')

#! handle leading zeroes (eg. 01, 09, etc...), they cause eval errors, there
#! are a few oddball tracks that are only a few seconds long
Expand All @@ -291,13 +276,12 @@ def __query_and_simplify(searchTerm: str, apiKey: str = ytmApiKey) -> List[dict]


# format relevant details
if availableDetails[1] == 'Song':

if availableDetails['type'] == 'Song':
formattedDetails = {
'name' : availableDetails[0],
'name' : availableDetails['name'],
'type' : 'song',
'artist' : availableDetails[2],
'album' : availableDetails[3],
'artist' : availableDetails['artist'],
'album' : availableDetails['album'],
'length' : time,
'link' : resultLink,
'position' : resultPosition
Expand All @@ -306,10 +290,10 @@ def __query_and_simplify(searchTerm: str, apiKey: str = ytmApiKey) -> List[dict]
if formattedDetails not in simplifiedResults:
simplifiedResults.append(formattedDetails)

elif availableDetails[1] == 'Video':
elif availableDetails['type'] == 'Video':

formattedDetails = {
'name' : availableDetails[0],
'name' : availableDetails['name'],
'type' : 'video',
'length' : time,
'link' : resultLink,
Expand Down Expand Up @@ -366,7 +350,6 @@ def search_and_order_ytm_results(songName: str, songArtists: List[str],
results = __query_and_simplify(songSearchStr)



# Assign an overall avg match value to each result
linksWithMatchValue = {}

Expand Down
4 changes: 1 addition & 3 deletions spotdl/search/songObj.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ def from_url(cls, spotifyURL: str):
duration
)

youtubeLink = youtubeLink

return cls(
rawTrackMeta, rawAlbumMeta,
rawArtistMeta, youtubeLink
Expand Down Expand Up @@ -223,4 +221,4 @@ def get_data_dump(self) -> dict:
'rawTrackMeta' : self.__rawTrackMeta,
'rawAlbumMeta' : self.__rawAlbumMeta,
'rawArtistMeta': self.__rawArtistMeta
}
}

0 comments on commit 4d25594

Please sign in to comment.