-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkutx_playlist.py
95 lines (79 loc) · 2.67 KB
/
kutx_playlist.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
import json
import requests
def lambda_handler(event, context):
print json.dumps(event)
print 'track info is:\n{}'.format(json.dumps(track_info()))
print "checking request type..."
if event['request']['type'] == 'IntentRequest':
print "request type was IntentRequest"
return handle_intent(event.get('request'), event.get('session'))
def handle_intent(request, session):
intent = request.get('intent')
intent_name = intent.get('name')
print "handling intent", intent_name
if intent_name == 'get_current_track':
return handle_intent_get(request, session)
def handle_intent_get(request, session):
print "handing get intent..."
track = track_info()
response_string = ''
if track:
print "we got track info."
response_string = "this is {}, by artist {}.".format(
track.get('track'),
track.get('artist'))
else:
print "there was no track info available."
response_string = "could not get track info."
print "caling build_response..."
return build_response(build_speechlet(response_string), session)
def build_response(speechlet, session):
print "build_response called..."
response = {
"version": "1.0",
"sessionAttributes": session,
"response": speechlet
}
print json.dumps(response, indent=2)
return response
def build_speechlet(speech):
print "build_speechlet called..."
speech_response = {
"outputSpeech": {
"type": "PlainText",
"text": speech
},
"card": {
"type": "Simple",
"title": "kutx song",
"content": speech
},
"shouldEndSession": True
}
return speech_response
def now_playing(station_ucs='50ef24ebe1c8a1369593d032'):
url = "https://api.composer.nprstations.org/v1/widget/{}/now".format(
station_ucs)
r = requests.get(url, params={'format': 'json'})
if r.status_code == 200:
return r.json()
else:
r.raise_for_status()
def track_info():
try:
on_now = now_playing().get('onNow', {})
track = on_now.get('song', {})
if track:
track_name = track.get('trackName')
artist_name = track.get('artistName')
print 'track is', track.get('trackName'), '-', \
track.get('artistName') # onNow.trackName
return {'artist': artist_name, 'track': track_name}
else:
print "there was no track info."
return {}
except requests.exceptions.HTTPError as oops:
print "sorry, there was an error:", oops
return None
if __name__ == '__main__':
track_info()