-
Notifications
You must be signed in to change notification settings - Fork 0
/
__main__.py
345 lines (305 loc) · 12 KB
/
__main__.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
import os
import sys
import time
import datetime
import urllib.request
from globals import *
from html.parser import HTMLParser
class MyHTMLParser(HTMLParser):
def __init__(self):
#http://stackoverflow.com/questions/3276040/how-can-i-use-the-python-htmlparser-library-to-extract-data-from-a-specific-div
HTMLParser.__init__(self)
self.scanData = 0
self.recData = []
self.parsedAttrs = {}
self.parsedAttrs["genre"] = []
def handle_starttag(self, tag, attrs):
#print("Start tag:", tag)
#for attr in attrs:
# print(" attr:", attr)
if tag == "time":
self.parsedAttrs["dateTime"] = attrs[1][1]
if tag == "a" and "genre" in attrs[0][1]:
self.parsedAttrs["genre"].append(attrs[0][1])
if tag == "meta" and "datePublished" in attrs[0][1]:
self.parsedAttrs["datePublished"] = attrs[1][1]
if tag == "meta" and "og:title" in attrs[0][1]:
self.parsedAttrs['title'] = attrs[1][1]
if tag == "time" and "birthDate" in attrs[1][1]:
self.parsedAttrs['birthDate'] = attrs[0][1]
if tag == "span" and "location-display" in attrs[0][1]:
self.scanData = 1
def handle_endtag(self, tag):
#print("End tag :", tag)
pass
def handle_data(self, data):
#print("Data :", data)
if self.scanData:
self.recData.append(data)
self.scanData = 0
pass
def handle_comment(self, data):
#print("Comment :", data)
pass
class movie:
def __init__(self, imdbID):
self.id = imdbID
self.title = ""
self.runTime = 0
self.genre = []
self.releaseDate = 0
self.cast = []
#http://stackoverflow.com/questions/11325019/output-on-the-console-and-file-using-python
class Tee(object):
def __init__(self, *fileName):
self.file = fileName
def write(self, obj):
for f in self.file:
f.write(obj)
def HTMLResponse(url):
try:
page = urllib.request.urlopen(url)
except urllib.error.URLError as e:
print(e.strerror)
return page.read()
def filterMovieID(fileName, method):
movieID = []
if method == "getNowShowing":
with open(fileName, "r", encoding="utf8") as file:
for line in iter(file):
if "/showtimes/title/" in line:
if "tt" in line:
indexStart = line.index("tt")
indexEnd = indexStart + line[indexStart:].index('/')
if line[indexStart:indexEnd] not in movieID:
movieID.append(line[indexStart:indexEnd])
if "location-display" in line:
parser = MyHTMLParser()
parser.feed(line)
print("{} movies currently running in {} within 30 miles\n".format(len(movieID), parser.recData[0]))
elif method == "getTopTen":
with open(fileName, "r", encoding="utf8") as file:
startRecord = False
for line in iter(file):
if parseStarter_boxOfficeTopTen in line:
startRecord = True
if startRecord and "?ref_=inth_ov_i" in line:
if "tt" in line:
indexStart = line.index("tt")
indexEnd = indexStart + line[indexStart:].index('/')
if line[indexStart:indexEnd] not in movieID:
movieID.append(line[indexStart:indexEnd])
print("Box Office - Top Ten Movies\n")
return movieID
def cacheCheck(path, fileName):
#print(os.path.join(path, fileName))
if os.path.isfile(os.path.join(path, fileName)) : return True
def getNowShowing():
page_content = HTMLResponse(nowShowingURL)
with open("html_cache/now_showing.html", "wb") as file:
file.write(page_content)
file.close()
return filterMovieID("html_cache/now_showing.html", "getNowShowing")
def getTopTen():
page_content = HTMLResponse(topTenURL)
with open("html_cache/top_ten.html", "wb") as file:
file.write(page_content)
file.close()
return filterMovieID("html_cache/top_ten.html", "getTopTen")
def initDatabase(movieList):
nowShowing = []
[nowShowing.append(movie(movieID)) for movieID in movieList]
return nowShowing
def convertDate(myDate):
YYYY, MM, DD = (myDate.split("-"))
YYYY, MM, DD = int(YYYY), int(MM), int(DD)
if MM>0 and DD>0 and YYYY>0:
return datetime.date(YYYY, MM, DD).strftime("%d %B %Y")
else:
if YYYY:
return str(YYYY)
else:
return myDate
def getGenreStr(genreList):
genreStr = ""
for genreType in genreList:
genreStr = genreStr + genreType + ", "
return genreStr[:-2]
def getRunTime(runTime):
if runTime:
for char in runTime:
if char.isdigit():
indexStart = runTime.index(char)
break
for char in runTime[indexStart:]:
if char.isalpha():
indexEnd = indexStart + runTime[indexStart:].index(char)
return int(runTime[indexStart:indexEnd])
def getCastAverageAge(castList, releaseDate):
totalAge = 0
castNum = 0
if releaseDate:
releaseYear = releaseDate.split("-")[0]
if len(releaseYear) == 4:
releaseYear = int(releaseYear)
else:
return 0
for cast in castList:
if cast[1]:
year = cast[1].split('-')[0]
if year:
if int(year) > 0:
age = releaseYear - int(year)
#print("{}:{} year".format(cast[0], age))
castNum += 1
totalAge += age
if castNum:
return totalAge//castNum
else:
return 0
def printMovieInfoDetailed(nowShowing):
#stdout = sys.stdout
#sys.stdout = open('output.txt', 'w')
f = open('output.txt', 'w')
stdout = sys.stdout
sys.stdout = Tee(sys.stdout, f)
print("\n")
for counter, movie in enumerate(nowShowing):
if len(nowShowing) < 10:
print("{}. {}".format(counter+1, movie.title))
elif len(nowShowing) < 100:
print("{:2d}. {}".format(counter+1, movie.title))
else:
print("{:3d}. {}".format(counter+1, movie.title))
if movie.runTime:
print(" RunTime: {} mins".format(movie.runTime))
else:
print(" RunTime: N/A")
print(" Genre: ", end="")
genreStr = getGenreStr(movie.genre)
print("{}".format(genreStr))
dateStr = convertDate(movie.releaseDate)
print(" Date Released: {}".format(dateStr))
averageCastAge = getCastAverageAge(movie.cast, movie.releaseDate)
if averageCastAge:
print(" Cast Average Age(when released): {} years".format(averageCastAge))
else:
print(" Cast Average Age: N/A")
if len(movie.cast):
print(" Cast List:")
for cast in movie.cast:
if cast[1]:
print(" {:<20s}: {:<20s}".format(cast[0], convertDate(cast[1])))
sys.stdout = stdout
def getCastInfo(nameID):
imdb = "http://www.imdb.com"
if not cacheCheck(os.path.join("html_cache", "cast"), str(nameID)+str(".html")):
print(" Querying cast# {}".format(nameID))
page_content = HTMLResponse(imdb + "/name/" + nameID + "/")
with open("html_cache/cast/"+nameID+".html", "wb") as file:
file.write(page_content)
file.close()
with open("html_cache/cast/"+nameID+".html", "r", encoding='utf8') as file:
parser = MyHTMLParser()
for line in iter(file):
if "og:title" in line:
parser.feed(line)
if "birthDate" in line:
parser.feed(line)
break
if parseBreaker_birthDate in line:
break
if "title" in parser.parsedAttrs:
name = parser.parsedAttrs["title"]
if "birthDate" in parser.parsedAttrs:
birthDate = parser.parsedAttrs["birthDate"]
else:
birthDate = None
return (name, birthDate)
def getAllCreditedCasts(movieID):
#print(moviePageURL+movieID+fullcreditsExtn)
imdb = "http://www.imdb.com"
cast = []
if not cacheCheck("html_cache", str(movieID)+str("_full_credits.html")):
print(" Querying credited casts not in cache..")
page_content = HTMLResponse(moviePageURL+movieID+fullcreditsExtn)
with open("html_cache/"+movieID+"_full_credits.html", "wb") as file:
file.write(page_content)
file.close()
with open("html_cache/"+movieID+"_full_credits.html", "r", encoding='utf8') as file:
parser = MyHTMLParser()
parsingOn = False
for line in iter(file):
if "?ref_=ttfc_fc_cl_i" in line:
#I prefer to do this in single line
nameID = (line[line.index("name")+5:line.index("name")+5+line[line.index("name")+5:].index("/")])
cast.append(getCastInfo(nameID))
elif parseBreaker_uncreditedCast in line:
break
elif parseBreaker_characterPage in line:
break
return cast
def movie_setParameters(nowShowing):
#print([movie.id for movie in nowShowing])
for counter, movie in enumerate(nowShowing):
if not cacheCheck("html_cache", str(movie.id)+str(".html")):
print("Querying movie({}/{})# {}".format(counter+1, len(nowShowing), movie.id))
page_content = HTMLResponse(moviePageURL+movie.id)
with open("html_cache/"+movie.id+".html", "wb") as file:
file.write(page_content)
file.close()
with open("html_cache/"+movie.id+".html", "r", encoding='utf8') as file:
parser = MyHTMLParser()
parsingOn = False
for line in iter(file):
#Parses movie title
if "og:title" in line:
parser.feed(line)
#Parses other data
if "class=\"infobar\"" in line:
parsingOn = True
if parsingOn:
parser.feed(line)
if "</div>" in line:
break
movie.title = parser.parsedAttrs['title']
if 'dateTime' in parser.parsedAttrs:
movie.runTime = getRunTime(parser.parsedAttrs['dateTime'])
if 'genre' in parser.parsedAttrs:
for genreType in parser.parsedAttrs['genre']:
movie.genre.append(genreType[genreType.index("genre")+6:genreType.index("?")])
if 'datePublished' in parser.parsedAttrs:
movie.releaseDate = parser.parsedAttrs['datePublished']
movie.cast = getAllCreditedCasts(movie.id)
return nowShowing
def createDatabase(movieList):
nowShowing = initDatabase(movieList)
nowShowing = movie_setParameters(nowShowing)
return nowShowing
if __name__ == "__main__":
if not os.path.exists("html_cache"):
os.mkdir("html_cache")
if not os.path.exists("html_cache/cast"):
os.mkdir("html_cache/cast")
while True:
try:
whatMovies = int(input("1. Top Ten Movie\n2. All Now Showing Nearby\n"))
if whatMovies == 1 or whatMovies ==2:
break
else:
print("Please input a valid choice..")
except ValueError: print("Please input a valid choice..")
startTime = time.time()
if whatMovies == 1:
movieList = getTopTen()
else:
movieList = getNowShowing()
nowShowing = createDatabase(movieList)
endTime = time.time()
printMovieInfoDetailed(nowShowing)
"""Testing small list of movies, skip parsing nowShowingURL"""
#movieList = ['tt2713180']
#nowShowing = createDatabase(movieList)
#getAllCreditedCasts('tt2713180')
"""--------------------------------------------------------"""
print("Result generated in {:.2f} seconds".format(endTime-startTime))