-
Notifications
You must be signed in to change notification settings - Fork 0
/
movie_crawl.py
317 lines (239 loc) · 12.2 KB
/
movie_crawl.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
# -*- coding: utf-8 -*-
# This is a simple Hello World Alexa Skill, built using
# the implementation of handler classes approach in skill builder.
import logging
import requests
import random
from ask_sdk_core.skill_builder import SkillBuilder
from ask_sdk_core.dispatch_components import AbstractRequestHandler
from ask_sdk_core.dispatch_components import AbstractExceptionHandler
from ask_sdk_core.utils import is_request_type, is_intent_name
from ask_sdk_core.handler_input import HandlerInput
from ask_sdk_model.ui import SimpleCard
from ask_sdk_model import Response
sb = SkillBuilder()
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
indent_slots = {
'date_slot': 'date',
'date_slot_key': 'DATE',
'industry_slot': 'industry',
'industry_slot_key': 'Industry',
}
class LaunchRequestHandler(AbstractRequestHandler):
"""Handler for Skill Launch."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("LaunchRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "Welcome to Movie Crawl, you can ask movie releases by saying, what's coming this week in Bollywood?"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Movie Crawl", speech_text)).set_should_end_session(
False)
return handler_input.response_builder.response
class HelpIntentHandler(AbstractRequestHandler):
"""Handler for Help Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.HelpIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "You can ask about movie releases to me!!"
handler_input.response_builder.speak(speech_text).ask(
speech_text).set_card(SimpleCard(
"Movie Help", speech_text))
return handler_input.response_builder.response
class CancelOrStopIntentHandler(AbstractRequestHandler):
"""Single handler for Cancel and Stop Intent."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return (is_intent_name("AMAZON.CancelIntent")(handler_input) or
is_intent_name("AMAZON.StopIntent")(handler_input))
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = "Goodbye!"
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Movie Crawl", speech_text))
return handler_input.response_builder.response
class FallbackIntentHandler(AbstractRequestHandler):
"""AMAZON.FallbackIntent is only available in en-US locale.
This handler will not be triggered except in that locale,
so it is safe to deploy on any locale.
"""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("AMAZON.FallbackIntent")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
speech_text = (
"The Movie Crawl skill can't help you with that. "
"You can ask about movie releases by saying, movies releasing in Bollywood this friday!!")
reprompt = "You can ask about movie releases by saying, movies releasing in Bollywood this friday!!"
handler_input.response_builder.speak(speech_text).ask(reprompt)
return handler_input.response_builder.response
class SessionEndedRequestHandler(AbstractRequestHandler):
"""Handler for Session End."""
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_request_type("SessionEndedRequest")(handler_input)
def handle(self, handler_input):
# type: (HandlerInput) -> Response
return handler_input.response_builder.response
class CatchAllExceptionHandler(AbstractExceptionHandler):
"""Catch all exception handler, log exception and
respond with custom message.
"""
def can_handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> bool
return True
def handle(self, handler_input, exception):
# type: (HandlerInput, Exception) -> Response
logger.error(exception, exc_info=True)
speech = "Sorry, there was some problem. Please try again!!"
handler_input.response_builder.speak(speech).ask(speech)
return handler_input.response_builder.response
class GetUpcomingMoviesHandler(AbstractRequestHandler):
"""Handler for Get Upcoming Movies Intent """
def can_handle(self, handler_input):
# type: (HandlerInput) -> bool
return is_intent_name("GetUpcomingMovies")(handler_input)
def handle(self, handler_input):
# type (HandlerInput) -> Response
slots = handler_input.request_envelope.request.intent.slots
if indent_slots['industry_slot'] in slots and slots[indent_slots['industry_slot']].value:
industry_value = slots[indent_slots['industry_slot']].value
request_url = "https://api.themoviedb.org/3/movie/upcoming"
parameters = {
'api_key': 'c0f06b953bd8a08dfaef7196d198b463',
}
region_dict = {
"Bollywood": {
'region': "IN"
},
"Hollywood": {
'region': "US"
}
}
if not industry_value:
industry_value = random.choice(list(region_dict))
parameters.update(region_dict[industry_value])
response = requests.get(request_url, params=parameters).json()
movies = []
card_movies = []
if len(response['results']):
speech_text = "Upcoming movies in {} are:- ".format(industry_value)
card_text = speech_text + '\n'
for i, item in enumerate(response['results'][:5]):
movies.append(item['title'])
card_movies.append(str(i+1) + '. '+ item['title'])
speech_text += ', '.join(movies).replace('&', 'and')
card_text += '\n'.join(card_movies)
if len(response['results']) > 5:
speech_text += ", and many more."
card_text += ", and many more."
else:
speech_text = "There are no upcoming movies."
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Upcoming Movie Releases", card_text)).set_should_end_session(True)
else:
speech_text = "I am not sure."
reprompt = "You can ask by saying, upcoming movies in Hollywood"
handler_input.response_builder.speak(speech_text).ask(reprompt)
return handler_input.response_builder.response
class GetMovieReleasesHandler(AbstractRequestHandler):
"""Handler for Get Movie Releases"""
def can_handle(self, handler_input):
return is_intent_name("GetMovieReleases")(handler_input)
def handle(self, handler_input):
def set_date_parameters(params, date_value):
from datetime import datetime, timedelta
date_list = date_value.split('-')
date_list_len = len(date_list)
date_year = date_list[0]
parameters['primary_release_year'] = date_year
if 'W' in date_value: # user request for week range
start_date = datetime.strptime(date_value + '-1', "%Y-W%W-%w")
end_date = start_date + timedelta(days=6)
params['primary_release_date.gte'] = start_date.strftime("%Y-%m-%d")
params['primary_release_date.lte'] = end_date.strftime("%Y-%m-%d")
elif date_list_len == 2 and 'W' not in date_value: # User request for month range
start_date = datetime.strptime(date_value , "%Y-%m")
next_month = start_date.replace(day=28) + timedelta(days=4)
end_date = next_month - timedelta(days=next_month.day)
params['primary_release_date.gte'] = start_date.strftime("%Y-%m-%d")
params['primary_release_date.lte'] = end_date.strftime("%Y-%m-%d")
# elif date_list_len == 1 and 'W' not in date_value: # User request for full year
else: # It is a normal date
params['primary_release_date.gte'] = params['primary_release_date.lte'] = date_value
def set_industry_parameters(params, industry_value):
industry_dict = {
"Bollywood": {
'region': 'IN',
'with_original_language': 'hi'
},
"Hollywood": {
'region': 'US',
'sort_by': 'popularity.desc'
}
}
if not industry_value:
random_industry = random.choice(list(industry_dict))
params.update(industry_dict[random_industry])
return random_industry
elif industry_value in industry_dict:
params.update(industry_dict[industry_value])
return industry_value
else:
speech_text = "I am not sure about {}".format(industry_value)
reprompt = "Please try again by specifying film industry."
handler_input.response_builder.speak(speech_text).ask(reprompt)
slots = handler_input.request_envelope.request.intent.slots
if indent_slots['date_slot'] in slots and slots[indent_slots['date_slot']].value:
date_value = slots[indent_slots['date_slot']].value
handler_input.attributes_manager.session_attributes[indent_slots['date_slot_key']] = date_value
industry_value = ''
if indent_slots['industry_slot'] in slots:
industry_value = slots[indent_slots['industry_slot']].value
handler_input.attributes_manager.session_attributes[indent_slots['industry_slot_key']] = industry_value
request_url = "https://api.themoviedb.org/3/discover/movie"
parameters = {
'api_key': 'c0f06b953bd8a08dfaef7196d198b463',
'sort_by': 'release_date.asc',
}
# Sets the date range parameter based on user input date
set_date_parameters(parameters, date_value)
industry_value = set_industry_parameters(parameters, industry_value)
response = requests.get(request_url, params=parameters).json()
movies = []
card_movies = []
if response['total_results']:
speech_text = "The movies releasing in {} are: ".format(industry_value)
card_text = speech_text + '\n'
for i, item in enumerate(response['results'][:5]):
movies.append(item['title'])
card_movies.append(str(i+1) + '. ' + item['title'])
speech_text += ', '.join(movies).replace('&', 'and')
card_text += '\n'.join(card_movies)
if len(response['results']) > 5:
speech_text += ", and many more."
card_text += ", and many more."
else:
speech_text = "There are no movies releasing on {}".format(date_value)
handler_input.response_builder.speak(speech_text).set_card(
SimpleCard("Movie Releases", card_text)).set_should_end_session(True)
else:
speech_text = "I am not sure what you said. "
reprompt = "You can ask by saying, movies releasing this week in Hollywood."
handler_input.response_builder.speak(speech_text).ask(reprompt)
return handler_input.response_builder.response
sb.add_request_handler(LaunchRequestHandler())
sb.add_request_handler(HelpIntentHandler())
sb.add_request_handler(CancelOrStopIntentHandler())
sb.add_request_handler(FallbackIntentHandler())
sb.add_request_handler(SessionEndedRequestHandler())
# Custom
sb.add_request_handler(GetUpcomingMoviesHandler())
sb.add_request_handler(GetMovieReleasesHandler())
sb.add_exception_handler(CatchAllExceptionHandler())
handler = sb.lambda_handler()