/
server.py
executable file
·67 lines (51 loc) · 1.6 KB
/
server.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
#!/usr/bin/python
import os
import logging
from bottle import Bottle, request, response, run, static_file
import youtube
app = Bottle()
youtube = youtube.YouTube()
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_URL = os.path.join(BASE_DIR, 'static')
@app.route('/')
def index():
return static_file('index.html', root=STATIC_URL)
@app.route('/static/<filename:path>')
def static(filename):
return static_file(filename, root=STATIC_URL)
@app.route('/search')
def search():
params = request.query
lat = params.get('lat')
lng = params.get('lng')
radius = params.get('radius')
# quick validation
if not all([lat, lng, radius]):
response.status = 400
return {'error': 'Check geo params.'}
location = "%s,%s" % (lat, lng)
locationRadius = "%skm" % radius
results = youtube.search(
part=params.get('part', 'id'),
type=params.get('type', 'video'),
q=params.get('q'),
location=location,
locationRadius=locationRadius,
order=params.get('order', 'date'),
channelId=params.get('channelId'),
maxResults=params.get('limit', 50),
pageToken=params.get('next')
)
# extract video ids
video_ids = youtube.extract_video_ids(results)
# retrieve video results
videos = youtube.videos(
part='snippet,recordingDetails',
id=','.join(video_ids)
)
return {
'videos': videos.get('items', []),
'next': results.get('nextPageToken')
}
if __name__ == '__main__':
run(app, reloader=True, debug=True, host='localhost', port=8080)