从零开发手机app之歌曲列表接口及文件访问路由

接上篇,利用采集已经帮我们准备好了歌曲数据,接下来写一个获取歌曲列表的接口,当然还有封面及mp3文件访问的路由。

为了将项目显得稍微清晰点,我们将每个功能单独写一个蓝图,本次我们主要写歌曲列表蓝图和文件访问蓝图。

修改配置文件

我们为返回接口规定一个固定的相应格式,整个文件内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import os

MEDIA_PATH = "static/media"
IMAGE_PATH = "static/img"

#数据库配置
import pymongo
client = pymongo.MongoClient(host="127.0.0.1",port=27017)
MONGO_DB = client["aios"]

#Rest-Api
RET={
"code":0,
"msg":"",
"data":{}
}

创建蓝图

我们在项目根创建一个blue_print的专门用来存放蓝图的目录,在其中创建songs和files文件,代表以上两个路由的蓝图。

歌曲列表蓝图

主要用来返回歌曲列表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from flask import Blueprint, jsonify
import settings
# 实例化蓝图
songs = Blueprint('songs', __name__)

@songs.route("/songs", methods=['GET'])
def songs_list():
# 查询5条数据
res = list(settings.MONGO_DB.music.find().limit(5))
# 将每条数据的_id由objectid修改为str字符串
for index, item in enumerate(res):
res[index]['_id'] = str(item.get('_id'))
# 返回格式
settings.RET["code"] = 200
settings.RET["msg"] = "查询歌曲列表"
settings.RET["data"] = res
return jsonify(settings.RET)

文件访问蓝图

后期app调用我么借口会访问我们的图片和音频等静态资源,此处我们为其这些静态资源写一个路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
'''
显示文件路由
'''
from flask import Blueprint, send_file
import os

import settings

files = Blueprint('files', __name__)

# 音频路由
@files.route('/media/<file>')
def get_media(file):
media = os.path.join(settings.MEDIA_PATH, file)
return send_file(media)

# 图片路由
@files.route('/images/<file>')
def get_images(file):
image = os.path.join(settings.IMAGE_PATH, file)
return send_file(image)

绑定蓝图

我们创建一个入口主文件app_api.py,在其中注册我们刚才创建的蓝图

1
2
3
4
5
6
7
8
9
10
11
12
from flask import Flask
from blue_print import songs, files

app = Flask(__name__)

# 注册歌曲列表蓝图
app.register_blueprint(songs.songs)
# 注册文件访问蓝图
app.register_blueprint(files.files)

if __name__ == '__main__':
app.run('0.0.0.0', 9859, debug=True)

访问测试

歌曲列表

访问http://localhost:9859/songs,返回结果如下:

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
{
code: 0,
data: [
{
_id: "5da0160897fde511e5f13948",
audio: "178fea24-95ef-489b-8a6b-987babeab02a.mp3",
author: "姚智鑫",
pic: "178fea24-95ef-489b-8a6b-987babeab02a.jpg",
releaseDate: "2019-09-10",
songTimeMinutes: "02:21",
title: "野狼disco (抒情完整版)"
},
{
_id: "5da0161097fde511e5f13949",
audio: "d2672688-649f-4557-82b5-59206bf54842.mp3",
author: "张泽熙",
pic: "d2672688-649f-4557-82b5-59206bf54842.jpg",
releaseDate: "2018-08-11",
songTimeMinutes: "03:40",
title: "那个女孩"
},
{
_id: "5da0161d97fde511e5f1394a",
audio: "7ea23d21-c0db-4fd7-bfcb-33ca9f712061.mp3",
author: "汪峰",
pic: "7ea23d21-c0db-4fd7-bfcb-33ca9f712061.jpg",
releaseDate: "2016-12-30",
songTimeMinutes: "05:34",
title: "我爱你中国"
},
{
_id: "5da0162597fde511e5f1394b",
audio: "117ebccb-7dd1-4436-a82e-4000083c1789.mp3",
author: "音阙诗听&赵方婧",
pic: "117ebccb-7dd1-4436-a82e-4000083c1789.jpg",
releaseDate: "2019-06-06",
songTimeMinutes: "03:36",
title: "芒种"
},
{
_id: "5da0162f97fde511e5f1394c",
audio: "6c2b1c8f-4448-4421-b116-250ddd4bfda5.mp3",
author: "华语群星",
pic: "6c2b1c8f-4448-4421-b116-250ddd4bfda5.jpg",
releaseDate: "2019-09-29",
songTimeMinutes: "04:03",
title: "我和我的祖国"
}
],
msg: "查询歌曲列表"
}

成功访问到了歌曲列表数据,接着测试文件访问

文件访问路由

后期要在我们app上显示服务器上的图片和音频,肯定要有一个静态资源访问到的路由

访问图片

访问:http://localhost:9859/images/3227f9ba-8a2b-4686-9400-09d247b36f33.jpg

没毛病

访问音频

完美