From 2e1901470834da315a1d5a80345277a80283a3ba Mon Sep 17 00:00:00 2001 From: Michaƫl Ball Date: Wed, 24 Dec 2014 17:52:19 +0000 Subject: Better searching, plus serve files --- mach2.py | 57 ++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/mach2.py b/mach2.py index 479c0ab..f631a79 100644 --- a/mach2.py +++ b/mach2.py @@ -1,7 +1,9 @@ import configparser import json +import mimetypes from flask import Flask +from flask import Response from models.album import Album from models.artist import Artist @@ -17,28 +19,31 @@ def hello(): return "Hello world!" -@app.route("/search/album/") -def album_search(albumname): +@app.route("/search/album/") +def album_search(album_name): albums = [] - for album in Album.search(name=albumname): + for album in Album.search(name={'data': album_name, 'operator': 'LIKE'}): albums.append(album.__dict__) return json.dumps(albums) -@app.route("/search/artist/") -def artist_search(artistname): +@app.route("/search/artist/") +def artist_search(artist_name): artists = [] - for artist in Artist.search(name=artistname): + for artist in Artist.search(name={ + 'data': artist_name, + 'operator': 'LIKE' + }): artists.append(artist.__dict__) return json.dumps(artists) -@app.route("/search/track/") -def track_search(trackname): +@app.route("/search/track/") +def track_search(track_name): tracks = [] - for track in Track.search(name=trackname): + for track in Track.search(name={'data': track_name, 'operator': 'LIKE'}): tracks.append(track.__dict__) return json.dumps(tracks) @@ -47,7 +52,7 @@ def track_search(trackname): @app.route("/artist//tracks") def artist_tracks(artist_id): tracks = [] - artist = Artist(artist_id) + artist = Artist(id=artist_id) for track in artist.tracks: tracks.append(track.__dict__) @@ -58,7 +63,7 @@ def artist_tracks(artist_id): @app.route("/artist//albums") def artist_albums(artist_id): albums = [] - artist = Artist(artist_id) + artist = Artist(id=artist_id) for album in artist.albums: albums.append(album.__dict__) @@ -69,7 +74,7 @@ def artist_albums(artist_id): @app.route("/album//tracks") def album_tracks(album_id): tracks = [] - album = Album(album_id) + album = Album(id=album_id) for track in album.tracks: tracks.append(track.__dict__) @@ -80,7 +85,7 @@ def album_tracks(album_id): @app.route("/album//artists") def album_artists(album_id): artists = [] - album = Album(album_id) + album = Album(id=album_id) for artist in album.artists: artists.append(artist.__dict__) @@ -88,6 +93,32 @@ def album_artists(album_id): return json.dumps(artists) +@app.route("/track/") +def track(track_id): + def stream_file(filename, chunksize=8192): + with open(filename, "rb") as f: + while True: + chunk = f.read(chunksize) + if chunk: + yield chunk + else: + break + + local_track = Track(track_id) + + mime_string = "application/octet-stream" + + mime = mimetypes.guess_type(local_track.filename) + if mime[0]: + mime_string = mime[0] + + resp = Response(stream_file(local_track.filename), mimetype=mime_string) + + if mime[1]: + resp.headers["Content-Encoding"] = mime[1] + + return resp + if __name__ == "__main__": config = configparser.ConfigParser() config.read("mach2.ini") -- cgit v1.2.3