fixed Youtube searches

This commit is contained in:
Manuel Cortez 2020-07-07 17:03:04 -05:00
parent 10d60e3aa2
commit 01f639b809
2 changed files with 33 additions and 38 deletions

View File

@ -4,10 +4,8 @@ requests
bs4 bs4
pypubsub pypubsub
python-vlc python-vlc
google-api-python-client
youtube-dl youtube-dl
pyinstaller cx_freeze
isodate
configobj configobj
winpaths winpaths
mutagen mutagen

View File

@ -1,18 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import isodate
import youtube_dl import youtube_dl
import logging import logging
import wx import wx
import config import config
from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from update.utils import seconds_to_string from update.utils import seconds_to_string
from .import base from .import base
DEVELOPER_KEY = "AIzaSyCU_hvZJEjLlAGAnlscquKEkE8l0lVOfn0"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
log = logging.getLogger("extractors.youtube.com") log = logging.getLogger("extractors.youtube.com")
class interface(base.baseInterface): class interface(base.baseInterface):
@ -27,27 +20,30 @@ class interface(base.baseInterface):
type = "video" type = "video"
max_results = config.app["services"]["youtube"]["max_results"] max_results = config.app["services"]["youtube"]["max_results"]
log.debug("Retrieving data from Youtube...") log.debug("Retrieving data from Youtube...")
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) ydl = youtube_dl.YoutubeDL({'quiet': True, 'ignore_errors': True, 'no_warnings': True, 'logger': log, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'})
search_response = youtube.search().list(q=text, part="id,snippet", maxResults=max_results, type=type).execute() with ydl:
search_param = "ytsearch{}:{}".format(max_results, text)
result = ydl.extract_info(search_param, download=False)
self.results = [] self.results = []
ids = [] for search_result in result["entries"]:
for search_result in search_response.get("items", []): s = base.song(self)
if search_result["id"]["kind"] == "youtube#video": s.title = search_result["title"]
s = base.song(self) s.url = "https://www.youtube.com/watch?v="+search_result["id"]
s.title = search_result["snippet"]["title"] s.duration = seconds_to_string(search_result["duration"])
ids.append(search_result["id"]["videoId"]) if search_result.get("track") != None:
s.url = "https://www.youtube.com/watch?v="+search_result["id"]["videoId"] s.title = search_result["track"]
self.results.append(s) if search_result.get("album") != None:
ssr = youtube.videos().list(id=",".join(ids), part="contentDetails", maxResults=1).execute() s.album = search_result["album"]
for i in range(len(self.results)): if search_result.get("artist") != None:
self.results[i].duration = seconds_to_string(isodate.parse_duration(ssr["items"][i]["contentDetails"]["duration"]).total_seconds()) s.artist = search_result["artist"]
self.results.append(s)
log.debug("{0} results found.".format(len(self.results))) log.debug("{0} results found.".format(len(self.results)))
def search_from_url(self, url): def search_from_url(self, url):
log.debug("Getting download URL for {0}".format(url,)) log.debug("Getting download URL for {0}".format(url,))
if "playlist?list=" in url: if "playlist?list=" in url:
return self.search_from_playlist(url) return self.search_from_playlist(url)
ydl = youtube_dl.YoutubeDL({'quiet': True, 'no_warnings': True, 'logger': log, 'prefer-free-formats': True, 'format': 'bestaudio', 'outtmpl': u'%(id)s%(ext)s'}) ydl = youtube_dl.YoutubeDL({'quiet': True, 'ignore_errors': True, 'no_warnings': True, 'logger': log, 'prefer-free-formats': True, 'format': 'bestaudio', 'outtmpl': u'%(id)s%(ext)s'})
with ydl: with ydl:
result = ydl.extract_info(url, download=False) result = ydl.extract_info(url, download=False)
if 'entries' in result: if 'entries' in result:
@ -55,7 +51,7 @@ class interface(base.baseInterface):
else: else:
videos = [result] videos = [result]
for video in videos: for video in videos:
s = baseFile.song(self) s = base.song(self)
s.title = video["title"] s.title = video["title"]
s.url = video["webpage_url"] # Cannot use direct URL here cause Youtube URLS expire after a minute. s.url = video["webpage_url"] # Cannot use direct URL here cause Youtube URLS expire after a minute.
s.duration = seconds_to_string(video["duration"]) s.duration = seconds_to_string(video["duration"])
@ -66,21 +62,22 @@ class interface(base.baseInterface):
id = url.split("=")[1] id = url.split("=")[1]
max_results = 50 max_results = 50
log.debug("Retrieving data from Youtube...") log.debug("Retrieving data from Youtube...")
youtube = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) ydl = youtube_dl.YoutubeDL({'quiet': True, 'ignore_errors': True, 'no_warnings': True, 'logger': log, 'format': 'bestaudio/best', 'outtmpl': u'%(id)s%(ext)s'})
search_response = youtube.playlistItems().list(playlistId=id, part="id, status, snippet", maxResults=max_results).execute() with ydl:
result = ydl.extract_info(url, download=False)
self.results = [] self.results = []
ids = [] for search_result in result["entries"]:
for search_result in search_response.get("items", []): s = base.song(self)
if search_result["status"]["privacyStatus"] != "public": s.title = search_result["title"]
continue s.url = "https://www.youtube.com/watch?v="+search_result["id"]
s = baseFile.song(self) s.duration = seconds_to_string(search_result["duration"])
s.title = search_result["snippet"]["title"] if search_result.get("track") != None:
ids.append(search_result["snippet"]["resourceId"]["videoId"]) s.title = search_result["track"]
s.url = "https://www.youtube.com/watch?v="+search_result["snippet"]["resourceId"]["videoId"] if search_result.get("album") != None:
s.album = search_result["album"]
if search_result.get("artist") != None:
s.artist = search_result["artist"]
self.results.append(s) self.results.append(s)
ssr = youtube.videos().list(id=",".join(ids), part="contentDetails", maxResults=50).execute()
for i in range(len(self.results)):
self.results[i].duration = seconds_to_string(isodate.parse_duration(ssr["items"][i]["contentDetails"]["duration"]).total_seconds())
log.debug("{0} results found.".format(len(self.results))) log.debug("{0} results found.".format(len(self.results)))
def get_download_url(self, url): def get_download_url(self, url):