玩具需求分析
1.语音实时通讯 - 让玩具成为孩子与父母之间沟通的桥梁 分支 建立幼儿社交圈2.积极正面幼教内容 - 让玩具能够播放 父母选定的儿歌,故事,百科,英语- 通过语音识别内容名称 玩具可以主动点播内容3.陪伴孩子聊天 - 孩子是需要表达的 5-6 阶段话痨4.解答十万个为什么 - 每一个孩子都是一本十万个为什么5.家长可以玩具的社交圈 - 我妈不让我跟你玩儿- "监视"玩具的动向
Mongo 数据结构
用户数据表 玩具数据表
幼教内容数据表
# 智能玩具 MongoDB 数据结构#### 1.App用户数据表##### 表名 : ``` Users ```##### 数据结构:```json {"_id" : ObjectId("5c9d8da3ea512d2048826260"), //自动生成ID"username" : "asdf", //用户名"password" : "962012d09b8170d912f0669f6d7d9d07", //密码"nickname" : "DragonFire", // 用户昵称"gender" : "2", // 用户性别"avatar" : "baba.jpg", // 用户头像 男=="baba.jpg" 女=="mama.jpg""bind_toys" : [ // 用户绑定的玩具ID(string)"5ca17c7aea512d26281bcb8d","5ca17f85ea512d215cd9b079"], "friend_list" : [ // 用户的通讯录列表{ // 通讯录信息"friend_id" : "5ca17c7aea512d26281bcb8d", // 好友id"friend_nick" : "背背", // 好友的昵称"friend_remark" : "臭屎蛋儿", // 好友备注"friend_avatar" : "toy.jpg", // 好友头像"friend_chat" : "5ca17c7aea512d26281bcb8c", // 私聊窗口ID 聊天数据表对应值"friend_type" : "toy" // 好友类型 },{"friend_id" : "5ca17f85ea512d215cd9b079","friend_nick" : "圆圆","friend_remark" : "小粪球儿","friend_avatar" : "toy.jpg","friend_chat" : "5ca17f85ea512d215cd9b078","friend_type" : "toy"}] } ```#### 2.玩具信息数据表##### 表名 : ``` Toys ```##### 数据结构:```json {"_id" : ObjectId("5ca17f85ea512d215cd9b079"), // 自动生成ID"toy_name" : "小粪球儿", // 玩具的昵称"baby_name" : "圆圆", // 玩具主人的昵称"device_key" : "afc59916257ae8a2b6ccdfb9fd273373", // 玩具的设备编号"avatar" : "toy.jpg", // 玩具的头像固定值 "toy.jpg""bind_user" : "5c9d8da3ea512d2048826260", // 玩具的绑定用户"friend_list" : [ // 玩具通讯录信息 { // 与Users数据表 friend_list 结构相同"friend_id" : "5c9d8da3ea512d2048826260","friend_nick" : "DragonFire","friend_remark" : "爸爸","friend_avatar" : "baba.jpg","friend_chat" : "5ca17f85ea512d215cd9b078","friend_type" : "app"},{"friend_id" : "5ca17c7aea512d26281bcb8d","friend_nick" : "臭屎蛋儿","friend_remark" : "蛋蛋的忧伤","friend_avatar" : "toy.jpg","friend_chat" : "5ca5e789ea512d2e544da015","friend_type" : "toy"}] } ```#### 3.好友请求信息数据表##### 表名: ``` Request ```##### 数据结构:```json {"_id" : ObjectId("5ca5bfbaea512d269449ed1b"), // 自动生成ID"add_user" : "5ca17c7aea512d26281bcb8d", // 发起好友申请方"toy_id" : "5ca17f85ea512d215cd9b079", // 收到好友申请方"add_type" : "toy", // 发起方的用户类型 app/toy"req_info" : "我是仇视单", // 请求信息"remark" : "园园", // 发起方对接收方的好友备注"avatar" : "toy.jpg", // 发起方的头像"nickname" : "背背", // 发起方的名称"status" : 1, // 请求状态 1同意 0未处理 2拒绝"toy_name" : "圆圆" // 接收方的名称 } ```#### 4.设备信息数据表##### 表名 :``` Devices ```##### 数据结构:```json {"_id" : ObjectId("5c9d9e72ea512d1ae49f002e"), // 自动生成ID"device_key" : "afc59916257ae8a2b6ccdfb9fd273373" // 设备的唯一编号 二维码信息 } ```#### 5.幼教内容数据表##### 表名 :``` Content ```##### 数据结构:```JSON {"_id" : ObjectId("5c9c53a7ea512d282495a499"), // 自动生成ID"music" : "2f1fe658-d018-4aaf-a088-bdaaeed29745.mp3", // 内容文件名"cover" : "2f1fe658-d018-4aaf-a088-bdaaeed29745.jpg", // 内容图像"title" : "一只哈巴狗", // 内容名称"zhuanji": "以哦签领以" // 专辑名称 } ```#### 6.聊天信息数据表##### 表名 : ``` Chats ```##### 数据结构:```json {"_id" : ObjectId("5ca5e789ea512d2e544da015"), //自动生成ID"user_list" : [ // 用户列表 数据此聊天窗口的用户"5ca17f85ea512d215cd9b079","5ca17c7aea512d26281bcb8d"],"chat_list" : [ // 聊天内容列表 {"from_user" : "5ca17c7aea512d26281bcb8d", // 信息发送方ID"to_user" : "5ca17f85ea512d215cd9b079", // 信息接收方ID"chat" : "c22b9edd-4e7a-4eee-94e7-b239a90b9b16.wav", // 语音消息文件名"createTime" : 1554376821.5634897 // 聊天创建时间 }] } ```
API文档
# 智能玩具API文档## 用户相关API#### 用户注册:用于App用户注册URL地址: /reg 请求方式: POST 请求协议:```json JSON: {"username":username,"password":password,"nickname":nickname,"gender":gender,"avatar":avatar.jpg } ```响应数据:``` JSON: {"code":0,"msg":"注册成功","data":{} } ```#### 用户登录:用于App用户登录URL地址: /login 请求方式: POST 请求协议: ```json JSON: {"username":username,"password":password } ```响应数据:``` JSON: {"code":0,"msg":"登录成功","data":{"_id" : "5c8f582f268d7942f44c6703","username" : "DragonFire","gender" : "2","nickname" : "厉害了我的哥","avatar" : "baba.jpg","friend_list" : [],"bind_toy" : []} } ```#### 用户自动登录:用于App打开时用户进行自动登录URL地址: /auto_login 请求方式: POST 请求协议:```json JSON: {"_id" : "5c8f582f268d7942f44c6703" } ```响应数据:``` JSON: {"code":0,"msg":"登录成功","data":{"_id" : "5c8f582f268d7942f44c6703","username" : "DragonFire","gender" : "2","nickname" : "厉害了我的哥","avatar" : "baba.jpg","friend_list" : [],"bind_toy" : []} } ```## 内容相关API#### 获取内容资源:用于App首页内容资源获取URL地址: /content_list 请求方式: POST 请求协议:```json JSON: {} ```响应数据:``` JSON: {"code":0,"msg":"获取内容资源列表","data":[{"_id" : "5c8f58eb268d79173c97bac5","music" : "21b61cc0-9292-4e51-bf58-72be8ee6f962.mp3","cover" : "21b61cc0-9292-4e51-bf58-72be8ee6f962.jpg","title" : "一只哈巴狗"},{"_id" : "5c8f58eb268d79173c97bac4","music" : "aa523ebe-95f9-4641-9478-1663cc74c6a6.mp3","cover" : "aa523ebe-95f9-4641-9478-1663cc74c6a6.jpg","title" : "学习雷锋好榜样"}] } ```### 获取音乐资源用于App/Toy播放内容URL地址: /get_music/<musicname>.mp3 请求方式: GET 请求协议: 无 响应数据: 数据流### 获取图片资源用于App获取图片资源URL地址: /get_image/<imagename>.jpg 请求方式: GET 请求协议: 无 响应数据: 数据流### 获取语音消息资源用于App/Toy播放语音消息URL地址: /get_chat/<chatname>.mp3 请求方式: GET 请求协议: 无 响应数据: 数据流### 获取二维码图片资源用于App获取二维码图片URL地址: /get_qr/<qrname>.mp3 请求方式: GET 请求协议: 无 响应数据: 数据流## 音频上传相关API#### App录制语音上传接口:用于App录制语音消息上传URL地址: /app_uploader 请求方式: POST 请求协议:```json JSON: {"to_user":to_user, //语音消息接收方"user_id":from_user, //语音消息发送方"reco_file":File, //语音文件 } ```响应数据:``` JSON: {"code":0,"msg":"上传成功","data":{"filename":"filename","friend_type":"app"} } ```#### Toy录制语音上传接口:用于Toy录制语音消息上传URL地址: /toy_uploader 请求方式: POST 请求协议:```json JSON: {"to_user":to_user, //语音消息接收方"user_id":from_user, //语音消息发送方"friend_type":app/toy, //语音接收方的用户类型"reco":File, //语音文件 } ```响应数据:``` JSON: {"code":0,"msg":"上传成功","data":{"filename":"filename","friend_type":"app"} } ```#### Toy录制语音上传AI接口:用于Toy录制语音消息上传至AI接口URL地址: /ai_uploader 请求方式: POST 请求协议:```json JSON: {"toy_id":toy_id, //Toy的Id"reco":File, //语音文件 } ```响应数据:``` JSON: //1.ai响应播放音乐 {"from_user": "ai","music": music_name } //2.ai响应语音消息 {"from_user": "ai", "chat": filename } //3.ai响应主动发起消息 {"from_user":friend_id,"chat":filename,"friend_type":app/toy } ```## 语音消息相关API#### App获取历史消息:用于App获取历史消息URL地址: /chat_list 请求方式: POST 请求协议:```json JSON: {"chat_id":chat_id, //聊天窗口Id"to_user":user_id, //App用户Id"from_user":friend_id, //接收消息方Id } ```响应数据:``` JSON: {"code":0,"msg":"查询聊天记录","data":[{"from_user" : "5c8f582f268d7942f44c6703","message" : "1552898221960.amr.mp3","create_time" : 1552898225.2744157},{"from_user" : "5c8f5853268d7942f44c6705","message" : "ba7462dd-62a5-460c-bfab-bb64edd7c983.wav","create_time" : 1552899004.6702642}] } ```#### Toy接收未读消息:用于Toy收到消息提醒后URL地址: /recv_msg 请求方式: POST 请求协议:```json JSON: {"from_user":user_id/toy_id, //发送语音消息方Id"to_user":toy_id, //当前接收语音消息的toy_id } ```响应数据:``` JSON: [{"from_user" : "5c8f582f268d7942f44c6703","message" : "1552898221960.amr.mp3","create_time" : 1552898225.2744157},{"from_user" : "5c8f5853268d7942f44c6705","message" : "ba7462dd-62a5-460c-bfab-bb64edd7c983.wav","create_time" : 1552899004.6702642} ] ```#### ## 硬件设备及二维码相关API#### App扫描二维码验证接口:用于App扫描Toy对应二维码进行识别URL地址: /scan_qr 请求方式: POST 请求协议:```json JSON: {"device_key":device_key, //app扫描二维码后获取到的device_key } ```响应数据:``` JSON: //1.二维码扫描成功并且设备未进行绑定 {"code":0,"msg":"二维码扫描成功","data":{"device_key":device_key} }//2.二维码扫描失败,扫描的条码不是设备库中存在的 {"code":1,"msg":"请扫描玩具二维码","data":{} }//3.二维码扫描成功,但设备已经进行绑定 {"code":2,"msg":"设备已经进行绑定","data":{"toy_id":toy_id} } ```#### App绑定设备接口:用于App绑定设备,并创建Toy信息URL地址: /bind_toy 请求方式: POST 请求协议:```json JSON: {"toy_name":toy_name, //toy名称"baby_name":baby_name, //toy所属主人名称"remark":remark, //toy主人对App用户的称呼"user_id":user_id,//绑定Toy的App用户Id"device_key":device_key, //设备唯一编码device_key } ```响应数据:``` JSON: {"code":0,"msg":"绑定完成","data":{} } ```#### App获取绑定Toy信息接口:用于App获取已经绑定的设备和创建过Toy信息URL地址: /toy_list 请求方式: POST 请求协议:```json JSON: {"_id":user_id //App用户Id } ```响应数据:``` JSON: {"code":0,"msg":"获取Toy列表","data":[{"_id" : ObjectId("5bcdaaa6268d794ec8af3fa2"),"device_key" : "bc557bcc9570069a494a64eb38698d35","bind_user" : "5bcda858268d796fc8d3e3de","toy_name" : "蛋蛋","avatar" : "toy.jpg","baby_name" : "臭屎蛋儿","gender" : "1","friend_list" : [{"friend_nickname" : "淫王","friend_avatar" : "girl.jpg","friend_remark" : "爸爸","friend_chat" : "5bcdaaa6268d794ec8af3fa1"}]},{"_id" : ObjectId("5bcdaa6f268d794ec8af3fa0"),"device_key" : "a195ac8014fb069676835a78d300f8a3","bind_user" : "5bcda858268d796fc8d3e3de","toy_name" : "球球","avatar" : "toy.jpg","baby_name" : "小粪球儿","gender" : "1","friend_list" : [{"friend_nickname" : "淫王","friend_avatar" : "girl.jpg","friend_remark" : "爸爸","friend_chat" : "5bcdaa6f268d794ec8af3f9f"}]}] } ```#### 设备启动登录接口:用于设备启动后验证身份信息URL地址: /open_toy 请求方式: POST 请求协议:```json JSON: {"device_key":device_key, //设备中写定的DeviceKey } ```响应数据:``` JSON: //1.设备处于绑定状态,正常启动 {"code":0,"music":"Success.mp3","toy_id":toy_id,"name":toy_name }//2.设备未绑定 {"code":2,"music":"Nolic.mp3" }//3.设备未授权 {"code":1,"music":"Nobind.mp3" } ```#### ## 好友通讯录相关API#### App获取好友列表接口:用于App获取好友通讯录URL地址: /friend_list 请求方式: POST 请求协议:```json JSON: {"_id":user_id, //app用户Id } ```响应数据:``` JSON: //1.二维码扫描成功并且设备未进行绑定 {"code":0,"msg":"好友查询","data":[{"friend_id" : "5c8f5853268d7942f44c6705","friend_nick" : "小粪球儿","friend_remark" : "圆圆","friend_avatar" : "toy.jpg","friend_chat" : "5c8f5853268d7942f44c6704","friend_type" : "toy"}] } ```#### 添加好友请求接口:用于请求添加好友URL地址: /add_req 请求方式: POST 请求协议:```json JSON: {"req_user":req_user_id, //发送好友请求方Id"add_user":add_user_id, //被请求方Id"type":app/toy, //请求方客户端类型"req_info":"我是xxx", //请求内容信息"remark":remark //请求方 对 被请求方 的备注名称 } ```响应数据:``` JSON: {"code":0,"msg":"添加好友请求成功","data":{} } ```#### App好友请求列表查询接口:用于App查询绑定Toy的好友请求URL地址: /req_list 请求方式: POST 请求协议:```json JSON: {"_id":user_id, //app用户Id } ```响应数据:``` JSON: {"code":0,"msg":"查询好友请求","data":[{"req_user":req_user_id, //发送好友请求方Id"add_user":add_user_id, //被请求方Id"type":app/toy, //请求方客户端类型"req_info":"我是xxx", //请求内容信息"remark":remark //请求方 对 被请求方 的备注名称"avatar":avatar.jpg // 请求方的头像"nickname":"nickname" // 请求方的昵称"status":0 // 请求状态 0待处理 1同意 2拒绝"toy_name":"toy_name" // 被请求方的昵称 }] } ```#### App拒绝好友请求接口:用于App拒绝绑定Toy被添加为好友URL地址: /ref_req 请求方式: POST 请求协议:```json JSON: {"req_id":req_id //好友请求信息Id } ```响应数据:``` JSON: {"code":0,"msg":"拒绝添加好友","data":{} } ```#### App同意好友请求接口:用于App同意绑定Toy被添加为好友URL地址: /acc_req 请求方式: POST 请求协议:```json JSON: {"req_id":req_id, //好友请求信息Id"remark":"friend_remark", //为请求方添加备注名称 } ```响应数据:``` JSON: {"code":0,"msg":"同意添加好友","data":{} } ```####
初步任务
积极正面幼教内容 - 让玩具能够播放 父母选定的儿歌,故事,百科,英语- 通过语音识别内容名称 玩具可以主动点播内容 1.从喜马拉雅内容提供商 采集积极正面幼教内容App 获取内容信息 /content_list - 将数据库中的内容信息使用列表的形式返回APPApp /get_cover 获取内容的图片信息App /get_music 获取音乐2.用户的注册登录接口实现App /reg 注册用户App /login 用户登录 返回的用户信息App /auto_login 用户打开APP后自动登录 返回的用户信息3.创建设备的DeviceKey通过联图二维码API批量创建二维码 - md5(uuid4 + time + uuid4)
从网站获取播放信息--提取所需要的数据-- 存入数据库
从网站获取资源存入文件
对应的数据存入数据库
import timeimport requestsfrom settings import COVER_PATH, MUSIC_PATH, MDBdata = """ {"ret": 200,"msg": "声音播放数据","data": {"uid": 0,"albumId": 424529,"sort": 1,"pageNum": 1,"pageSize": 30,"tracksAudioPlay": [ {"index": 10,"trackId": 7713656,"trackName": "小小发型师","trackUrl": "/ertong/424529/7713656","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 77,"src": "https://fdfs.xmcdn.com/group14/M06/3C/1C/wKgDY1Wcw0_AUxG7AAmWOfM875o604.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 9,"trackId": 7713655,"trackName": "小宝贝","trackUrl": "/ertong/424529/7713655","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 145,"src": "https://fdfs.xmcdn.com/group15/M0B/3C/32/wKgDaFWcwtaisdS-ABIEA9keU18436.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 8,"trackId": 7713654,"trackName": "小孩应把卫生讲","trackUrl": "/ertong/424529/7713654","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 95,"src": "https://fdfs.xmcdn.com/group15/M0B/3C/33/wKgDZVWcwvaT_e8oAAvfdhfXJNQ861.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 7,"trackId": 7713653,"trackName": "小喇叭","trackUrl": "/ertong/424529/7713653","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 85,"src": "https://fdfs.xmcdn.com/group12/M03/3B/B1/wKgDXFWcwtzgy3BgAAqLJj1ijho574.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 6,"trackId": 7713652,"trackName": "小可爱","trackUrl": "/ertong/424529/7713652","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 214,"src": "https://fdfs.xmcdn.com/group12/M03/3B/B8/wKgDW1WcwvHzL1FEABqFFMJa4L4742.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 5,"trackId": 7713649,"trackName": "学走路","trackUrl": "/ertong/424529/7713649","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 73,"src": "https://fdfs.xmcdn.com/group12/M00/3B/BA/wKgDW1Wcw4zBISExAAkms7-Of3Q290.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 4,"trackId": 7713648,"trackName": "学唱数字歌","trackUrl": "/ertong/424529/7713648","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 113,"src": "https://fdfs.xmcdn.com/group12/M00/3B/B2/wKgDXFWcw2mgNQIdAA4EE3MzTKU976.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 3,"trackId": 7713647,"trackName": "学习雷锋好榜样","trackUrl": "/ertong/424529/7713647","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 68,"src": "https://fdfs.xmcdn.com/group12/M00/3B/BA/wKgDW1Wcw4fQ7JWWAAh7UKT5m7c703.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 2,"trackId": 7713644,"trackName": "一只哈巴狗","trackUrl": "/ertong/424529/7713644","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 86,"src": "https://fdfs.xmcdn.com/group9/M08/3B/CA/wKgDYlWcw5HTULbSAAq9BqdgRXQ509.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}, {"index": 1,"trackId": 7713643,"trackName": "一双小小手","trackUrl": "/ertong/424529/7713643","trackCoverPath": "//imagev2.xmcdn.com/group9/M04/3B/E1/wKgDZlWcvRKwSOIMAAD3201gPxc590.jpg","albumId": 424529,"albumName": "【一千零一夜】经典儿歌","albumUrl": "/ertong/424529/","anchorId": 9216785,"canPlay": true,"isBaiduMusic": false,"isPaid": false,"duration": 62,"src": "https://fdfs.xmcdn.com/group13/M00/3C/12/wKgDXlWcw5GzJ1NgAAfMZ7UShYY633.m4a","hasBuy": true,"albumIsSample": false,"sampleDuration": 0,"updateTime": "2年前","createTime": "4年前","isLike": false,"isCopyright": true,"firstPlayStatus": true}],"hasMore": true} } """ import json import os from uuid import uuid4my_data = json.loads(data) # print(my_data)# 提取所需数据(比如获取十个数据) data_list = my_data.get("data").get("tracksAudioPlay") # print(data_list)music_list=[] for audio in data_list:# print(audio)cover_url = f"http:{audio.get('trackCoverPath')}"music_url = audio.get('src')all_name = uuid4()# 获取封面文件cover_name = f"{all_name}.jpg"cover_path = os.path.join(COVER_PATH, cover_name)cover = requests.get(cover_url)# print(cover)with open(cover_path, "wb") as fcover:fcover.write(cover.content)# 获取音乐文件music_name = f"{all_name}.mp3"music_path = os.path.join(MUSIC_PATH, music_name)music = requests.get(music_url)with open(music_path, "wb") as fmusic:fmusic.write(music.content)time.sleep(0.3)title = audio.get("trackName")zhuanji = audio.get("albumName")# 将对应数据写入字典,存入数据库music_info = {"music": music_name,"cover": cover_name,"title": title,"zhuanji": zhuanji}##### 数据结构:'''JSON {"_id" : ObjectId("5c9c53a7ea512d282495a499"), // 自动生成ID"music" : "2f1fe658-d018-4aaf-a088-bdaaeed29745.mp3", // 内容文件名"cover" : "2f1fe658-d018-4aaf-a088-bdaaeed29745.jpg", // 内容图像"title" : "一只哈巴狗", // 内容名称"zhuanji": "以哦签领以" // 专辑名称}''' music_list.append(music_info) MDB.Content.insert_many(music_list)
setting 数据库的配置
# 目录配置 COVER_PATH = 'COVER' MUSIC_PATH = 'MUSIC' QRCODE_PATH='QRcode'# 数据库的配置from pymongo import MongoClientM = MongoClient("127.0.0.1", 27017) MDB = M["DogToy"]# RET的返回值RET = {"CODE": 0,"MSG": "注册成功","DATA": {}}# 联图二维码接口 API LT_URL = "http://qr.liantu.com/api.php?text=%s"
app.py
from flask import Flaskfrom serv.content import content_bp from serv.user import user_bpapp = Flask(__name__) app.config["DEBUG"] = Trueapp.register_blueprint(content_bp) app.register_blueprint(user_bp)if __name__ == '__main__':app.run("0.0.0.0", 9090)
首页 音乐表单信息展示 图片资源 音乐资源
import osfrom flask import Blueprint, jsonify, send_filefrom settings import MDB, COVER_PATH, MUSIC_PATHcontent_bp=Blueprint("content_bp",__name__)# 首页展示音乐表单信息""" #### 获取内容资源: 用于App首页内容资源获取 URL地址: / content_list 请求方式: POST 请求协议: json JSON: { }响应数据:JSON: {"code": 0,"msg": "获取内容资源列表","data":[{"_id": "5c8f58eb268d79173c97bac5","music": "21b61cc0-9292-4e51-bf58-72be8ee6f962.mp3","cover": "21b61cc0-9292-4e51-bf58-72be8ee6f962.jpg","title": "一只哈巴狗"},{"_id": "5c8f58eb268d79173c97bac4","music": "aa523ebe-95f9-4641-9478-1663cc74c6a6.mp3","cover": "aa523ebe-95f9-4641-9478-1663cc74c6a6.jpg","title": "学习雷锋好榜样"}] } """
# 获取首页音乐内容
@content_bp.route("/content_list", methods=["post"])
def content_list():
# 数据库查询
content=list(MDB.Content.find({}))
# 更改数据格式
for item in content:
item["_id"]=str(item.get("_id"))
return jsonify(content)
""" ### 获取图片资源 用于App获取图片资源 URL地址: /get_image/<imagename>.jpg 请求方式: GET 请求协议: 无 响应数据: 数据流 """
获取文件资源 数据流的形式上传
@content_bp.route("/get_cover/<filename>",methods=["GET"])
def get_cover(filename):
cover_path=os.path.join(COVER_PATH,filename)
return send_file(cover_path)
""" ### 获取音乐资源 用于App/Toy播放内容 URL地址: /get_music/<musicname>.mp3 请求方式: GET 请求协议: 无 响应数据: 数据流 """
@content_bp.route("/get_music/<filename>",methods=["GET"])
def get_music(filename):
cover_path=os.path.join(MUSIC_PATH,filename)
return send_file(cover_path)
#后续添加
# 返回二维码信息图片
@content_bp.route("/get_qr/<filename>",methods=["GET"])
def get_qr(filename):
qr_path=os.path.join(QRCODE_PATH,filename)
return send_file(qr_path)
# 返回聊天信息
@content_bp.route("/get_chat/<filename>",methods=["GET"])
def get_chat(filename):
chat_path=os.path.join(CHAT_PATH,filename)
return send_file(chat_path)
用户注册 登录 自动登录
import osfrom bson import ObjectId from flask import Blueprint, jsonify, send_file, requestfrom settings import MDB, COVER_PATH, MUSIC_PATH, RETuser_bp=Blueprint("user_bp",__name__)""" #### 用户注册:用于App用户注册URL地址: /reg 请求方式: POST 请求协议:```json JSON: {"username":username,"password":password,"nickname":nickname,"gender":gender,"avatar":avatar.jpg } ```响应数据:``` JSON: {"code":0,"msg":"注册成功","data":{} } """ """ #### 1.App用户数据表##### 表名 : ``` Users ```##### 数据结构:```json {"_id" : ObjectId("5c9d8da3ea512d2048826260"), //自动生成ID"username" : "asdf", //用户名"password" : "962012d09b8170d912f0669f6d7d9d07", //密码"nickname" : "DragonFire", // 用户昵称"gender" : "2", // 用户性别"avatar" : "baba.jpg", // 用户头像 男=="baba.jpg" 女=="mama.jpg""bind_toys" : [ // 用户绑定的玩具ID(string)"5ca17c7aea512d26281bcb8d","5ca17f85ea512d215cd9b079"], "friend_list" : [ // 用户的通讯录列表{ // 通讯录信息"friend_id" : "5ca17c7aea512d26281bcb8d", // 好友id"friend_nick" : "背背", // 好友的昵称"friend_remark" : "臭屎蛋儿", // 好友备注"friend_avatar" : "toy.jpg", // 好友头像"friend_chat" : "5ca17c7aea512d26281bcb8c", // 私聊窗口ID 聊天数据表对应值"friend_type" : "toy" // 好友类型 },{"friend_id" : "5ca17f85ea512d215cd9b079","friend_nick" : "圆圆","friend_remark" : "小粪球儿","friend_avatar" : "toy.jpg","friend_chat" : "5ca17f85ea512d215cd9b078","friend_type" : "toy"}] } ``` """ # 注册 @user_bp.route("/reg",methods=["POST"]) def reg():# 按照前端格式要求,补充数据,存入数据库user_info=request.form.to_dict()# print(user_info)# user_dict={"username":user_info.get("username")}# print(user_dict)# res = MDB.Users.find_one(user_dict)# print(res)# if res:# return jsonify({"CODE": 1, "MSG": "该用户已存在,注册失败", "DATA": {}})# else:
#添加数据时,已有的数据不用管, 再添加缺少的数据user_info['avatar']="baba.jpg" if user_info.get("gender") == "2" else "mama.jpg"user_info["bind_toys"]=[]user_info["friend_list"]=[]MDB.Users.insert_one(user_info)return jsonify({"CODE":0,"MSG":"注册成功","DATA":{}})""" #### 用户登录:用于App用户登录URL地址: /login 请求方式: POST 请求协议: ```json JSON: {"username":username,"password":password } ```响应数据:JSON: {"code":0,"msg":"登录成功","data":{"_id" : "5c8f582f268d7942f44c6703","username" : "DragonFire","gender" : "2","nickname" : "厉害了我的哥","avatar" : "baba.jpg","friend_list" : [],"bind_toy" : []} } """ @user_bp.route("/login",methods=["POST"]) def login():user_info=request.form.to_dict()# 数据库查看是否有对应的数据user_info_dict=MDB.Users.find_one(user_info)print(user_info_dict)if user_info_dict:
#注意格式转化user_info_dict['_id']=str(user_info_dict.get("_id"))RET["CODE"]=0RET["MSG"]=f"欢迎{user_info_dict.get('nickname')}登录!"RET["DATA"]=user_info_dictreturn jsonify(RET)else:RET["CODE"] = 1RET["MSG"] = f"用户名或者密码错误!"return jsonify(RET)""" #### 用户自动登录:用于App打开时用户进行自动登录URL地址: /auto_login 请求方式: POST 请求协议:```json JSON: {"_id" : "5c8f582f268d7942f44c6703" } ```响应数据:``` JSON: {"code":0,"msg":"登录成功","data":{"_id" : "5c8f582f268d7942f44c6703","username" : "DragonFire","gender" : "2","nickname" : "厉害了我的哥","avatar" : "baba.jpg","friend_list" : [],"bind_toy" : []} } ``` """ @user_bp.route("/auto_login",methods=["POST"]) def auto_login():user_info = request.form.to_dict()# 数据库查看是否有对应的数据print(user_info)user_info["_id"]=ObjectId(user_info.get("_id"))user_info_dict = MDB.Users.find_one(user_info)# print(user_info_dict)user_info_dict['_id'] = str(user_info_dict.get("_id"))
# 未读信息提醒 后面添加未读提醒
count_dict=get_all_redis(user_info_dict["_id"])
user_info_dict["chat"]=count_dict
RET["CODE"] = 0RET["MSG"] = f"欢迎{user_info_dict.get('nickname')}登录!"RET["DATA"] = user_info_dictreturn jsonify(RET)
创建二维码
# # 测试 # import requests # # res=requests.get("http://qr.liantu.com/api.php?text=%s"%("niuniu")) # print(res.content) # with open("a.jpg" ,"wb") as f: # f.write(res.content) import osimport requests from uuid import uuid4 import time import hashlibfrom settings import MDB, LT_URL, QRCODE_PATH""" 二维码作为设备唯一标识
#设备信息表 的 创建### 获取二维码图片资源 用于App获取二维码图片 URL地址: /get_qr/<qrname>.mp3 请求方式: GET 请求协议: 无 响应数据: 数据流 """ device_list = [] for i in range(5):
#保证唯一性 (复杂性)qr_str = hashlib.md5(f"{uuid4()}{time.time()}{uuid4()}".encode("utf8")).hexdigest()# print(qr_str)device_info={"device_key":qr_str}device_list.append(device_info)res=requests.get(LT_URL%(qr_str))qrfile_path=os.path.join(QRCODE_PATH,f"{qr_str}.jpg")with open(qrfile_path,"wb") as f:f.write(res.content)MDB.Device.insert_many(device_list)