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

View File

@ -55,23 +55,21 @@ class YouTube(callbacks.Plugin):
threaded = True threaded = True
def dosearch(self, query): def dosearch(self, query):
url = None
k = 0
while k < 3 and not url:
try: try:
url = None
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()
header = {'User-Agent':str(ua.random)} header = {'User-Agent':str(ua.random)}
data = requests.get(searchurl, headers=header, timeout=10) data = requests.get(searchurl, headers=header, timeout=10)
data.raise_for_status()
soup = BeautifulSoup(data.text) soup = BeautifulSoup(data.text)
elements = soup.select('.r a') elements = soup.select('.r a')
for i in range(len(elements)): for i in range(len(elements)):
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:
continue pass
return url return url
def get_video_id_from_url(self, url): def get_video_id_from_url(self, url):