-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
298 lines (277 loc) · 12.3 KB
/
helper.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# This file contains helper functions
import requests
from requests_toolbelt.utils import dump
from SPARQLWrapper import SPARQLWrapper, JSON
import urllib.request
import datetime
# from collections import Counter
import locale
locale.setlocale( locale.LC_ALL, '' ) # to format currency
# DEFAULT PROXY
http_proxy = "http://172.16.2.30:8080"
proxyDict = {
"http" : http_proxy,
"https" : http_proxy,
"ftp" : http_proxy
}
def cap(str):
# capitalises each word of a string
return ' '.join(s[:1].upper() + s[1:] for s in str.split(' '))
def get_movie_poster(movie_name, proxy=proxyDict):
# this function fetches the movie posters using TheMovieDataBase
movie_path = "https://api.themoviedb.org/3/search/movie/"
params = {
"api_key" : "1332a724cdfb99d5c9a2b600d4012951",
"query" : movie_name
}
movie = requests.get(movie_path, params=params, proxies=proxy).json()
# data = dump.dump_all(movie)
# print(data.decode('utf-8'))
if movie['total_results'] == 0:
# handle failure
return None
# fetch the url for movie poster and return it
# get the rating
rating = movie['results'][0]['vote_average']
poster_path = "https://image.tmdb.org/t/p/w500/" + movie['results'][0]['poster_path']
return (poster_path, rating)
class Sparql:
def __init__(self, proxy = proxyDict):
self.agent = SPARQLWrapper("https://query.wikidata.org/sparql")
self.agent.setTimeout(40)
self.proxy_support = urllib.request.ProxyHandler(proxy)
self.opener = urllib.request.build_opener(self.proxy_support)
urllib.request.install_opener(self.opener)
def formatDate(self, dateString, needYear=False):
# format the XML date format and returns nicely formatted date with Age too
# no error handling is done here :)
date = [int(d) for d in dateString.split('T')[0].split('-')]
monthDict = {
1: 'January',2: 'February', 3: 'March', 4: 'April', 5: 'May', 6: 'June',
7: 'July', 8: 'August', 9: 'September', 10: 'October', 11: 'November', 12: 'December'
}
if(needYear):
return date[0]
return f"{date[2]} - {monthDict[int(date[1])]} - {date[0]} ({datetime.datetime.now().year - date[0]} Years old)"
def getMovieDetails(self, movieName):
queryString = """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?label ?boxLabel ?genreLabel ?dateLabel ?vactorLabel ?awardsLabel ?directorLabel ?musicLabel ?companyLabel ?actorLabel ?runtime {
?movie wdt:P31/wdt:P279* wd:Q11424 .
?movie rdfs:label ?label
""" +f"FILTER(?label = \"{cap(movieName)}\"@en)"+"""
?movie wdt:P136 ?genre ;
wdt:P577 ?date ;
wdt:P57 ?director ;
wdt:P86 ?music ;
wdt:P2142 ?box;
wdt:P2047 ?runtime;
wdt:P272 ?company ;
OPTIONAL{
?movie wdt:P161 ?actor .
}
OPTIONAL{
?movie wdt:P725 ?vactor .
}
OPTIONAL{
?movie wdt:P166 ?awards .
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}LIMIT 100"""
# print(queryString)
self.agent.setQuery(queryString)
self.agent.setReturnFormat(JSON)
results = self.agent.query().convert()['results']['bindings']
# if no results are found, print a log and return Empty dict
if len(results) == 0:
print(f"No results found for {movieName.title()}")
return "FAIL"
# if results are found, build a dictionary and return it
movie_dict = {}
movie_dict['name'] = results[0]['label']['value'].title()
movie_dict['year'] = self.formatDate(results[0]['dateLabel']['value'], needYear=True)
movie_dict['ML'] = []
movie_dict['BOX'] = locale.currency(int(results[0]['boxLabel']['value']), grouping=True)
movie_dict['GENRE'] = []
voice_actors = []
actors = []
movie_dict['DIR'] = []
movie_dict['CL'] = []
movie_dict['RTIME'] = results[0]['runtime']['value'] + " minutes"
movie_dict['AWS'] = []
VactorExists = 'vactorLabel' in results[0].keys()
ActorExists = 'actorLabel' in results[0].keys()
for result in results:
if VactorExists:
voice_actors.append(result['vactorLabel']['value'].title())
if ActorExists:
actors.append(result['actorLabel']['value'].title())
if 'genreLabel' in result.keys():
movie_dict['GENRE'].append(result['genreLabel']['value'].title())
if 'directorLabel' in result.keys():
movie_dict['DIR'].append(result['directorLabel']['value'].title())
if 'companyLabel' in result.keys():
movie_dict['CL'].append(result['companyLabel']['value'].title())
if 'awardsLabel' in result.keys():
movie_dict['AWS'].append(result['awardsLabel']['value'].title())
if 'musicLabel' in result.keys():
movie_dict['ML'].append(result['musicLabel']['value'].title())
movie_dict['GENRE'] = list(set(movie_dict['GENRE']))
voice_actors = list(set(voice_actors))
actors = list(set(actors))
movie_dict['cast'] = voice_actors + actors
movie_dict['CL'] = list(set(movie_dict['CL']))
movie_dict['DIR'] = list(set(movie_dict['DIR']))
movie_dict['ML'] = list(set(movie_dict['ML']))
movie_dict['AWS'] = list(set(movie_dict['AWS']))
return movie_dict
def getDirectorDetails(self, director):
# returns the details of a director based on director name
queryString = """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?label ?GDLabel ?CZLabel ?IMGLabel ?DOBLabel ?movieLabel ?awardsLabel {
?director wdt:P106 wd:Q2526255 .
?director rdfs:label ?label
FILTER(lang(?label) = 'en')
FILTER(?label = \"""" + cap(director) + """\"@en)
?director wdt:P21 ?GD ;
wdt:P27 ?CZ ;
wdt:P569 ?DOB ;
wdt:P18 ?IMG.
OPTIONAL{
?movie wdt:P31/wdt:279* wd:Q11424.
?movie wdt:P57 ?director.
}
OPTIONAL{
?director wdt:P166 ?awards.
?awards wdt:P31/wdt:P279* wd:Q618779
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""
# print(queryString)
self.agent.setQuery(queryString)
self.agent.setReturnFormat(JSON)
results = self.agent.query().convert()['results']['bindings']
# if no results are found, print a log and return Empty dict
if len(results) == 0:
print(f"No results found for {director.title()}")
return "FAIL"
# if results are found, build a dictionary and return it
dir_dict = {}
dir_dict['name'] = results[0]['label']['value'].title()
dir_dict['DOB'] = self.formatDate(results[0]['DOBLabel']['value'])
dir_dict['CZ'] = results[0]['CZLabel']['value'].title()
dir_dict['GD'] = results[0]['GDLabel']['value'].title()
dir_dict['IMG'] = results[0]['IMGLabel']['value']
dir_dict['movies'] = list([])
dir_dict['awards'] = list([])
for result in results:
try:
movie = result['movieLabel']['value']
except KeyError:
movie = None
try:
award = result['awardsLabel']['value']
except KeyError:
award = None
if movie is not None:
dir_dict['movies'].append(movie.title())
if award is not None:
dir_dict['awards'].append(award.title() + " for " + movie)
if award is not None:
dir_dict['awards'].append(award.title())
dir_dict['movies'] = list(set(dir_dict['movies']))
dir_dict['awards'] = list(set(dir_dict['awards']))
if(len(dir_dict['awards']) > 15):
dir_dict['awards'] = dir_dict['awards'][:14]
# print(dir_dict)
return dir_dict
def getActorDetails(self, actorName):
# returns the details of an actor if found on wikidata
queryString = """
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX p: <http://www.wikidata.org/prop/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?label ?GDLabel ?CZLabel ?DOBLabel ?IMGLabel ?movieLabel ?awardsLabel {
?actor wdt:P106 wd:Q10800557 .
?actor rdfs:label ?label
FILTER(lang(?label) = 'en')
FILTER(?label = \"""" + cap(actorName) + """\"@en)
?actor wdt:P21 ?GD ;
wdt:P27 ?CZ ;
wdt:P569 ?DOB ;
wdt:P18 ?IMG.
OPTIONAL{
?movie wdt:P31/wdt:279* wd:Q11424.
?movie wdt:P161 ?actor.
}
OPTIONAL{
?actor wdt:P166 ?awards.
?awards wdt:P31/wdt:P279* wd:Q618779
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
"""
# print(queryString)
self.agent.setQuery(queryString)
self.agent.setReturnFormat(JSON)
results = self.agent.query().convert()['results']['bindings']
# if no results are found, print a log and return empty dict
if len(results) == 0:
print(f"No results found for {actorName.title()}")
return "FAIL"
# if results are found, build a dictionary and return it
actor_dict = {}
actor_dict['name'] = results[0]['label']['value'].title()
actor_dict['DOB'] = self.formatDate(results[0]['DOBLabel']['value'])
actor_dict['CZ'] = results[0]['CZLabel']['value'].title()
actor_dict['GD'] = results[0]['GDLabel']['value'].title()
actor_dict['IMG'] = results[0]['IMGLabel']['value']
actor_dict['movies'] = []
actor_dict['awards'] = []
for result in results:
try:
movie = result['movieLabel']['value']
if movie[0] == 'Q':
movie = None
except KeyError:
movie = None
try:
award = result['awardsLabel']['value']
except KeyError:
award = None
if movie is not None:
actor_dict['movies'].append(movie.title())
if award is not None:
actor_dict['awards'].append(award.title() + " for " + movie)
if award is not None:
actor_dict['awards'].append(award.title())
actor_dict['movies'] = list(set(actor_dict['movies']))
actor_dict['awards'] = list(set(actor_dict['awards']))
if(len(actor_dict['awards']) > 15):
actor_dict['awards'] = actor_dict['awards'][:14]
return actor_dict
if __name__ == '__main__':
sparql = Sparql(proxyDict)
results = sparql.getActorDetails(input("Enter name: "))
print(results)