forked from tooxie/shiva-server
/
resources.py
613 lines (455 loc) · 16.3 KB
/
resources.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# -*- coding: utf-8 -*-
from datetime import datetime
import logging
import urllib2
from flask import request, Response, current_app as app, g
from flask.ext.restful import abort, fields, marshal, Resource
from lxml import etree
import requests
from shiva import get_version, get_contributors
from shiva.fields import (Boolean, DownloadURI, ForeignKeyField, InstanceURI,
ManyToManyField, StreamURI)
from shiva.models import Artist, Album, Track, Lyrics
from shiva.lyrics import get_lyrics
logger = logging.getLogger(__name__)
DEFAULT_ALBUM_COVER = ('http://wortraub.com/wp-content/uploads/2012/07/'
'Vinyl_Close_Up.jpg')
DEFAULT_ARTIST_IMAGE = 'http://www.super8duncan.com/images/band_silhouette.jpg'
class JSONResponse(Response):
"""
A subclass of flask.Response that sets the Content-Type header by default
to "application/json"
"""
def __init__(self, status=200, **kwargs):
params = {
'headers': [],
'mimetype': 'application/json',
'response': '',
'status': status,
}
params.update(kwargs)
super(JSONResponse, self).__init__(**params)
def full_tree():
""" Checks the GET parameters to see if a full tree was requested """
arg = request.args.get('fulltree')
return (arg and arg not in ('false', '0'))
def paginate(queryset):
"""
Function that receives a queryset and paginates it based on the GET
parameters.
"""
try:
page_size = int(request.args.get('page_size', 0))
except ValueError:
page_size = 0
try:
page_number = int(request.args.get('page', 0))
except ValueError:
page_number = 0
if not page_size or not page_number:
return queryset
total = queryset.count()
if total < page_size:
return queryset
limit = page_size
offset = page_size * (page_number - 1)
return queryset.limit(limit).offset(offset)
class ArtistResource(Resource):
"""
"""
route_base = 'artists'
resource_fields = {
'id': fields.Integer(attribute='pk'),
'name': fields.String,
'slug': fields.String,
'uri': InstanceURI('artist'),
'download_uri': DownloadURI('artist'),
'image': fields.String(default=DEFAULT_ARTIST_IMAGE),
'events_uri': fields.String(attribute='events'),
}
def get(self, artist_id=None, artist_slug=None):
if not artist_id and not artist_slug:
return list(self.get_all())
if not artist_id and artist_slug:
artist = self.get_by_slug(artist_slug)
else:
artist = self.get_one(artist_id)
if full_tree():
return self.get_full_tree(artist)
return marshal(artist, self.resource_fields)
def get_all(self):
for artist in paginate(Artist.query.order_by(Artist.name)):
yield marshal(artist, self.resource_fields)
def get_one(self, artist_id):
artist = Artist.query.get(artist_id)
if not artist:
abort(404)
return artist
def get_by_slug(self, artist_slug):
artist = Artist.query.filter_by(slug=artist_slug).first()
if not artist:
abort(404)
return artist
def get_full_tree(self, artist):
_artist = marshal(artist, self.resource_fields)
_artist['albums'] = []
albums = AlbumResource()
for album in artist.albums:
_artist['albums'].append(albums.get_full_tree(album))
return _artist
def delete(self, artist_id=None):
if not artist_id:
return JSONResponse(405)
artist = Artist.query.get(artist_id)
if not artist:
abort(404)
g.db.session.delete(artist)
g.db.session.commit()
return {}
class AlbumResource(Resource):
"""
"""
route_base = 'albums'
resource_fields = {
'id': fields.Integer(attribute='pk'),
'name': fields.String,
'slug': fields.String,
'year': fields.Integer,
'uri': InstanceURI('album'),
'artists': ManyToManyField(Artist, {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('artist'),
}),
'download_uri': DownloadURI('album'),
'cover': fields.String(default=DEFAULT_ALBUM_COVER),
}
def get(self, album_id=None, album_slug=None):
if not album_id and not album_slug:
return list(self.get_many())
if not album_id and album_slug:
album = self.get_by_slug(album_slug)
else:
album = self.get_one(album_id)
if full_tree():
return self.get_full_tree(album)
return marshal(album, self.resource_fields)
def get_many(self):
artist_pk = request.args.get('artist')
if artist_pk:
albums = Album.query.join(Album.artists).filter(
Artist.pk == artist_pk)
else:
albums = Album.query
queryset = albums.order_by(Album.year, Album.name, Album.pk)
for album in paginate(queryset):
yield marshal(album, self.resource_fields)
def get_one(self, album_id):
album = Album.query.get(album_id)
if not album:
abort(404)
return album
def get_by_slug(self, album_slug):
album = Album.query.filter_by(slug=album_slug).first()
if not album:
abort(404)
return album
def get_full_tree(self, album):
_album = marshal(album, self.resource_fields)
_album['tracks'] = []
tracks = TracksResource()
for track in album.tracks.order_by('number', 'title'):
_album['tracks'].append(tracks.get_full_tree(track))
return _album
def delete(self, album_id=None):
if not album_id:
return JSONResponse(405)
album = Album.query.get(album_id)
if not album:
abort(404)
g.db.session.delete(album)
g.db.session.commit()
return {}
class TracksResource(Resource):
"""
"""
route_base = 'tracks'
resource_fields = {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('track'),
'stream_uri': StreamURI,
'bitrate': fields.Integer,
'length': fields.Integer,
'title': fields.String,
'slug': fields.String,
'artist': ForeignKeyField(Artist, {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('artist'),
}),
'album': ForeignKeyField(Album, {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('album'),
}),
'number': fields.Integer,
}
def get(self, track_id=None, track_slug=None):
if not track_id and not track_slug:
return list(self.get_many())
if not track_id and track_slug:
track = self.get_by_slug(track_slug)
else:
track = self.get_one(track_id)
if full_tree():
return self.get_full_tree(track, include_scraped=True)
return marshal(track, self.resource_fields)
# TODO: Pagination
def get_many(self):
album_pk = request.args.get('album')
artist_pk = request.args.get('artist')
if album_pk:
album_pk = None if album_pk == 'null' else album_pk
tracks = Track.query.filter_by(album_pk=album_pk)
elif artist_pk:
tracks = Track.query.filter(Track.artist_pk == artist_pk)
else:
tracks = Track.query
queryset = tracks.order_by(Track.album_pk, Track.number, Track.pk)
for track in paginate(queryset):
yield marshal(track, self.resource_fields)
def get_one(self, track_id):
track = Track.query.get(track_id)
if not track:
abort(404)
return track
def get_by_slug(self, track_slug):
track = Track.query.filter_by(slug=track_slug).first()
if not track:
abort(404)
return track
def get_full_tree(self, track, include_scraped=False):
"""
Retrives the full tree for a track. If the include_scraped option is
not set then a normal track structure will be retrieved. If its set
external resources that need to be scraped, like lyrics, will also be
included.
This is disabled by default to avois DoS'ing lyrics' websites when
requesting many tracks at once.
"""
_track = marshal(track, self.resource_fields)
if include_scraped:
lyrics = LyricsResource()
_track['lyrics'] = lyrics.get_for(track)
# tabs = TabsResource()
# _track['tabs'] = tabs.get()
return _track
def delete(self, track_id=None):
if not track_id:
return JSONResponse(405)
track = Track.query.get(track_id)
if not track:
abort(404)
g.db.session.delete(track)
g.db.session.commit()
return {}
class LyricsResource(Resource):
"""
"""
resource_fields = {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('lyrics'),
'text': fields.String,
'source_uri': fields.String(attribute='source'),
'track': ForeignKeyField(Track, {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('track'),
}),
}
def get(self, track_id=None, track_slug=None):
if not track_id and not track_slug:
abort(404)
if not track_id and track_slug:
track = Track.query.filter_by(slug=track_slug).first()
else:
track = Track.query.get(track_id)
return self.get_for(track)
def get_for(self, track):
if track.lyrics:
return marshal(track.lyrics, self.resource_fields)
try:
lyrics = get_lyrics(track)
except Exception, e:
logging.debug(e)
lyrics = None
if not lyrics:
abort(404)
return marshal(lyrics, self.resource_fields)
def post(self, track_id):
text = request.form.get('text', None)
if not text:
return JSONResponse(400)
track = Track.query.get(track_id)
lyric = Lyrics(track=track, text=text)
g.db.session.add(lyric)
g.db.commit()
return JSONResponse(200)
def delete(self, track_id):
track = Track.query.get(track_id)
g.db.session.delete(track.lyrics)
g.db.session.commit()
return JSONResponse(200)
class ShowsResource(Resource):
"""
"""
resource_fields = {
'id': fields.String,
'artists': ManyToManyField(Artist, {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI('artist'),
}),
'other_artists': fields.List(fields.Raw),
'datetime': fields.DateTime,
'title': fields.String,
'tickets_left': Boolean,
'venue': fields.Nested({
'latitude': fields.String,
'longitude': fields.String,
'name': fields.String,
}),
}
def get(self, artist_id=None, artist_slug=None):
if not artist_id and not artist_slug:
return JSONResponse(404)
if not artist_id and artist_slug:
artist = Artist.query.filter_by(slug=artist_slug).first()
else:
artist = Artist.query.get(artist_id)
if not artist:
return JSONResponse(404)
latitude = request.args.get('latitude')
longitude = request.args.get('longitude')
country = request.args.get('country')
city = request.args.get('city')
if latitude and longitude:
location = (latitude, longitude)
elif country and city:
location = (city, country)
else:
location = ()
response = self.fetch(artist.name, location)
return list(response) if response else []
def fetch(self, artist_name, location):
bit_uri = ('http://api.bandsintown.com/artists/%(artist)s/events'
'/search?format=json&app_id=%(app_id)s&api_version=2.0')
bit_uri = bit_uri % {
'artist': urllib2.quote(artist_name),
'app_id': app.config['BANDSINTOWN_APP_ID'],
}
_location = urllib2.quote('%s, %s' % location) if location else \
'use_geoip'
bit_uri = '&'.join((bit_uri, '='.join(('location', _location))))
logger.info(bit_uri)
try:
response = requests.get(bit_uri)
except requests.exceptions.RequestException:
return
for event in response.json():
yield marshal(ShowModel(artist_name, event), self.resource_fields)
class ShowModel(object):
"""
Mock model that encapsulates the show logic for converting a JSON structure
into an object.
"""
def __init__(self, artist, json):
self.json = json
self.id = json['id']
self.artists, self.other_artists = self.split_artists(json['artists'])
self.datetime = self.to_datetime(json['datetime'])
self.title = json['title']
self.tickets_left = (json['ticket_status'] == 'available')
self.venue = json['venue']
def split_artists(self, json):
if not len(json):
return ([], [])
elif len(json) == 1:
artist = Artist.query.filter_by(name=json[0]['name']).first()
return ([artist], [])
my_artists = []
other_artists = []
for artist_dict in json:
artist = Artist.query.filter_by(name=artist_dict['name'])
if artist.count():
my_artists.append(artist.first())
else:
del artist_dict['thumb_url']
other_artists.append(artist_dict)
return (my_artists, other_artists)
def to_datetime(self, timestamp):
return datetime.strptime(timestamp, '%Y-%m-%dT%H:%M:%S')
def get_mbid(self, artist):
mb_uri = 'http://musicbrainz.org/ws/2/artist?query=%(artist)s' % {
'artist': urllib2.quote(artist)
}
logger.info(mb_uri)
response = requests.get(mb_uri)
mb_xml = etree.fromstring(response.text)
# /root/artist-list/artist.id
artist_list = mb_xml.getchildren()[0].getchildren()
if artist_list:
return artist_list[0].get('id')
return None
def __getitem__(self, key):
return getattr(self, key, None)
class RandomResource(Resource):
"""
Retrieves a random instance of a specified resource.
"""
def get(self, resource_name):
get_resource = getattr(self, 'get_%s' % resource_name)
if get_resource and callable(get_resource):
return get_resource()
return JSONResponse(404)
def get_random_for(self, model, resource_name):
from random import random
top = model.query.count()
model_id = int(random() * top)
instance = model.query.get(model_id)
resource_fields = {
'id': fields.Integer(attribute='pk'),
'uri': InstanceURI(resource_name),
}
return marshal(instance, resource_fields)
def get_track(self):
return self.get_random_for(Track, 'track')
def get_album(self):
return self.get_random_for(Album, 'album')
def get_artist(self):
return self.get_random_for(Artist, 'artist')
class ClientResource(Resource):
def get(self):
clients = [
{
'name': 'Shiva-Client',
'uri': 'https://github.com/tooxie/shiva-client',
'authors': [
u'Alvaro Mouriño <alvaro@mourino.net>',
],
},
{
'name': 'Shiva4J',
'uri': 'https://github.com/instant-solutions/shiva4j',
'authors': [
u'David Andinger <david.andlinger@instant-it.at>',
u'Max Holzleitner <max.holzleitner@instant-it.at>',
],
},
]
return clients
class AboutResource(Resource):
def get(self):
info = {
'name': 'Shiva',
'version': get_version(),
'author': u'Alvaro Mouriño <alvaro@mourino.net>',
'uri': 'https://github.com/tooxie/shiva-server',
'contributors': get_contributors(),
}
return info