forked from msaqib4203/IMDB-API
-
Notifications
You must be signed in to change notification settings - Fork 0
/
imdb.py
64 lines (53 loc) · 1.96 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
57
58
59
60
61
62
63
64
#Dependancies
from bs4 import BeautifulSoup
import requests
import json
def parsePersons(persons):
names = []
if isinstance(persons,dict):
names.append(persons['name'])
return names
for person in persons:
if person['@type'] == "Person":
names.append(person['name'])
return names
def getJSON(html):
data = {}
data['id'] = html.find(attrs={'property':'pageId'})['content']
data['url'] = 'https://www.imdb.com/title/'+data['id']
html_json = html.find(attrs={'type':'application/ld+json'}).text.strip()
fetchedJson = json.loads(html_json)
data['poster'] = html.find(attrs={'class':'poster'}).find('img')['src']
title_wrapper = html.find(attrs={'class':'title_wrapper'}).text.strip()
data['title'] = title_wrapper[:title_wrapper.find(')')+1]
data['rating'] = html.find(itemprop='ratingValue').text
data['bestRating'] = html.find(itemprop='bestRating').text
data['votes'] = html.find(itemprop='ratingCount').text
data['rated'] = fetchedJson['contentRating']
data['genres'] = fetchedJson['genre']
data['description'] = fetchedJson['description']
data['cast'] = parsePersons(fetchedJson['actor'])
data['writers'] = parsePersons(fetchedJson['creator'])
data['directors'] = parsePersons(fetchedJson['director'])
json_data = json.dumps(data)
return json_data
def getHTML(url):
response = requests.get(url)
return BeautifulSoup(response.content,'html.parser')
def getURL(input):
try:
if input[0] == 't' and input[1] == 't':
html = getHTML('https://www.imdb.com/title/'+input+'/')
else:
html = getHTML('https://www.google.co.in/search?q='+input)
for cite in html.findAll('cite'):
if 'imdb.com/title/tt' in cite.text:
html = getHTML(cite.text)
break
return getJSON(html)
except Exception as e:
print e
return 'Invalid input or Network Error!'
input = raw_input("Enter IMDB ID or Title: ")
print('Getting information, Please Wait....')
print(getURL(input))