/
app.py
58 lines (52 loc) · 1.86 KB
/
app.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
import re
from flask import Flask
from flask import render_template
from flask import url_for
from flask import request
import urllib
from twilio import twiml
from requests import get as rget
from requests.compat import urlencode
import os
import random
api_key = os.environ.get('DPLA_API_KEY', None)
# Declare and configure application
app = Flask(__name__, static_url_path='/static')
app.config.from_pyfile('local_settings.py')
@app.route('/')
def hello():
r = twiml.Response()
r.say("Welcome to Dial a D P L A!")
r.say("Today we are providing access to Kentucky Digital Library's Claude Sullivan audio recordings.")
with r.gather(numDigits=4, action="lookup", method="POST") as g:
g.say("Please enter a four digit year of recording followed by the pound key.")
return str(r)
@app.route('/lookup', methods=['POST'])
def obj():
year = request.values.get('Digits', None)
r = twiml.Response()
params = {
"sourceResource.type": "sound",
"provider.name": "Kentucky Digital Library",
"sourceResource.date.begin": str(year),
"api_key": api_key,
"page_size": 100
}
url = "http://api.dp.la/v2/items/?%s" % urllib.urlencode(params)
results = rget(url).json()
if len(results['docs']) < 1:
r.say("Sorry, nothing matched your query.")
else:
if results['count'] > params['page_size']:
upper_bound = params['page_size'] - 1
else:
upper_bound = results['count'] - 1
index = random.randint(0, upper_bound)
item = results['docs'][index]
stream = item['object']
stream = stream.replace("_tb.mp3", ".mp3")
phrase = "You are about to listen to: " + item['sourceResource']['title'] + ". "
phrase += "This item is from " + item['dataProvider'] + "."
r.say(phrase)
r.play(stream)
return str(r)