summaryrefslogtreecommitdiff
path: root/mach2.py
blob: dfe7452b06e40ffd25ceb5015a2d5e4687e9051a (plain)
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
import configparser
import json
import mimetypes
import sqlite3

from flask import Flask, Response, current_app, g, redirect, render_template
from flask import request, url_for
from flask.ext.compress import Compress
from flask.ext.login import LoginManager, current_user, login_required
from flask.ext.login import login_user, logout_user


from models.album import Album
from models.artist import Artist
from models.track import Track


DATABASE = "app.db"

app = Flask(__name__)
app.config.from_object(__name__)

config = configparser.ConfigParser()
config.read("mach2.ini")

app.config["DEBUG"] = config["DEFAULT"]["debug"]

login_manager = LoginManager()
login_manager.login_view = "login"
login_manager.session_protection = "strong"


def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = sqlite3.connect(DATABASE)
        db.row_factory = sqlite3.Row
        setattr(g, "_database", db)

    return db


@app.teardown_appcontext
def close_connection(exception):
    db = getattr(g, "_database", None)
    if db is not None:
        db.close()


def query_db(query, args=(), one=False):
    cur = get_db().execute(query, args)
    rv = cur.fetchall()
    cur.close()
    return (rv[0] if rv else None) if one else rv


@app.route("/")
def hello():
    return "Hello world!"


@app.route("/search/album/<album_name>")
def album_search(album_name):
    albums = []
    for album in Album.search(name={'data': album_name, 'operator': 'LIKE'}):
        albums.append(album.__dict__)

    return json.dumps(albums)


@app.route("/search/artist/<artist_name>")
def artist_search(artist_name):
    artists = []
    for artist in Artist.search(name={
                                'data': artist_name,
                                'operator': 'LIKE'
                                }):
        artists.append(artist.__dict__)

    return json.dumps(artists)


@app.route("/search/track/<track_name>")
def track_search(track_name):
    tracks = []
    for track in Track.search(name={'data': track_name, 'operator': 'LIKE'}):
        tracks.append(track.__dict__)

    return json.dumps(tracks)


@app.route("/artist/<int:artist_id>/tracks")
def artist_tracks(artist_id):
    tracks = []
    artist = Artist(id=artist_id)

    for track in artist.tracks:
        tracks.append(track.__dict__)

    return json.dumps(tracks)


@app.route("/artist/<int:artist_id>/albums")
def artist_albums(artist_id):
    albums = []
    artist = Artist(id=artist_id)

    for album in artist.albums:
        albums.append(album.__dict__)

    return json.dumps(albums)


@app.route("/album/<int:album_id>/tracks")
def album_tracks(album_id):
    tracks = []
    album = Album(id=album_id)

    for track in album.tracks:
        tracks.append(track.__dict__)

    return json.dumps(tracks)


@app.route("/album/<int:album_id>/artists")
def album_artists(album_id):
    artists = []
    album = Album(id=album_id)

    for artist in album.artists:
        artists.append(artist.__dict__)

    return json.dumps(artists)


@app.route("/track/<int:track_id>")
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")

    login_manager.init_app(app)
    app.run()