YouTube/Lyrics/IMDb: add retries

This commit is contained in:
oddluck 2020-02-25 04:48:29 +00:00
parent 022743711c
commit 009d6ceaa3
3 changed files with 46 additions and 43 deletions

View File

@ -55,19 +55,21 @@ class IMDb(callbacks.Plugin):
threaded = True threaded = True
def dosearch(self, query): def dosearch(self, query):
try: url = None
searchurl = "https://www.google.com/search?&q={0} site:imdb.com/title/".format(query) i = 0
ua = UserAgent() while i < 3 and not url:
header = {'User-Agent':str(ua.random)} try:
data = requests.get(searchurl, headers=header) searchurl = "https://www.google.com/search?&q={0} site:imdb.com/title/".format(query)
soup = BeautifulSoup(data.text) ua = UserAgent()
elements = soup.select('.r a') header = {'User-Agent':str(ua.random)}
url = elements[0]['href'] data = requests.get(searchurl, headers=header)
url = urljoin(url, urlparse(url).path) soup = BeautifulSoup(data.text)
except Exception: elements = soup.select('.r a')
return url = urljoin(elements[0]['href'], urlparse(url).path)
else: i += 1
return url except Exception:
continue
return url
def imdb(self, irc, msg, args, query): def imdb(self, irc, msg, args, query):
"""<title> """<title>

View File

@ -53,20 +53,22 @@ class Lyrics(callbacks.Plugin):
threaded = True threaded = True
def dosearch(self, lyric): def dosearch(self, lyric):
try: url = None
searchurl = "https://www.google.com/search?&q={0} site:lyrics.fandom.com/wiki/".format(lyric) i = 0
ua = UserAgent() while i < 3 and not url:
header = {'User-Agent':str(ua.random)} try:
data = requests.get(searchurl, headers=header) searchurl = "https://www.google.com/search?&q={0} site:lyrics.fandom.com/wiki/".format(lyric)
soup = BeautifulSoup(data.text) ua = UserAgent()
elements = soup.select('.r a') header = {'User-Agent':str(ua.random)}
url = elements[0]['href'] data = requests.get(searchurl, headers=header)
urljoin(url, urlparse(url).path) soup = BeautifulSoup(data.text)
title = soup.find("h3").getText().replace(":", " - ").split('|')[0] elements = soup.select('.r a')
except Exception: title = soup.find("h3").getText().replace(":", " - ").split('|')[0]
return url = urljoin(elements[0]['href'], urlparse(url).path)
else: i += 1
return title, url except Exception:
continue
return title, url
def getlyrics(self, url): def getlyrics(self, url):
try: try:

View File

@ -59,24 +59,23 @@ class YouTube(callbacks.Plugin):
def dosearch(self, query): def dosearch(self, query):
url = None url = None
try: k = 0
searchurl = "https://www.google.com/search?&q={0} site:youtube.com".format(query) while k < 3 and not url:
ua = UserAgent()
header = {'User-Agent':str(ua.random)}
data = requests.get(searchurl, headers=header)
soup = BeautifulSoup(data.text)
elements = soup.select('.r a')
for i in range(len(elements)):
if 'watch?v=' in elements[i]['href']:
url = elements[i]['href']
break
except Exception:
try: try:
self.dosearch(query) searchurl = "https://www.google.com/search?&q={0} site:youtube.com".format(query)
ua = UserAgent()
header = {'User-Agent':str(ua.random)}
data = requests.get(searchurl, headers=header)
soup = BeautifulSoup(data.text)
elements = soup.select('.r a')
for i in range(len(elements)):
if 'watch?v=' in elements[i]['href']:
url = elements[i]['href']
break
k += 1
except Exception: except Exception:
return None continue
else: return url
return url
def get_video_id_from_url(self, url): def get_video_id_from_url(self, url):
""" """