-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.py
69 lines (51 loc) · 1.69 KB
/
api.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
from flask import Flask
from flask_restful import Resource, Api, reqparse
from gevent.pywsgi import WSGIServer
import sqlite3
import asyncio
def defaultGet(db, cargs):
con = sqlite3.connect('data/music.db')
cur = con.cursor()
parser = reqparse.RequestParser()
for i in cargs:
parser.add_argument(i)
args = parser.parse_args()
existing_args = {}
for key, value in args.items():
if value and value.strip():
existing_args[key] = value
rq = f'SELECT * from {db}'
if existing_args != {}:
rq += ' where'
count = 1
for key, value in existing_args.items():
rq += f" {key} = '{value}'"
if len(existing_args) > 1 and count < len(existing_args):
rq += ' and'
count += 1
rq_result = cur.execute(rq).fetchall()
con.close()
result = []
for i in rq_result:
data = {}
for j in range(len(cargs)):
data[cargs[j]] = i[j]
result.append(data)
return result
class Songs(Resource):
def get(self):
return defaultGet('songs', ['song_id', 'song_name', 'artist_name', 'album_name', 'genre', 'date', 'song_file_name', 'sample_rate', 'length'])
class Artists(Resource):
def get(self):
return defaultGet('artists', ['artist_id', 'artist_name'])
class Albums(Resource):
def get(self):
return defaultGet('albums', ['album_id', 'album_name', 'artist_name'])
app = Flask(__name__)
api = Api(app)
api.add_resource(Songs, '/api/songs')
api.add_resource(Artists, '/api/artists')
api.add_resource(Albums, '/api/albums')
def startAPI():
http_server = WSGIServer(('', 5000), app)
http_server.serve_forever()