-
Notifications
You must be signed in to change notification settings - Fork 0
/
WeatherOrMovies.py
157 lines (135 loc) · 6.13 KB
/
WeatherOrMovies.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
import requests
import json
import random
weatherKey = "0e24e886caae46e0bb8153748172504"
moviesKey = "a2b094bc91767343355b1f3a76678420"
def getResults(class_type, request_parameter):
if class_type == "weather":
key = weatherKey
else:
key = moviesKey
tempClass = WeatherOrMovies(class_type, request_parameter, key)
result = tempClass.executeApi()
return result
class WeatherOrMovies:
def __init__(self, _intentType, _requestParameter, _key):
self.intentType = _intentType
self.requestParameter = _requestParameter
self.key = _key
self.genres = {
"action": 28,
"adventure": 12,
"animation": 16,
"comedy": 35,
"crime": 80,
"drama": 18,
"horror": 27,
"mystery": 9648,
"romance": 10749
}
self.genre_ids = {
28: "action",
12: "adventure",
16: "animation",
35: "comedy",
80: "crime",
18: "drama",
27: "horror",
9648: "mystrey",
10749: "romance"
}
def executeApi(self):
if (self.requestParameter is None):
return_data = {}
error = {}
error["message"] = "The parameter passed is Null"
return_data["status_code"] = 400
return_data["error"] = error
raise ValueError(json.dumps(return_data))
self.requestParameter = self.requestParameter.replace(" ", "%20")
if (self.intentType == "movie"):
getTopRated = "top rated".replace(" ", "%20")
getPopular = "popular"
compareString = str(self.requestParameter)
if (compareString == getTopRated or compareString == getPopular):
self.requestParameter = self.requestParameter.replace("%20", "_")
url = "https://api.themoviedb.org/3/movie/" + str(
self.requestParameter) + "?language=en-US&page=1&api_key=" + str(self.key)
data = self._call(url)
results = data['results']
result = random.choice(results)
return self._formatMovieResponse(result)
elif (compareString.find("genre:") != -1):
genre = compareString[compareString.index(":") + 1:]
if genre in self.genres:
url = "https://api.themoviedb.org/3/genre/" + str(self.genres[
genre]) + "/movies?api_key=" + self.key + "&language=en-US&include_adult=false&sort_by=created_at.asc"
data = self._call(url)
results = data['results']
result = random.choice(results)
return self._formatMovieResponse(result)
else:
self.requestParameter = getPopular
return self.executeApi()
else:
url = "https://api.themoviedb.org/3/search/multi?api_key=" + str(
self.key) + "&language=en-US&query=" + str(self.requestParameter) + "&page=1&include_adult=false"
data = self._call(url)
results = data['results']
if len(results) == 0:
return "No Movie Found"
# return_data = {}
# error = {}
# error["message"] = "No Movie Found"
# return_data["status_code"] = 400
# return_data["error"] = error
# raise ValueError(json.dumps(return_data))
return self._formatMovieResponse(results[0])
elif (self.intentType == "weather"):
url = "http://api.apixu.com/v1/current.json?key=" + str(self.key) + "&q=" + str(self.requestParameter)
data = self._call(url)
return_data = {}
try:
return_data['Temperature In Celcius'] = data['current']['temp_c']
return_data['Temperature In Fahrenheit'] = data['current']['temp_f']
return_data['Weather Condition'] = data['current']['condition']['text']
return_data['Feels Like In Celcius'] = data['current']['feelslike_c']
return_data['Feels Like In Fahrenheit'] = data['current']['feelslike_f']
return_data['Humidity'] = data['current']['humidity']
return json.dumps(return_data)
except ValueError:
return "No Matching City Was Found"
else:
raise ValueError("This module does not deal with this intent type")
def _formatMovieResponse(self, result):
return_data = {}
return_data['Original Title'] = result['original_title']
return_data['Overview'] = result['overview']
if (result['poster_path'] is not None):
return_data['poster'] = " http://image.tmdb.org/t/p/w185/" + result['poster_path']
if ('media_type' in result.keys()):
return_data['Media Type'] = result['media_type']
return_data['rating'] = result['vote_average']
movieID = result['id']
getVideoURL = "https://api.themoviedb.org/3/movie/" + str(movieID) + "/videos?language=en-US&api_key=" + str(
self.key)
videoData = self._call(getVideoURL)
if (len(videoData['results']) > 0):
videoResult = videoData['results'][0]
if (videoResult['site'] == "YouTube"):
return_data['Trailer Link'] = "https://www.youtube.com/watch?v=" + videoResult['key']
return_data["genres"] = []
for genre_id in result["genre_ids"]:
if genre_id in self.genre_ids:
return_data["genres"].append(self.genre_ids[genre_id])
return json.dumps(return_data)
def _call(self, url):
response = requests.get(url)
data = response.json()
if (response.status_code != 200):
print response.json()
result = {}
result['error'] = data['error']
result['status_code'] = response.status_code
raise ValueError(json.dumps(result))
return data