YouTube/IMDb/Lyrics: remove retries

This commit is contained in:
oddluck 2020-02-26 20:48:54 +00:00
parent 5f2625695f
commit abfbb3937f
3 changed files with 44 additions and 49 deletions

View File

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

View File

@ -33,6 +33,7 @@ import supybot.plugins as plugins
import supybot.ircutils as ircutils
import supybot.callbacks as callbacks
import supybot.ircmsgs as ircmsgs
import supybot.log as log
from bs4 import BeautifulSoup
import requests
import re
@ -53,21 +54,20 @@ class Lyrics(callbacks.Plugin):
threaded = True
def dosearch(self, lyric):
url = None
i = 0
while i < 3 and not url:
try:
searchurl = "https://www.google.com/search?&q={0} site:lyrics.fandom.com/wiki/".format(lyric)
ua = UserAgent()
header = {'User-Agent':str(ua.random)}
data = requests.get(searchurl, headers=header, timeout=10)
soup = BeautifulSoup(data.text)
elements = soup.select('.r a')
title = soup.find("h3").getText().replace(":", " - ").split('|')[0]
url = urljoin(elements[0]['href'], urlparse(url).path)
i += 1
except Exception:
continue
try:
url = None
title = None
searchurl = "https://www.google.com/search?&q={0} site:lyrics.fandom.com/wiki/".format(lyric)
ua = UserAgent()
header = {'User-Agent':str(ua.random)}
data = requests.get(searchurl, headers=header, timeout=10)
data.raise_for_status()
soup = BeautifulSoup(data.text)
elements = soup.select('.r a')
title = soup.find("h3").getText().replace(":", " - ").split('|')[0]
url = urljoin(elements[0]['href'], urlparse(url).path)
except Exception:
pass
return title, url
def getlyrics(self, url):
@ -84,9 +84,8 @@ class Lyrics(callbacks.Plugin):
"""<text>
Get song lyrics from Lyrics Wiki. Search powered by Google.
"""
try:
title, url = self.dosearch(lyric)
except Exception:
title, url = self.dosearch(lyric)
if not title or not url:
irc.reply("No results found for {0}".format(lyric))
else:
try:

View File

@ -55,23 +55,21 @@ class YouTube(callbacks.Plugin):
threaded = True
def dosearch(self, query):
url = None
k = 0
while k < 3 and not url:
try:
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, timeout=10)
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:
continue
try:
url = None
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, timeout=10)
data.raise_for_status()
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:
pass
return url
def get_video_id_from_url(self, url):