def test_check_exists(): expect_check = False # prerequisites for determining filename songname = internals.generate_songname(const.args.file_format, meta_tags) global file_name file_name = internals.sanitize_title(songname) check = spotdl.check_exists(file_name, raw_song, meta_tags) assert check == expect_check
def test_check_track_exists_before_download(tmpdir): expect_check = False const.args.folder = str(tmpdir) # prerequisites for determining filename songname = internals.generate_songname(const.args.file_format, meta_tags) global file_name file_name = internals.sanitize_title(songname) check = spotdl.check_exists(file_name, raw_song, meta_tags) assert check == expect_check
def scrape(self, tries_remaining=5): """ Search and scrape YouTube to return a list of matching videos. """ # prevents an infinite loop but allows for a few retries if tries_remaining == 0: log.debug('No tries left. I quit.') return if self.meta_tags is None: song = self.raw_song search_url = generate_search_url(song) else: song = internals.generate_songname(const.args.file_format, self.meta_tags) search_url = generate_search_url(song) log.debug('Opening URL: {0}'.format(search_url)) item = urllib.request.urlopen(search_url).read() items_parse = BeautifulSoup(item, "html.parser") videos = [] for x in items_parse.find_all( 'div', {'class': 'yt-lockup-dismissable yt-uix-tile'}): if not is_video(x): continue y = x.find('div', class_='yt-lockup-content') link = y.find('a')['href'][-11:] title = y.find('a')['title'] try: videotime = x.find('span', class_="video-time").get_text() except AttributeError: log.debug( 'Could not find video duration on YouTube, retrying..') return generate_youtube_url(self.raw_song, self.meta_tags, tries_remaining - 1) youtubedetails = { 'link': link, 'title': title, 'videotime': videotime, 'seconds': internals.get_sec(videotime) } videos.append(youtubedetails) if self.meta_tags is None: break return self._best_match(videos)
def test_without_spaces(self): expect_title = 'David_André_Østby_-_Intro' const.args.no_spaces = True title = internals.generate_songname(const.args.file_format, meta_tags) assert title == expect_title
def test_with_spaces(self): expect_title = 'David André Østby - Intro' title = internals.generate_songname(const.args.file_format, meta_tags) assert title == expect_title
def grab_single(raw_song, number=None): """ Logic behind downloading a song. """ if internals.is_youtube(raw_song): log.debug('Input song is a YouTube URL') content = youtube_tools.go_pafy(raw_song, meta_tags=None) raw_song = slugify(content.title).replace('-', ' ') meta_tags = spotify_tools.generate_metadata(raw_song) else: meta_tags = spotify_tools.generate_metadata(raw_song) content = youtube_tools.go_pafy(raw_song, meta_tags) if content is None: log.debug('Found no matching video') return if const.args.download_only_metadata and meta_tags is None: log.info('Found no metadata. Skipping the download') return # "[number]. [artist] - [song]" if downloading from list # otherwise "[artist] - [song]" youtube_title = youtube_tools.get_youtube_title(content, number) log.info('{} ({})'.format(youtube_title, content.watchv_url)) # generate file name of the song to download songname = content.title if meta_tags is not None: refined_songname = internals.generate_songname(meta_tags) log.debug('Refining songname from "{0}" to "{1}"'.format(songname, refined_songname)) if not refined_songname == ' - ': songname = refined_songname else: log.warning('Could not find metadata') if const.args.dry_run: return file_name = internals.sanitize_title(songname) if not check_exists(file_name, raw_song, meta_tags): if youtube_tools.download_song(file_name, content): input_song = file_name + const.args.input_ext output_song = file_name + const.args.output_ext print('') try: convert.song(input_song, output_song, const.args.folder, avconv=const.args.avconv) except FileNotFoundError: encoder = 'avconv' if const.args.avconv else 'ffmpeg' log.warning('Could not find {0}, skipping conversion'.format(encoder)) const.args.output_ext = const.args.input_ext output_song = file_name + const.args.output_ext if not const.args.input_ext == const.args.output_ext: os.remove(os.path.join(const.args.folder, input_song)) if not const.args.no_metadata and meta_tags is not None: metadata.embed(os.path.join(const.args.folder, output_song), meta_tags) if const.args.preserve_spaces and "_" in output_song: song_path = os.path.join(const.args.folder, output_song.replace('_', ' ')) os.rename(os.path.join(const.args.folder, output_song), song_path) else: log.error('No audio streams available')
def generate_youtube_url(raw_song, meta_tags, tries_remaining=5): """ Search for the song on YouTube and generate a URL to its video. """ # prevents an infinite loop but allows for a few retries if tries_remaining == 0: log.debug('No tries left. I quit.') return query = {'part': 'snippet', 'maxResults': 50, 'type': 'video'} if const.args.music_videos_only: query['videoCategoryId'] = '10' if not meta_tags: song = raw_song query['q'] = song else: song = internals.generate_songname(meta_tags) query['q'] = song log.debug('query: {0}'.format(query)) data = pafy.call_gdata('search', query) query_results = { 'part': 'contentDetails,snippet,statistics', 'maxResults': 50, 'id': ','.join(i['id']['videoId'] for i in data['items']) } log.debug('query_results: {0}'.format(query_results)) vdata = pafy.call_gdata('videos', query_results) videos = [] for x in vdata['items']: duration_s = pafy.playlist.parseISO8591( x['contentDetails']['duration']) youtubedetails = { 'link': x['id'], 'title': x['snippet']['title'], 'videotime': internals.videotime_from_seconds(duration_s), 'seconds': duration_s } videos.append(youtubedetails) if not meta_tags: break if not videos: return None if const.args.manual: log.info(song) log.info('0. Skip downloading this song.\n') # fetch all video links on first page on YouTube for i, v in enumerate(videos): log.info(u'{0}. {1} {2} {3}'.format( i + 1, v['title'], v['videotime'], "http://youtube.com/watch?v=" + v['link'])) # let user select the song to download result = internals.input_link(videos) if not result: return None else: if not meta_tags: # if the metadata could not be acquired, take the first result # from Youtube because the proper song length is unknown result = videos[0] log.debug( 'Since no metadata found on Spotify, going with the first result' ) else: # filter out videos that do not have a similar length to the Spotify song duration_tolerance = 10 max_duration_tolerance = 20 possible_videos_by_duration = list() ''' start with a reasonable duration_tolerance, and increment duration_tolerance until one of the Youtube results falls within the correct duration or the duration_tolerance has reached the max_duration_tolerance ''' while len(possible_videos_by_duration) == 0: possible_videos_by_duration = list( filter( lambda x: abs(x['seconds'] - (int(meta_tags[ 'duration_ms']) / 1000)) <= duration_tolerance, videos)) duration_tolerance += 1 if duration_tolerance > max_duration_tolerance: log.error("{0} by {1} was not found.\n".format( meta_tags['name'], meta_tags['artists'][0]['name'])) return None result = possible_videos_by_duration[0] if result: url = "http://youtube.com/watch?v=" + result['link'] else: url = None return url
def test_spotify_title(): expect_title = 'David André Østby - Intro' global meta_tags meta_tags = spotify_tools.generate_metadata(raw_song) title = internals.generate_songname(const.args.file_format, meta_tags) assert title == expect_title