在創建項目以前,首先咱們要獲取數據,html
簡單寫一個爬取信息的py文件。web
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中的配置:json
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構建底部欄flask
在main.html中,輸入ms構建輪播圖。mg九宮格後端
mui.js中的配置:websocket
後端代碼:app
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.htmlsocket
而後再index.html頁面接受。ui
在後端創建一個websocket鏈接:url
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實現手機遙控器播放兒歌