11import re
2-
32import requests
43
54from cloudbot import hook
65
7-
86id_re = re .compile ("tt\d+" )
97imdb_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
0 commit comments