forked from 2hands10fingers/movie-pal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmoviepal.py
364 lines (310 loc) · 12.9 KB
/
moviepal.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
#!/usr/bin/python3
import re
import webbrowser
import json
from re import findall
from time import sleep
from bs4 import BeautifulSoup as bs
from requests import get
import pandas as pd
class EarningsCell():
def __init__(self, match):
self.daily_gross = match['daily_gross']
self.percent_change = match['percent_change']
self.average = match['average']
self.total_gross = match['total_gross']
self.day_number = match['day_number']
def __str__(self):
return f'Daily:{self.daily_gross} Change:{self.percent_change} Average:{self.average} Total:{self.total_gross} Day:{self.day_number}'
class mp():
url = 'http://www.omdbapi.com/'
parameters = {'apikey': ''}
# TODO setting the pandas options here is really gross.
# TODO consider not printing right from the dataframe in boxoffice (or elsewhere)
pd.set_option('display.height', 1000)
pd.set_option('display.width', 1000)
pd.set_option('display.max_columns', 500)
pd.set_option('display.max_colwidth', 500)
def __init__(self):
pass
def api(self):
with open('config.json', 'r') as keyfile:
apikey = json.load(keyfile)["omdb_api_key"]
site = 'http://www.omdbapi.com/apikey.aspx'
if apikey in ["KEYHERE", ""]:
sitevisit = input(
f"API Key not found in 'config.json'.\nIt is currently: '{apikey}'.\nWoud you like to visit the site to obtain one (Y/N)? ")
if sitevisit in ['Yes', 'Y', 'y', 'yes', 'Of Course']:
webbrowser.open(site)
else:
raise SystemExit(f'\nERROR: OMDB API Key Missing.\nPlease visit: {site}')
mp.parameters['apikey'] = apikey
def _convert_title(self, content):
match = re.match(r'(?P<title>[A-Z0-9\.\?\-",\(\)\s:]+)[A-Z]{1}.*', content)
return match['title'].title() if match else content
def _convert_earnings_cell(self, content):
match = re.match(
r'(?P<daily_gross>\$[\S]*) (?P<percent_change>--|[+-].*%) / (?P<average>\$[^\$]+)(?P<total_gross>\$[^\$]+) / (?P<day_number>\d{1,3})',
content)
return EarningsCell(match) if match else content
def _convert_date_col_header(self, content):
match = re.match(r'.*(?P<date>\d\d?/\d\d?).*', content)
return match['date'] if match else content
def boxoffice(self):
# TODO stop hard coding the URL
url = 'http://www.boxofficemojo.com/daily/chart/?view=7day&sortdate=2018-05-25&p=.htm'
converters = {
1: self._convert_title,
2: self._convert_earnings_cell,
3: self._convert_earnings_cell,
4: self._convert_earnings_cell,
5: self._convert_earnings_cell,
6: self._convert_earnings_cell,
7: self._convert_earnings_cell,
8: self._convert_earnings_cell
}
df = pd.read_html(io=url,
header=0,
match='Rank\*',
skiprows=3,
converters=converters)[0][:-1]
df.fillna('Not Available', inplace=True)
# Rename the column headers.
df.rename(mapper=self._convert_date_col_header,
axis='columns',
inplace=True)
# TODO decide if you want to print out more than just the current day's results
# TODO perhaps consider returning the dataframe or the string version of it instead of printing
print(df.iloc[:, :3])
def rotten(self):
source = get('''https://www.rottentomatoes.com/
browse/in-theaters?minTomato=0
&maxTomato=100
&minPopcorn=0
&maxPopcorn=100
&genres=1;2;4;5;6;8;9;10;11;13;18;14&sortBy=release''').text
soup = bs(source, 'lxml')
js_source = soup.find_all("script")[38].prettify()
final_json = findall('\[{.*}\]', js_source)
try:
final_json = json.loads(final_json[0])
except IndexError:
SystemExit('Error fetching data. Please retry.')
finally:
final_json = [i['title'] for i in final_json]
return final_json
def imdb(self):
source = get(
'http://www.imdb.com/movies-in-theaters/?ref_=nv_mv_inth_1').text
soup = bs(source, 'lxml')
titles = soup.find_all('td', {"class": "overview-top"})
titles = [i.h4.a.text[:-7].lstrip() for i in titles]
return titles
def metac(self):
ua_one = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_1) '
ua_two = 'AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36'
headers = {'User-Agent': ua_one + ua_two}
source = get(
'http://www.metacritic.com/browse/movies/release-date/theaters/metascore', headers=headers).text
soup = bs(source, 'lxml')
imdb_movies = soup.find_all(
'div', {'class': 'browse_list_wrapper wide'})
titles_list = []
for i in imdb_movies:
scraped_titles = i.find_all('div', {'class': 'title'})
titles = [title.text.rstrip().lstrip() for title in scraped_titles]
titles_list.append(titles)
return sum(titles_list, [])
def in_theaters(self, key=""):
key = key.lower()
if key == "imdb":
return mp.imdb()
if key in ["rt", "rottentomatoes", "rotten tomatoes"]:
return mp.rotten()
if key in ['meta', 'metac', 'metacritic', 'mtc']:
return mp.metac()
all_titles = mp.merged_titles()
print(f'Requesting {len(all_titles)} items...')
return
def requester(self, key=""):
rqst = get(mp.url, params=mp.parameters).json()
print(rqst)
print()
sleep(0.05)
movie = rqst
if key == "":
return movie
try:
return movie[key]
except KeyError:
raise SystemExit(f'That key doesn\'t seem available.\n Try any of these keys:\n\n{list(movie.keys())}')
def merged_titles(self):
tomatoes = mp.rotten()
for i in mp.imdb():
tomatoes.append(i)
for i in mp.metac():
tomatoes.append(i)
return set(tomatoes)
def search_title(self, search_term, key=""):
mp.parameters['t'] = self
return mp.requester(key)
def search_id(self, search_term, key=""):
mp.parameters['i'] = self
return mp.requester(key)
def rotten_search(movie, search_term, key="", printer=False):
source = get(
f'https://www.rottentomatoes.com/search/?search={movie}').text
soup = bs(source, 'lxml')
rotten_results = soup.find_all('script')
for i in rotten_results:
scraped_json = i.text.rstrip().lstrip()
if scraped_json.startswith(
("require(['jquery', 'globals', 'search-results',",
"'bootstrap'], function($, RT, mount)")):
movies = findall('({.*})', scraped_json)[0]
movies = json.loads(movies)
if key == 'print':
printer = True
if key == 'verbose':
links = [i["url"] for i in movies["movies"]]
for url in links:
mp.rotten_scraper(url, key='slug')
if printer == True:
return mp.super_sort(movies)
return movies
def rotten_scraper(self, entry, the_year='', key=''):
rotten_link = 'https://www.rottentomatoes.com{}{}{}'
def link_mangler(entry):
return entry.lower().replace(" ", "_")
def the_url(year):
if key == 'slug':
link = rotten_link.format(entry, '', '')
else:
link = rotten_link.format('/', link_mangler(entry), year)
return link
def perform(source):
the_page = source.text
soup = bs(the_page, 'lxml')
print(f'Page Title: "{soup.find("title").text}"')
audience_review = ''
rotten_rating = ''
try:
audience_review = soup.find(
'div',
{'class': 'audience-score'}).find(
'div',
{'class': 'meter-value'}).span.text
except AttributeError:
audience_review = soup.find(
'div', {'class': 'audience-score'}).find(
'div', {'class': 'noScore'}).text
titles = soup.find('script', {'id': 'jsonLdSchema'}).text
titles = json.loads(titles.encode('ascii', 'ignore').decode('ascii'))
try:
rotten_rating = str(
titles["aggregateRating"]["ratingValue"]) + "%"
except KeyError:
rotten_rating = 'Tomatometer Not Available'
the_ratings = {"AudienceRating": audience_review,
"AverageRating": rotten_rating}
if key == '':
return the_ratings
mp.sorter(the_ratings)
print()
def get_this(year):
if year == '':
return get(the_url(year))
return get(the_url(f'_{year}'))
if get_this(the_year).status_code == 404:
raise SystemExit(
"404 ERROR: \nThat didn't seem to work. Try entering a year or a different title.")
return perform(get_this(the_year))
def search(self, search_term, key=""):
mp.parameters['s'] = self
rqst = get(mp.url, params=mp.parameters)
movie = rqst.json()["Search"]
if key == "":
return mp.key_loop(movie)
items = []
for i in movie:
try:
items.append(i[key])
except KeyError:
raise SystemExit(f'"{key}" is not an available key.',
'\nTry: "Poster", "Title", "imdbID", or "Year"\n')
return mp.looper(items)
def display(self, printer=False, key=""):
if isinstance(self, (list, set)):
items = []
for i in self:
try:
movie = mp.search_title(i)
except KeyError:
pass
finally:
if movie["Response"] == 'False':
pass
if printer == True and key != "":
try:
print(movie[key])
except KeyError:
pass
except TypeError:
pass
elif printer == True:
print(str(movie).encode(
'ASCII', "ignore").decode('ascii'))
print()
elif printer == False and key != "":
try:
items.append(movie[key])
except KeyError:
pass
except TypeError:
pass
else:
items.append(movie)
if printer == False:
return items
else:
raise SystemExit("Please use a [ list ] or { set }!")
def query(self, key="", the_site=""):
if key == "":
raise SystemExit('query() requires a key')
the_query = mp.display(mp.in_theaters(key=key), key=key)
return the_query
def sorter(self, iterable_to_be_sorted):
# TODO this doesn't sort anything??
keys = list(iterable_to_be_sorted)
for i in keys:
print(f'{i}: {iterable_to_be_sorted[i]}')
def looper(self, iterable_to_be_looped_over):
for i in iterable_to_be_looped_over:
if isinstance(i, dict):
mp.key_loop(i)
else:
print(i)
def key_loop(self, dict_to_be_looped_over):
for i in dict_to_be_looped_over:
for key, value in i.items():
print(f'{key}: {value}')
print()
def super_sort(self, data, dict_key="movies"):
# TODO this doesn't sort anything??
for movie in data[dict_key]:
print('-' * 20)
for k, v in movie.items():
if isinstance(movie[k], list):
if k == 'castItems':
print('Cast:')
else:
print(f'{k}:')
for x in movie[k]:
if isinstance(x, dict):
for key, value in x.items():
if key == "url":
print(f'\t\t{key}: https://rottentomatoes.com{value}')
else:
print(f'\t{key}: {value}')
else:
print(f'{k}: {v}')