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

View File

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

View File

@ -59,6 +59,8 @@ class YouTube(callbacks.Plugin):
def dosearch(self, query): def dosearch(self, query):
url = None url = None
k = 0
while k < 3 and not url:
try: try:
searchurl = "https://www.google.com/search?&q={0} site:youtube.com".format(query) searchurl = "https://www.google.com/search?&q={0} site:youtube.com".format(query)
ua = UserAgent() ua = UserAgent()
@ -70,12 +72,9 @@ class YouTube(callbacks.Plugin):
if 'watch?v=' in elements[i]['href']: if 'watch?v=' in elements[i]['href']:
url = elements[i]['href'] url = elements[i]['href']
break break
k += 1
except Exception: except Exception:
try: continue
self.dosearch(query)
except Exception:
return None
else:
return url return url
def get_video_id_from_url(self, url): def get_video_id_from_url(self, url):