mirror of
				https://github.com/KevinMidboe/spotify-downloader.git
				synced 2025-10-29 18:00:15 +00:00 
			
		
		
		
	Switch to youtube API (#191)
* Switch to youtube API * Fix test failures * Minor fix
This commit is contained in:
		
				
					committed by
					
						 Ritiek Malhotra
						Ritiek Malhotra
					
				
			
			
				
	
			
			
			
						parent
						
							57055eb65d
						
					
				
				
					commit
					4d664956cd
				
			
							
								
								
									
										52
									
								
								spotdl.py
									
									
									
									
									
								
							
							
						
						
									
										52
									
								
								spotdl.py
									
									
									
									
									
								
							| @@ -87,35 +87,35 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5): | ||||
|         log.debug('No tries left. I quit.') | ||||
|         return | ||||
|  | ||||
|     query = {'part': 'snippet', | ||||
|              'maxResults': 50, | ||||
|              'type': 'video'} | ||||
|  | ||||
|     if args.music_videos_only: | ||||
|         query['videoCategoryId'] = '10' | ||||
|  | ||||
|     if meta_tags is None: | ||||
|         song = raw_song | ||||
|         search_url = internals.generate_search_url(song, viewsort=False) | ||||
|         query['q'] = song | ||||
|     else: | ||||
|         song = generate_songname(meta_tags) | ||||
|         search_url = internals.generate_search_url(song, viewsort=True) | ||||
|     log.debug('Opening URL: {0}'.format(search_url)) | ||||
|         query['q'] = song | ||||
|     log.debug('Query: {0}'.format(query)) | ||||
|  | ||||
|     item = urllib.request.urlopen(search_url).read() | ||||
|     items_parse = BeautifulSoup(item, "html.parser") | ||||
|     data = pafy.call_gdata('search', query) | ||||
|     query2 = {'part': 'contentDetails,snippet,statistics', | ||||
|               'maxResults': 50, | ||||
|               'id': ','.join(i['id']['videoId'] for i in data['items'])} | ||||
|     log.debug('Query2: {0}'.format(query2)) | ||||
|  | ||||
|     vdata = pafy.call_gdata('videos', query2) | ||||
|  | ||||
|     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'] | ||||
|         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(raw_song, meta_tags, tries_remaining - 1) | ||||
|  | ||||
|         youtubedetails = {'link': link, 'title': title, 'videotime': videotime, | ||||
|                           'seconds': internals.get_sec(videotime)} | ||||
|     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 meta_tags is None: | ||||
|             break | ||||
| @@ -131,7 +131,7 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5): | ||||
|         # 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"+v['link'])) | ||||
|                   "http://youtube.com/watch?v="+v['link'])) | ||||
|         # let user select the song to download | ||||
|         result = internals.input_link(videos) | ||||
|         if result is None: | ||||
| @@ -163,11 +163,11 @@ def generate_youtube_url(raw_song, meta_tags, tries_remaining=5): | ||||
|             result = possible_videos_by_duration[0] | ||||
|  | ||||
|     if result: | ||||
|         full_link = u'http://youtube.com{0}'.format(result['link']) | ||||
|         url = "http://youtube.com/watch?v=" + result['link'] | ||||
|     else: | ||||
|         full_link = None | ||||
|         url = None | ||||
|  | ||||
|     return full_link | ||||
|     return  url | ||||
|  | ||||
|  | ||||
| def go_pafy(raw_song, meta_tags=None): | ||||
|   | ||||
		Reference in New Issue
	
	Block a user