Skip to content

Commit 85fbb8c

Browse files
authored
Merge pull request CloudBotIRC#64 from flotwig/new-imdb-api
Fixing IMDb API errors
2 parents a87972e + 345021b commit 85fbb8c

2 files changed

Lines changed: 36 additions & 30 deletions

File tree

plugins/imdb.py

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import re
2-
32
import requests
43

54
from cloudbot import hook
65

7-
86
id_re = re.compile("tt\d+")
97
imdb_re = re.compile(r'(.*:)//(imdb.com|www.imdb.com)(:[0-9]+)?(.*)', re.I)
108

@@ -17,28 +15,28 @@ def imdb(text, bot):
1715
strip = text.strip()
1816

1917
if id_re.match(strip):
20-
params = {'i': strip}
18+
endpoint = 'title'
19+
params = {'id': strip}
2120
else:
22-
params = {'t': strip}
21+
endpoint = 'search'
22+
params = {'q': strip, 'limit': 1}
2323

24-
request = requests.get("http://www.omdbapi.com/", params=params, headers=headers)
24+
request = requests.get(
25+
"https://imdb-scraper.herokuapp.com/" + endpoint,
26+
params=params,
27+
headers=headers)
2528
content = request.json()
2629

27-
if content.get('Error', None) == 'Movie not found!':
28-
return 'Movie not found!'
29-
elif content['Response'] == 'True':
30-
content['URL'] = 'http://www.imdb.com/title/{}'.format(content['imdbID'])
31-
32-
out = '\x02%(Title)s\x02 (%(Year)s) (%(Genre)s): %(Plot)s'
33-
if content['Runtime'] != 'N/A':
34-
out += ' \x02%(Runtime)s\x02.'
35-
if content['imdbRating'] != 'N/A' and content['imdbVotes'] != 'N/A':
36-
out += ' \x02%(imdbRating)s/10\x02 with \x02%(imdbVotes)s\x02' \
37-
' votes.'
38-
out += ' %(URL)s'
39-
return out % content
30+
if content['success'] is False:
31+
return 'Unknown error'
32+
elif len(content['result']) == 0:
33+
return 'No movie found'
4034
else:
41-
return 'Unknown error.'
35+
result = content['result']
36+
if endpoint == 'search':
37+
result = result[0] # part of the search results, not 1 record
38+
url = 'http://www.imdb.com/title/{}'.format(result['id'])
39+
return movie_str(result) + ' ' + url
4240

4341

4442
@hook.regex(imdb_re)
@@ -49,15 +47,23 @@ def imdb_url(match, bot):
4947
if imdb_id == "":
5048
imdb_id = match.group(4).split('/')[-2]
5149

52-
params = {'i': imdb_id}
53-
request = requests.get("http://www.omdbapi.com/", params=params, headers=headers)
50+
params = {'id': imdb_id}
51+
request = requests.get(
52+
"https://imdb-scraper.herokuapp.com/title",
53+
params=params,
54+
headers=headers)
5455
content = request.json()
5556

56-
if content['Response'] == 'True':
57-
out = '\x02%(Title)s\x02 (%(Year)s) (%(Genre)s): %(Plot)s'
58-
if content['Runtime'] != 'N/A':
59-
out += ' \x02%(Runtime)s\x02.'
60-
if content['imdbRating'] != 'N/A' and content['imdbVotes'] != 'N/A':
61-
out += ' \x02%(imdbRating)s/10\x02 with \x02%(imdbVotes)s\x02' \
62-
' votes.'
63-
return out % content
57+
if content['success'] is True:
58+
return movie_str(content['result'])
59+
60+
61+
def movie_str(movie):
62+
movie['genre'] = ', '.join(movie['genres'])
63+
out = '\x02%(title)s\x02 (%(year)s) (%(genre)s): %(plot)s'
64+
if movie['runtime'] != 'N/A':
65+
out += ' \x02%(runtime)s\x02.'
66+
if movie['rating'] != 'N/A' and movie['votes'] != 'N/A':
67+
out += ' \x02%(rating)s/10\x02 with \x02%(votes)s\x02' \
68+
' votes.'
69+
return out % movie

plugins/link_announcer.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from cloudbot import hook
66

77
# This will match any URL except the patterns defined in blacklist.
8-
blacklist = '.*(reddit\.com|redd\.it|youtube\.com|youtu\.be|spotify\.com|twitter\.com|twitch\.tv|amazon\.co|xkcd\.com|amzn\.co|steamcommunity\.com|steampowered\.com|newegg\.com|soundcloud\.com|vimeo\.com).*'
8+
blacklist = '.*(reddit\.com|redd\.it|youtube\.com|youtu\.be|imdb\.com|spotify\.com|twitter\.com|twitch\.tv|amazon\.co|xkcd\.com|amzn\.co|steamcommunity\.com|steampowered\.com|newegg\.com|soundcloud\.com|vimeo\.com).*'
99
url_re = re.compile('(?!{})http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+~]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+'.format(blacklist), re.I)
1010

1111
opt_out = []

0 commit comments

Comments
 (0)