-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdbex.py
62 lines (48 loc) · 1.49 KB
/
imdbex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# Code to get movie rating from imdb and sort the list of movies.
import imdb
import re
from xgoogle.search import GoogleSearch, SearchError
from xgoogle.googlesets import GoogleSets
def get_rating(movie_name):
try:
# Use xgoogle api to parse google. Following is the url to the api
# http://www.catonmat.net/blog/python-library-for-google-search/
search_str = movie_name + ' site:imdb.com'
gs = GoogleSearch(search_str)
gs.results_per_page = 1
results = gs.get_results()
url = results[0].url.encode('utf8')
# url_title = results[0].title.encode('utf8')
# print url, url_title
imdb_rating(url)
except SearchError, e:
print "Search failed: %s" % e
def imdb_rating(url):
tt = url.split('/')[-2]
if(tt[0:2] == 'tt'):
movie_id = tt[2:]
else:
movie_id = None
print 'Note: \"%s\" is\'t listed in imdb as movie name' %movie_name
# print movie name and movie rating using imdbpy api
if movie_id != None:
im = imdb.IMDb(accessSystem='http')
m = im.get_movie(movie_id)
title, year, rating = m['title'], m['year'], m['rating']
print "Movie name:: %s (%d) \nRating:: %0.2f" %(title, year, rating)
print
def main():
# read movies and generate ratings
f = open('movies.txt', 'r')
for line in f:
print line,
get_rating(line)
gs = GoogleSets(['Godfather', 'Scarface'])
results = gs.get_results()
print 'Some movies you might like: '
i = 0
while i < 5:
print " ", (i + 1), ".", results[i].encode('utf8')
i = i + 1
if __name__ == '__main__':
main()