-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_config.py
185 lines (161 loc) · 6.79 KB
/
route_config.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
from flask import Flask, request
import json
from API_Calls import get_recommendations, get_playlist, get_song, get_artist, get_album, remove_und
import os
# app reference
app = Flask(__name__)
prev_recommendation = {'prev_artist': None,
'prev_song': None}
@app.before_request
def before_request():
try:
if "queryResult" in json.loads(request.data):
if "parameters" in json.loads(request.data)["queryResult"]:
if "type" in json.loads(request.data)["queryResult"]["parameters"]:
print("Recommending: "+json.loads(request.data)["queryResult"]["parameters"]["type"])
else:
print("no type")
except json.decoder.JSONDecodeError:
print("no type")
@app.route("/")
def home_view():
return "<h1>Welcome to Tune Bot</h1>"
@app.route('/api/recommendation')
def recommendations_default():
return "Welcome to tunebot backend. Make a request."
@app.route('/api/recommendation', methods=['POST'])
def recommendation():
req_data = json.loads(request.data)
if "type" in json.loads(request.data)["queryResult"]["parameters"]:
if req_data["queryResult"]["parameters"]["type"] == "song":
return base_recommendation(req_data)
if req_data["queryResult"]["parameters"]["type"] == "artist":
return artist_recommendation(req_data)
else:
return {
"fulfillmentMessages": [
{
"text": {
"text": [
"Bot Failure Type"
]
}
}
]
}
def base_recommendation(req_data):
genre_list = []
track_list = []
artist_list = []
response = None
try:
for genre in req_data["queryResult"]["parameters"]["music-genre"]:
genre_list.append(genre)
for artist in req_data["queryResult"]["parameters"]["music-artist"]:
artist_list.append(get_artist(artist, limit=1)[0]['id'])
for track in req_data["queryResult"]["parameters"]["song-name"]:
track_list.append(get_song(track, limit=1)[0]['id'])
print("Successful Recommendation")
response = get_recommendations(genres=genre_list, artists=artist_list, tracks=track_list)
except TypeError as error: # depreciated
print("Invalid Genre Input, depreciated")
print(error)
response = get_recommendations(genres=[], artists=artist_list, tracks=track_list)
except Exception as error:
print("Total failure")
print(error)
return {
"fulfillmentMessages": [
{
"text": {
"text": [
"Bot Failure Song"
]
}
}
]
}
if len(req_data["queryResult"]["parameters"]["song-name"]) > 0:
print("Track Input:" + req_data["queryResult"]["parameters"]["song-name"][0])
if len(req_data["queryResult"]["parameters"]["music-genre"]) > 0:
print("Genre Input:" + req_data["queryResult"]["parameters"]["music-genre"][0])
if len(req_data["queryResult"]["parameters"]["music-artist"]) > 0:
print("Artist Input:" + req_data["queryResult"]["parameters"]["music-artist"][0])
response = remove_und(response,
artists=req_data["queryResult"]["parameters"]["music-artist"],
tracks=track_list)
print("Track recommendation:" + response['tracks'][0]['name'])
print("Artist recommendation:" + response['tracks'][0]['artists'][0]['name'])
prev_recommendation['prev_song'] = response['tracks'][0]['name']
prev_recommendation['prev_artist'] = response['tracks'][0]['artists'][0]['name']
return {
"fulfillmentMessages": [
{
"text": {
"text": [
"You should try listening to " + response['tracks'][0]['name'] + " by " +
response['tracks'][0]['artists'][0]['name'] + ". Would you like another music recommendation?"
]
}
}
]
}
def artist_recommendation(req_data):
genre_list = []
track_list = []
artist_list = []
response = None
try:
for genre in req_data["queryResult"]["parameters"]["music-genre"]:
genre_list.append(genre)
for artist in req_data["queryResult"]["parameters"]["music-artist"]:
artist_list.append(get_artist(artist, limit=1)[0]['id'])
for track in req_data["queryResult"]["parameters"]["song-name"]:
track_list.append(get_song(track, limit=1)[0]['id'])
print("Successful Recommendation")
response = get_recommendations(genres=genre_list, artists=artist_list, tracks=track_list)
except TypeError as error: # depreciated
print("Invalid Genre Input, depreciated")
print(error)
response = get_recommendations(genres=[], artists=artist_list, tracks=track_list)
except Exception as error:
print("Total failure")
print(error)
response = get_recommendations(genres=[], artists=[get_artist("Katy Perry", limit=1)[0]['id']],
tracks=[get_song("California Girls", limit=1)[0]['id']])
return {
"fulfillmentMessages": [
{
"text": {
"text": [
"Bot Failure Artist"
]
}
}
]
}
if len(req_data["queryResult"]["parameters"]["song-name"]) > 0:
print("Track Input:" + req_data["queryResult"]["parameters"]["song-name"][0])
if len(req_data["queryResult"]["parameters"]["music-genre"]) > 0:
print("Genre Input:" + req_data["queryResult"]["parameters"]["music-genre"][0])
if len(req_data["queryResult"]["parameters"]["music-artist"]) > 0:
print("Artist Input:" + req_data["queryResult"]["parameters"]["music-artist"][0])
response = remove_und(response,
tracks=[prev_recommendation['prev_song']],
artists=[prev_recommendation['prev_artist']] + artist_list)
print("Track recommendation:" + response['tracks'][0]['name'])
print("Artist recommendation:" + response['tracks'][0]['artists'][0]['name'])
prev_recommendation['prev_song'] = None
prev_recommendation['prev_artist'] = response['tracks'][0]['artists'][0]['name']
return {
"fulfillmentMessages": [
{
"text": {
"text": [
"You should try listening to " +
response['tracks'][0]['artists'][0]['name'] + ". Would you like another artist recommendation?"
]
}
}
]
}