summaryrefslogtreecommitdiff
path: root/mach2.py
blob: 479c0ab49f7c538592640a572d8c612e3a494b1a (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
import configparser
import json

from flask import Flask

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


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


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


@app.route("/search/album/<albumname>")
def album_search(albumname):
    albums = []
    for album in Album.search(name=albumname):
        albums.append(album.__dict__)

    return json.dumps(albums)


@app.route("/search/artist/<artistname>")
def artist_search(artistname):
    artists = []
    for artist in Artist.search(name=artistname):
        artists.append(artist.__dict__)

    return json.dumps(artists)


@app.route("/search/track/<trackname>")
def track_search(trackname):
    tracks = []
    for track in Track.search(name=trackname):
        tracks.append(track.__dict__)

    return json.dumps(tracks)


@app.route("/artist/<int:artist_id>/tracks")
def artist_tracks(artist_id):
    tracks = []
    artist = Artist(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(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(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(album_id)

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

    return json.dumps(artists)


if __name__ == "__main__":
    config = configparser.ConfigParser()
    config.read("mach2.ini")

    app.run(debug=config["DEFAULT"]["debug"])