-
Notifications
You must be signed in to change notification settings - Fork 6
/
imdb.py
56 lines (44 loc) · 1.53 KB
/
imdb.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
import json
import requests
class Movie():
title = None
id = None
def __init__(self, title, id):
self.session = requests.session()
self.title = title
self.id = id
def get_info(self):
try:
data = self.session.get('https://www.imdb.com/title/{id}/?ref_=nv_sr_1'.format(id=self.id)).content.decode(
'utf-8')
return data.split('<span itemprop="ratingValue">')[1].split('<')[0]
except:
return "N/A"
def __str__(self):
return "{} ({})".format(self.title, self.id)
def __unicode__(self):
return "{} ({})".format(self.title, self.id)
class Parser():
session = None
search_results = []
def __init__(self):
self.session = requests.session()
def search(self, keyword):
data = self.session.get(
'https://v2.sg.media-imdb.com/suggests/{aramailk}/{arama}.json'.format(
aramailk=keyword[0],
arama=keyword))
content = data.content.decode("utf-8")
suggests_json = content.split('imdb${sup}('.format(sup=keyword))[1][:-1]
suggests = json.loads(suggests_json)
for suggest in suggests.get('d'):
self.search_results.append(Movie(**{
"title": suggest.get('l'),
"id": suggest.get('id')
}))
return self
def get_results(self):
return self.search_results
parse = Parser()
for movie in parse.search("supernat").get_results():
print(movie, movie.get_info())