/
movie_extraction_backup.py
462 lines (441 loc) · 22.5 KB
/
movie_extraction_backup.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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/env python
from movie import Movie
from database import host, user, password, database
import _mysql, datetime, time
allmovies = []
connector = _mysql.connect(host,user,password,database)
for movie_table in ["moviesintheatres", "opening_movies", "upcomingmovies"]:
connector.query("SELECT * FROM " + movie_table)
allmovies_result = connector.store_result()
allmovies_movie = allmovies_result.fetch_row()
while allmovies_movie != ():
allmovies.append(allmovies_movie[0])
allmovies_movie = allmovies_result.fetch_row()
def parental_rating(sentence, movie):
tokened = sentence.split(' ')
for word in tokened:
if word in open("movie/pg.txt").read().split('\n'):
movie.set_parental_rating("PG")
elif word in open("movie/pg-13.txt").read().split('\n'):
movie.set_parental_rating("PG-13")
elif word in open("movie/r.txt").read().split('\n'):
movie.set_parental_rating("R")
def genre(sentence, movie):
genre_list = []
genres = open("movie/genre.txt").read().split('\n')
for genre in genres:
if genre in sentence:
genre_list.append(genre)
movie.set_genre(genre_list)
def directors(sentence, movie):
if "directed by" in sentence:
index = sentence.index("directed by")
sentence = sentence[index + 12:]
name_length = 0
sentence = sentence.replace(', ', ' ').replace(',', ' ').split(' ')
temp = []
for item in sentence:
if name_length == 0:
name = item
name_length += 1
elif name_length < 3:
if item == "and" or item == "or":
temp.append(name)
temp.append(item)
name_length = 0
else:
name += ' ' + item
name_length += 1
elif name_length == 3:
if item == "and" or item == "or":
temp.append(name)
temp.append(item)
name_length = 0
temp.append(name)
movie.set_directors(temp)
def stars(sentence, movie):
if "starring" in sentence or "with" in sentence:
if "starring" in sentence:
index = sentence.index("starring")
sentence = sentence[index + 9:]
elif "with" in sentence:
index = sentence.index("with")
sentence = sentence[index + 5:]
name_length = 0
sentence = sentence.replace(', ', ' ').replace(',', ' ').split(' ')
temp = []
for item in sentence:
if name_length == 0:
name = item
name_length += 1
elif name_length < 3:
if item == "and" or item == "or":
temp.append(name)
temp.append(item)
name_length = 0
else:
name += ' ' + item
name_length += 1
elif name_length == 3:
if item == "and" or item == "or":
temp.append(name)
temp.append(item)
name_length = 0
temp.append(name)
movie.set_stars(temp)
def get_date_interval(sentence):
if "tomorrow" in sentence:
#if int(datetime.date.today().strftime("%m")) in [1, 3, 5, 7, 8, 10, 12]:
#if int(datetime.date.today().strftime("%d")) != 31:
date_interval = (time.mktime(datetime.date(int(datetime.date.today().strftime("%y")) + 2000, int(datetime.date.today().strftime("%m")), int(datetime.date.today().strftime("%d")) + 1).timetuple()), time.mktime(datetime.date(int(datetime.date.today().strftime("%y")) + 2000, int(datetime.date.today().strftime("%m")), int(datetime.date.today().strftime("%d")) + 1).timetuple()))
#else:
# date_interval = (time.mktime(datetime.date(int(datetime.date.today().strftime("%y")) + 2000, int(datetime.date.today().strftime("%m")) + 1, 1).timetuple()), time.mktime(datetime.date(int(datetime.date.today().strftime("%y")) + 2000, int(datetime.date.today().strftime("%m")) + 1, 1).timetuple()))
elif "new" in sentence:
#Check for all new movies this month
if int(datetime.date.today().strftime("%m")) in [1, 3, 5, 7, 8, 10, 12]:
date_interval = ((int(time.mktime(datetime.date.today().timetuple())) - int(datetime.date.today().strftime("%d")) * 86400, int(time.mktime(datetime.date.today().timetuple())) + (31 - int(datetime.date.today().strftime("%d"))) * 86400))
elif int(datetime.date.today().strftime("%m")) in [4, 6, 9, 11]:
date_interval = ((int(time.mktime(datetime.date.today().timetuple())) - int(datetime.date.today().strftime("%d")) * 86400, int(time.mktime(datetime.date.today().timetuple())) + (30 - int(datetime.date.today().strftime("%d"))) * 86400))
elif int(datetime.date.today().strftime("%m")) == 2:
date_interval = ((int(time.mktime(datetime.date.today().timetuple())) - int(datetime.date.today().strftime("%d")) * 86400, int(time.mktime(datetime.date.today().timetuple())) + (28 - int(datetime.date.today().strftime("%d"))) * 86400))
else:
date_interval = (time.mktime(datetime.date.today().timetuple()), time.mktime(datetime.date.today().timetuple()))
return date_interval
def fix_time(sentence):
i = 0
while len(sentence) > i:
if sentence[i].isdigit():
if i + 1 == len(sentence):
if sentence[i] == '0' and i > 1 and sentence[i - 1].isdigit():
if len(sentence) > 2:
if sentence[i - 2] != ':':
sentence = sentence[:i + 1] + ":00"
elif sentence[i - 1] != '0':
sentence = sentence[:i + 1] + ":00"
else:
if len(sentence) > 2:
if sentence[i - 2] != ':':
sentence = sentence[:i + 1] + ":00"
else:
sentence = sentence[:i + 1] + ":00"
elif sentence[i + 1] == ' ':
if sentence[i] == '0' and i > 1 and sentence[i - 1].isdigit():
if len(sentence) > 2:
if sentence[i - 2] != ':':
sentence = sentence[:i + 1] + ":00" + sentence[i + 1:]
elif sentence[i - 1] != '0':
sentence = sentence[:i + 1] + ":00" + sentence[i + 1:]
else:
if len(sentence) > 2:
if sentence[i - 2] != ':':
sentence = sentence[:i + 1] + ":00" + sentence[i + 1:]
else:
sentence = sentence[:i + 1] + ":00" + sentence[i + 1:]
i += 1
return sentence
def get_time_interval(sentence):
sentence = fix_time(sentence)
i = 0
lis = []
if ':' in sentence:
while len(sentence) > i:
if sentence[i] == ':':
lis.append([sentence[i - 2: i], sentence[i + 1: i + 3]])
i += 1
for time in lis:
time[0] = time[0].rstrip()
time[0] = int(time[0])
if time[0] < 12:
time[0] += 12
time[1] = int(time[1])
return lis
def check_product(name_list, sentence, p, g, d, s):
offsettime = 1
before = "before" in sentence
after = "after" in sentence
around = "around" in sentence
filtered = []
showtime_results = []
showtime_theatres = []
connector.query("SELECT title,theatre_name,date_time,product_id FROM onconnect")
showtime_result = connector.store_result()
showtime_movie = showtime_result.fetch_row()
while showtime_movie != ():
showtime_results.append(showtime_movie[0])
theatre = showtime_movie[0][1].lower().replace("the ", "").replace(" cinemas", "").replace(" cinema", "").replace(" theatre", "").replace('-', ' ').replace(" and", "").replace(" in", "").replace(" at", "").replace(" for", "").split(" ")
if (theatre, showtime_movie[0][1]) not in showtime_theatres:
showtime_theatres.append((theatre, showtime_movie[0][1]))
showtime_movie = showtime_result.fetch_row()
theatre_list = []
matchmax = 0
count = 0
for theatre in showtime_theatres:
for word in theatre[0]:
if not word.isdigit() and word in sentence:
count += 1
theatre[0].append(count)
if count > matchmax:
matchmax = count
count = 0
if matchmax != 0:
for theatre in showtime_theatres:
if theatre[0][-1] == matchmax:
theatre_list.append(theatre[1])
date_interval = get_date_interval(sentence)
time_interval = get_time_interval(sentence)
if len(time_interval) == 1:
time_interval.append([23, 59])
if name_list != []:
if theatre_list == []:
for mov in showtime_results:
t = mov[2][:mov[2].index("T")].split('-')
h = mov[2][mov[2].index("T") + 1:].split(':')
h[0] = int(h[0])
h[1] = int(h[1])
if mov[0] in name_list and time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()) >= date_interval[0] and date_interval[1] >= time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()):
if time_interval == []:
filtered.append(mov)
else:
if before:
if h[0] < time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] < time_interval[0][1]:
filtered.append(mov)
elif after:
if h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] > time_interval[0][1]:
filtered.append(mov)
elif around:
if h[0] - offsettime < time_interval[0][0] or h[0] + offsettime > time_interval[0][0]:
filtered.append(mov)
else:
if h[0] >= time_interval[0][0] and h[0] <= time_interval[1][0]:
if h[0] == time_interval[0][0] and h[0] == time_interval[1][0]:
if h[1] >= time_interval[0][1] and h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] == time_interval[0][0]:
if h[1] >= time_interval[0][1]:
filtered.append(mov)
elif h[0] == time_interval[1][0]:
if h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] < time_interval[1][0]:
filtered.append(mov)
else:
for mov in showtime_results:
t = mov[2][:mov[2].index("T")].split('-')
h = mov[2][mov[2].index("T") + 1:].split(':')
h[0] = int(h[0])
h[1] = int(h[1])
if mov[0] in name_list and mov[1] in theatre_list and time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()) >= date_interval[0] and date_interval[1] >= time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()):
if time_interval == []:
filtered.append(mov)
else:
if before:
if h[0] < time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] < time_interval[0][1]:
filtered.append(mov)
elif after:
if h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] > time_interval[0][1]:
filtered.append(mov)
elif around:
if h[0] - offsettime < time_interval[0][0] or h[0] + offsettime > time_interval[0][0]:
filtered.append(mov)
else:
if h[0] >= time_interval[0][0] and h[0] <= time_interval[1][0]:
if h[0] == time_interval[0][0] and h[0] == time_interval[1][0]:
if h[1] >= time_interval[0][1] and h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] == time_interval[0][0]:
if h[1] >= time_interval[0][1]:
filtered.append(mov)
elif h[0] == time_interval[1][0]:
if h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] < time_interval[1][0]:
filtered.append(mov)
elif name_list == [] and p and g and d and s:
if theatre_list == []:
for mov in showtime_results:
t = mov[2][:mov[2].index("T")].split('-')
h = mov[2][mov[2].index("T") + 1:].split(':')
h[0] = int(h[0])
h[1] = int(h[1])
if time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()) >= date_interval[0] and date_interval[1] >= time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()):
if time_interval == []:
filtered.append(mov)
else:
if before:
if h[0] < time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] < time_interval[0][1]:
filtered.append(mov)
elif after:
if h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] > time_interval[0][1]:
filtered.append(mov)
elif around:
if h[0] - offsettime < time_interval[0][0] or h[0] + offsettime > time_interval[0][0]:
filtered.append(mov)
else:
if h[0] >= time_interval[0][0] and h[0] <= time_interval[1][0]:
if h[0] == time_interval[0][0] and h[0] == time_interval[1][0]:
if h[1] >= time_interval[0][1] and h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] == time_interval[0][0]:
if h[1] >= time_interval[0][1]:
filtered.append(mov)
elif h[0] == time_interval[1][0]:
if h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] < time_interval[1][0]:
filtered.append(mov)
else:
for mov in showtime_results:
t = mov[2][:mov[2].index("T")].split('-')
h = mov[2][mov[2].index("T") + 1:].split(':')
h[0] = int(h[0])
h[1] = int(h[1])
if mov[1] in theatre_list and time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()) >= date_interval[0] and date_interval[1] >= time.mktime(datetime.date(int(t[0]), int(t[1]), int(t[2])).timetuple()):
if time_interval == []:
filtered.append(mov)
else:
if before:
if h[0] < time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] < time_interval[0][1]:
filtered.append(mov)
elif after:
if h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] == time_interval[0][0] and h[1] > time_interval[0][1]:
filtered.append(mov)
elif around:
if h[0] - offsettime < time_interval[0][0] or h[0] + offsettime > time_interval[0][0]:
filtered.append(mov)
else:
if h[0] >= time_interval[0][0] and h[0] <= time_interval[1][0]:
if h[0] == time_interval[0][0] and h[0] == time_interval[1][0]:
if h[1] >= time_interval[0][1] and h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] == time_interval[0][0]:
if h[1] >= time_interval[0][1]:
filtered.append(mov)
elif h[0] == time_interval[1][0]:
if h[1] <= time_interval[1][1]:
filtered.append(mov)
elif h[0] > time_interval[0][0]:
filtered.append(mov)
elif h[0] < time_interval[1][0]:
filtered.append(mov)
if filtered == []:
return "none"
else:
connector.query("SELECT response_id FROM bi_response ORDER BY response_id DESC LIMIT 1")
results = connector.store_result()
answer = results.fetch_row()
if answer == ():
res_id = 0
else:
res_id = int(answer[0][0])
res_id += 1
for product in filtered:
connector.query("INSERT INTO bi_response (response_id,tweet_id,user_id,product_id,created_at) VALUES (" + str(res_id) + ",0,0," + str(product[3]) + ",0)")
return res_id
def movie(sentence):
sentence = sentence.lower()
movie_list = []
m = Movie()
parental_rating(sentence, m)
genre(sentence, m)
directors(sentence, m)
stars(sentence, m)
p = m.get_parental_rating() == ''
g = m.get_genre() == []
d = m.get_directors() == []
s = m.get_stars() == []
for movie in allmovies:
parental_rating_pass = p
genre_pass = g
director_pass = d
stars_pass = s
if not parental_rating_pass:
parental_rating_pass = m.get_parental_rating() == movie[2]
if not genre_pass:
for m_genre in m.get_genre():
genre_pass = m_genre in movie[4].lower()
if not director_pass:
i = 1
if movie[6].lower() != '':
temp = movie[6].lower().split(', ')
for actor in temp:
director_pass = actor in m.get_directors()[i-1].lower()
if director_pass:
break;
while i < len(m.get_directors()):
temp1 = False
temp2 = False
for actor in temp:
temp1 = actor in m.get_directors()[i-1].lower()
if temp1:
break;
for actor in temp:
temp2 = actor in m.get_directors()[i+1].lower()
if temp2:
break;
if m.get_directors()[i].lower() == 'and':
director_pass = temp1 and temp2
elif m.get_directors()[i].lower() == 'or':
director_pass = temp1 or temp2
if director_pass and i + 2 < len(m.get_directors()) and m.get_directors()[i + 2].lower() == 'or':
break;
i += 2
if not stars_pass:
i = 1
if movie[8].lower() != '':
temp = movie[8].lower().split(', ')
for actor in temp:
stars_pass = actor in m.get_stars()[i-1].lower()
if stars_pass:
break;
while i < len(m.get_stars()):
temp1 = False
temp2 = False
for actor in temp:
temp1 = actor in m.get_stars()[i-1].lower()
if temp1:
break;
for actor in temp:
temp2 = actor in m.get_stars()[i+1].lower()
if temp2:
break;
if m.get_stars()[i].lower() == 'and':
stars_pass = temp1 and temp2
elif m.get_stars()[i].lower() == 'or':
stars_pass = temp1 or temp2
if stars_pass and i + 2 < len(m.get_stars()) and m.get_stars()[i + 2].lower() == 'or':
break;
i += 2
if parental_rating_pass and genre_pass and director_pass and stars_pass:
if p and g and d and s:
return check_product([], sentence, p, g, d, s)
movie_list.append(movie[1])
return check_product(movie_list, sentence, p, g, d, s)
connector = _mysql.connect(host,user,password,database)
connector.query("SELECT * FROM mentions ORDER BY mention_id DESC LIMIT 1")
results = connector.store_result()
tweet = results.fetch_row()[0][1]
response = movie(tweet)
print(response)
#print(movie("comedy for adults starring channing tatum directed by phil lord playing from 7 to 8:45 at carlton theatres"))