flask项目 玩具的语音播报

在建立项目之前,首先我们要获取数据,

简单写一个爬取信息的py文件。

import requests,os
from settings import MONGO_DB
from settings import IMAGE_PATH
from settings import MUSIC_PATH
from uuid import uuid4

content_url = "/ertong/424529/7713678"
content_type = "erge"
content_id = content_url.rsplit("/", 1)[-1]
res = requests.get("http://m.ximalaya.com/tracks/%s.json" % (content_id))
content_info = res.json()

content_name = str(uuid4())
content_img_path = os.path.join(IMAGE_PATH,content_name)
content_music_path = os.path.join(MUSIC_PATH,content_name)

audio = requests.get(content_info.get("play_path"))
with open("{}.mp3".format(content_music_path), "wb") as f:
    f.write(audio.content)

img = requests.get(content_info.get("cover_url"))
with open("{}.jpg".format(content_img_path), "wb") as f:
    f.write(img.content)

title = content_info.get("title")
nickname = content_info.get("nickname")
album_title = content_info.get("album_title")
intro = content_info.get("intro")
play_count = 0

content = {
    "title": title,
    "content_type": content_type,
    "nickname": nickname,
    "album_title": album_title,
    "intro": intro,
    "play_count": play_count,
    "audio": "{}.mp3".format(content_name),
    "cover": "{}.jpg".format(content_name)
}

MONGO_DB.content.insert_one(content)

settings中的配置:

import pymongo
import os

# 数据配置
mongo_client = pymongo.MongoClient(host='127.0.0.1',port=27017)
MONGO_DB = mongo_client["KingEight"]

# 资源目录配置
IMAGE_PATH = "Images"
MUSIC_PATH = "Music"

#数据采集配置
XPP_URL = "http://m.ximalaya.com/tracks/%s.json"

# 协议格式
RET = {
    'code':0,
    'msg':'',
    'data':{ },
}

 

新建一个项目,在index.html,输入mta构建底部栏

在main.html中,输入ms构建轮播图。mg九宫格

mui.js中的配置:

后端代码:

from flask import Flask,request,jsonify
from serv import content

app = Flask(__name__)

app.register_blueprint(content.content_bp)


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

 

 

然后在player.html

 然后再index.html页面接受。

 在后端建立一个websocket连接:

from flask import Flask,request
from geventwebsocket.websocket import WebSocket
from gevent.pywsgi import WSGIServer
from geventwebsocket.handler import WebSocketHandler

import json

websocket_app = Flask(__name__)
user_socket_dict = { }

@websocket_app.route('/ws/<uid>')
def ws(uid):
    user_socket = request.environ.get('wsgi.websocket') # type:WebSocket
    user_socket_dict[uid] = user_socket
    print(len(user_socket_dict),user_socket_dict)
    while True:
        msg = user_socket.receive()
        print(msg)
        msg_dict = json.loads(msg)
        to_user = msg_dict.get('to_user') # {to_user:123,music:abc.mp3}
        to_user_socket = user_socket_dict.get(to_user)
        print(to_user_socket)
        to_user_socket.send(msg_dict.get('music'))
if __name__ == '__main__':
    http_serv = WSGIServer(("0.0.0.0",9528),websocket_app,handler_class=WebSocketHandler)
    http_serv.serve_forever()

 

今日开发者日志:

   1.基于requests模块实现数据采集
        
    2.基于mui + HTML5PLUS mlist图文列表,实现数据展示
        原生js mlist图文列表
        ES6 mlist图文列表
        
    3.基于HTML5PLUS Audio接口 实现手机APP的音频内容播放
        CreatePlayer,Audio在Stop清空 AudioPlayer
    
    4.基于Websocket实现手机遥控器播放儿歌

 

posted @ 2018-10-21 23:41  黑猫不是警长  阅读(376)  评论(0编辑  收藏  举报