• 人工智能玩具


    步骤 :

    1.flask app登录(自动登录)

    2.获取歌曲/图片资源 (存储到Content数据库里)+content蓝图(get_*接口)+创建二维码 

    3.scan_qr 接口+绑定玩具+玩具列表显示

    4.opentoy+WEBSOCKET+sendmusic(FLASKCORS 跨域请求)+ger_qr+friend_list+app,toy互聊

    5.friend_remark语音提醒 (谁发来的消息来的)+recv_msg+ai+toyAI  指令+未读消息存储(redis数据库)+收取多条消息语音提示

    6.ADD+REQ  +  req_list  +acc_req +ref_req 

    7.

    settings.py配置文件

    # 目录配置
    COVER_PATH = "Cover"
    MUSIC_PATH = "Music"
    QRCODE_PATH = "Qrcode"
    CHAT_PATH = "Chat"
    
    
    # 数据库配置
    from pymongo import MongoClient
    M = MongoClient("127.0.0.1",27017)
    MDB = M["MonsterToy"]
    
    from redis import Redis RDB = Redis("127.0.0.1",6379,db=8) # RET返回值 RET = { "CODE":0, "MSG":"注册成功", "DATA":{} } # 联图二维码接口API LT_URL = "http://qr.topscan.com/api.php?text=%s" # BaiduAI配置 from aip import AipSpeech, AipNlp """ 你的 APPID AK SK """ APP_ID = '16815061' API_KEY = 'G9k256n6aeMRnfvS1paws50x' SECRET_KEY = 'nCbbF44iUp6QrNvSgcYW0eo2EKKl4cvR' SPEECH_CLIENT = AipSpeech(APP_ID, API_KEY, SECRET_KEY) NLP_CLIENT = AipNlp(APP_ID, API_KEY, SECRET_KEY) VIOCE={ 'vol': 5, "spd": 4, "pit": 5, "per": 4 }

      

    (user蓝图)要学会看需求(request.form.to_dict()为获取请求信息)

    from bson import ObjectId
    from flask import Blueprint, request, jsonify
    
    from redis_chat import get_all_redis
    from setting import MDB, RET
    
    user_bp = Blueprint("user_bp",__name__)
    

    @user_bp.route("/reg",methods= ["POST"]) def reg(): user_info = request.form.to_dict() 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)#注意insert_one return jsonify({"CODE": 0, "MSG": "注册成功", "DATA": {}})



    #千万要注意 Mongodb 中 的“_id”在json传输是要转换成字符串 @user_bp.route("/login",methods=["POST"]) def login(): user_info = request.form.to_dict() print(user_info) user_info_dict = MDB.Users.find_one(user_info) print(user_info_dict) user_info_dict["_id"] = str(user_info_dict.get("_id")) RET["CODE"] = 0 RET["MSG"] = f"欢迎{user_info_dict.get('nickname')}登录" RET["DATA"] = user_info_dict return jsonify(RET)


    @user_bp.route("/auto_login",methods=["POST"])
    def auto_login():
    user_info = request.form.to_dict()
    user_info["_id"] = ObjectId(user_info.get("_id"))
    user_info_dict = MDB.Users.find_one(user_info)

    user_info_dict["_id"] = str(user_info_dict.get("_id"))
    count_dict = get_all_redis(user_info) (下面#获取所有未读消息)
    user_info_dict["chat"] = count_dict
    RET["CODE"] = 0
    RET["MSG"] = f"欢迎{user_info_dict.get('nickname')}登录"
    RET["DATA"] = user_info_dict

    return jsonify(RET)


    (位于redis_chat.py文件)
    # 获取所有未读消息
    def get_all_redis(to_user):
        to_user_json = RDB.get(to_user)
        if to_user_json:
            to_user_dict = json.loads(to_user_json)  # type:dict
            to_user_dict["count"] = sum(to_user_dict.values())
        else:
            to_user_dict = {"count": 0}
    
        print(to_user_dict)
        return to_user_dict

    (papa.py)(下载图片和歌曲  并存入数据库)

    import json
    import os
    import time
    from uuid import uuid4
    
    import requests
    
    from setting import COVER_PATH, MUSIC_PATH, MDB
    
    #从网上手动获取data data = '{"ret":200,"msg":"声音播放数据","data":{"uid":0,"albumId":424529,"sort":1,"pageNum":1,"pageSize":30,"tracksAudioPlay":[{"index":30,"trackId":7713678,"trackName":"新年恰恰","trackUrl":"/ertong/424529/7713678","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":92,"src":"https://fdfs.xmcdn.com/group12/M00/3B/B2/wKgDXFWcw12y8TanAAtkIsI9320251.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":29,"trackId":7713564,"trackName":"我的快乐style","trackUrl":"/ertong/424529/7713564","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":155,"src":"https://fdfs.xmcdn.com/group8/M01/3B/D1/wKgDYFWcwlKzWOleABNA26oG9m0575.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":28,"trackId":7713768,"trackName":"鱼儿水中游","trackUrl":"/ertong/424529/7713768","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":114,"src":"https://fdfs.xmcdn.com/group11/M07/3C/12/wKgDa1WcxH-yUx0yAA4jOCnkzoo604.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":27,"trackId":7713763,"trackName":"祝你圣诞快乐","trackUrl":"/ertong/424529/7713763","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":91,"src":"https://fdfs.xmcdn.com/group13/M0A/3C/07/wKgDXVWcxJOxETOJAAtXC0jV-tQ007.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"6月前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":26,"trackId":7713762,"trackName":"祖国祖国我们爱你","trackUrl":"/ertong/424529/7713762","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/group9/M06/3B/EA/wKgDZlWcxJSQzwcfAA4N0BoDMdY241.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":25,"trackId":7713760,"trackName":"最美的图画","trackUrl":"/ertong/424529/7713760","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":92,"src":"https://fdfs.xmcdn.com/group14/M00/3C/07/wKgDZFWcxLrzVVEcAAthUHuW9o8311.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":24,"trackId":7713757,"trackName":"愿望","trackUrl":"/ertong/424529/7713757","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":115,"src":"https://fdfs.xmcdn.com/group10/M05/3B/C0/wKgDaVWcxJGzJ7uSAA47_grnQu0333.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":23,"trackId":7713756,"trackName":"异想天开","trackUrl":"/ertong/424529/7713756","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":242,"src":"https://fdfs.xmcdn.com/group13/M04/3C/13/wKgDXlWcxCujLknpAB361yR0knM330.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":22,"trackId":7713682,"trackName":"雪宝宝","trackUrl":"/ertong/424529/7713682","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":80,"src":"https://fdfs.xmcdn.com/group12/M00/3B/B3/wKgDXFWcw3rzzAXUAAn2_rd_TSs416.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":21,"trackId":7713681,"trackName":"洗澡歌","trackUrl":"/ertong/424529/7713681","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":103,"src":"https://fdfs.xmcdn.com/group16/M04/3B/F0/wKgDalWcwovDamwPAAzWCE1KPhg634.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":20,"trackId":7713679,"trackName":"校园的早晨","trackUrl":"/ertong/424529/7713679","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":170,"src":"https://fdfs.xmcdn.com/group7/M0A/3C/83/wKgDWlWcw4ayhRYzABUJGT5KiYk025.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":19,"trackId":7713676,"trackName":"摇篮曲","trackUrl":"/ertong/424529/7713676","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":141,"src":"https://fdfs.xmcdn.com/group13/M00/3C/11/wKgDXlWcw4bg7pJVABFuZE2it5M023.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":18,"trackId":7713675,"trackName":"幸福的一家","trackUrl":"/ertong/424529/7713675","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":98,"src":"https://fdfs.xmcdn.com/group12/M00/3B/BA/wKgDW1Wcw3mzthSOAAwwJKKstIo183.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":17,"trackId":7713673,"trackName":"小马车","trackUrl":"/ertong/424529/7713673","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":71,"src":"https://fdfs.xmcdn.com/group11/M06/3C/14/wKgDbVWcwtOgeKw_AAjTUUqlrKg930.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":16,"trackId":7713670,"trackName":"小蚂蚁","trackUrl":"/ertong/424529/7713670","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/group11/M06/3C/14/wKgDbVWcwtXQ0dlHAAe1eRxnsE0477.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":15,"trackId":7713665,"trackName":"小红帽","trackUrl":"/ertong/424529/7713665","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":110,"src":"https://fdfs.xmcdn.com/group15/M0B/3C/33/wKgDZVWcwv2it2_0AA2pv_mYNFU658.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":14,"trackId":7713664,"trackName":"小玉米","trackUrl":"/ertong/424529/7713664","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":98,"src":"https://fdfs.xmcdn.com/group7/M0A/3C/7E/wKgDX1Wcw13z8c8YAAwzfb-5DTc863.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":13,"trackId":7713663,"trackName":"小猪猪","trackUrl":"/ertong/424529/7713663","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":201,"src":"https://fdfs.xmcdn.com/group7/M0A/3C/7F/wKgDX1Wcw2_AiWYsABjbdVPaqLY570.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":12,"trackId":7713662,"trackName":"小猪小猪肥嘟嘟","trackUrl":"/ertong/424529/7713662","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":79,"src":"https://fdfs.xmcdn.com/group7/M0A/3C/83/wKgDWlWcw2yBPuHdAAnSUqLNF0g598.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"index":11,"trackId":7713660,"trackName":"小毛驴","trackUrl":"/ertong/424529/7713660","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":91,"src":"https://fdfs.xmcdn.com/group12/M04/3B/B1/wKgDXFWcwvKzSpMUAAthUBlZLzc281.m4a","hasBuy":true,"albumIsSample":false,"sampleDuration":0,"updateTime":"2年前","createTime":"4年前","isLike":false,"isCopyright":true,"firstPlayStatus":true},{"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}}'

    #网上获取的是str 需要转成字典 my_data = json.loads(data) print(my_data)
    #取出前10条歌曲内容 data_list = my_data.get("data").get("tracksAudioPlay")[:10] # print(data_list) music_list = []
    for audio in data_list: #先爬取网址 title_name = audio.get("trackName") sp_is = audio.get('albumName') #(专辑) 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) #从网站获取2进制流 然后写进文件 cover = requests.get(cover_url) print(cover.content) #可以。content .json() .text time.sleep(0.3) 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) print(music.content) # 可以。content .json() .text time.sleep(0.3) with open(music_path, "wb") as fmusic: fmusic.write(music.content) music_info = { "music": music_name, "cover": cover_name, "title": title_name, "zhuanji": sp_is } music_list.append(music_info) MDB.Content.insert_many(music_list)

    Content.py蓝图

    import os
    
    from flask import Blueprint, jsonify, send_file
    from setting import MDB, COVER_PATH, MUSIC_PATH, QRCODE_PATH, CHAT_PATH
    
    content_bp = Blueprint("content_bp",__name__)
    
    

    #音乐列表 @content_bp.route("/content_list",methods=["POST"]) def content_list(): content = list(MDB.Content.find({})) #数据库取出的字典集转换为列表   #ObjectId(_id) 转换为 字符串 for index,item in enumerate(content): content[index]["_id"] = str(item.get("_id")) return jsonify(content)



    #获取图片 注意是 send_file() @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) #获取音乐 @content_bp.route("/get_music/<filename>",methods=["GET"]) def get_music(filename): music_path = os.path.join(MUSIC_PATH,filename) return send_file(music_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)

    createQR.py 创建二维码

    import os
    
    import requests
    from setting import LT_URL, MDB, QRCODE_PATH
    from uuid import uuid4
    import time, hashlib
    
    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.Devices.insert_many(device_list)

     

    devices.py 蓝图  scan_qr 接口+绑定玩具+玩具列表显示

    from bson import ObjectId
    from flask import Blueprint, request, jsonify
    
    from setting import MDB, RET
    
    devices_bp = Blueprint("devices_bp", __name__)
    
    
    @devices_bp.route("/scan_qr", methods=["POST"])
    def scan_qr():
        # 扫码绑定
        # 1.扫描成功,没有绑定 开启绑定
        # 2.扫码失败,未授权
        # 3.扫码成功,已经绑定  ?添加好友
        device_key = request.form.to_dict()
    
        toy_info = MDB.Toys.find_one(device_key)
        if not toy_info:
            device = MDB.Devices.find_one(device_key)
            if device:
                # 1.扫描成功,没有绑定 开启绑定
                RET["CODE"] = 0
                RET["MSG"] = "识别玩具成功"
                RET["DATA"] = device_key
    
            else:
                # 2.扫码失败, 未授权 授权库没有此条码
                RET["CODE"] = 1
                RET["MSG"] = "请不要瞎JB乱扫"
                RET["DATA"] = {}
    
        else:
            # // 3.蜜汁逻辑
            # 二维码扫描成功, 但设备已经进行绑定 开启添加好友逻辑
            RET["CODE"] = 2
            RET["MSG"] = "设备已经进行绑定"
            RET["DATA"] = {"toy_id":str(toy_info.get("_id"))}
    
        return jsonify(RET)
    



    #绑定玩具(创建一个玩具 创建聊天窗口) 创建连天窗口添加数据库 toy toy添加user 添加到数据库 user添加toy 更新Users数据库 更新连天数据库 @devices_bp.route("/bind_toy", methods=["POST"]) def bind_toy(): toy_info = request.form.to_dict() user_id = toy_info.pop("user_id") user_info = MDB.Users.find_one({"_id": ObjectId(user_id)}) # 查询user_info # 创建一个Chats chat_id = MDB.Chats.insert_one({"user_list": [], "chat_list": []}) # 1.创建toy toy_info["avatar"] = "toy.jpg" toy_info["friend_list"] = [] toy_info["bind_user"] = user_id # toy_info[bind_user]? 这个值怎么获得? # toy_id = MDB.Toys.insert_one(toy_info) 暂时不创建toy # 2.toy有了app的绑定对象, app的绑定toy对象是谁呢? # 在 Users 数据中的 bind_toys 列表 加入 toy 绑定对象的_id字符串 # MDB.Users.update_one({}, {"$push": {"bind_toys": str(toy_id.inserted_id)}}) 暂时不创建 # 3.将toy和app 交换名片 # 建立一个移动端好友关系是为了 即使通讯 IM 基于通讯录的 # 给玩具增加第一个好友 app toy_add_user = { "friend_id": user_id, # app id "friend_nick": user_info.get("nickname"), # user 昵称 "friend_remark": toy_info.pop("remark"), # remark在哪里呢? "friend_avatar": user_info.get("avatar"), # user有头像吗? "friend_chat": str(chat_id.inserted_id), "friend_type": "app" # 好友的用户类型 app / toy }, toy_info["friend_list"].append(toy_add_user) toy_id = MDB.Toys.insert_one(toy_info) # 一个人无法决定两人的关系 app 也要同时 增加toy为好友 user_add_toy = { "friend_id": str(toy_id.inserted_id), # toy_id str "friend_nick": toy_info.get("baby_name"), # baby_name "friend_remark": toy_info.get("toy_name"), # toy_name "friend_avatar": "toy.jpg", # 阿凡达 "friend_chat": str(chat_id.inserted_id), # chat_id "friend_type": "toy" # 好友的类型 toy } user_info["bind_toys"].append(str(toy_id.inserted_id)) user_info["friend_list"].append(user_add_toy) # 修改 Users 的全部数据 MDB.Users.update_one({"_id": ObjectId(user_id)}, {"$set": user_info}) # Chats 数据也会变化 user_list player_list 将 toy_id 和 user_id 加入 MDB.Chats.update_one({"_id": chat_id.inserted_id}, {"$set": {"user_list": [user_id, str(toy_id.inserted_id)]}}) RET["CODE"] = 0 RET["MSG"] = "绑定完成" RET["DATA"] = {} return jsonify(RET)

    @devices_bp.route("/toy_list", methods=["POST"]) def toy_list(): bind_user = request.form.get("_id") toyl = list(MDB.Toys.find({"bind_user": bind_user}))
    #ObjectId 转换为 str for toy in toyl: toy["_id"] = str(toy.get("_id")) RET["CODE"] = 0 RET["MSG"] = "获取Toy列表" RET["DATA"] = toyl return jsonify(RET)


    @devices_bp.route("/open_toy", methods=["POST"]) def open_toy(): device_key = request.form.to_dict() # 1.用 device_key 查询? Devices or Toys # 先查询已绑定Toy 可以有效减少数据库查询次数(减少IO操作) toy = MDB.Toys.find_one(device_key) # 2.设备处于绑定状态,正常启动 # 设备未绑定 # 设备未授权 0.5% if toy: # 开机成功 ret = { "code": 0, "music": "Success.mp3", "toy_id": str(toy.get("_id")), "name": toy.get("toy_name") } else: if MDB.Devices.find_one(device_key): # 设备未绑定 ret = { "code": 1, "music": "Nobind.mp3" } else: # 设备未授权 ret = { "code": 2, "music": "Nolic.mp3" } return jsonify(ret)

      

     #长连接websocket

    import json
    from flask import Flask, request,render_template
    from geventwebsocket.handler import WebSocketHandler
    from geventwebsocket.server import WSGIServer
    from geventwebsocket.websocket import WebSocket
    
    
    ws_app = Flask(__name__)
    
    user_socket_dict={}
    @ws_app.route("/app/<user_id>")
    def app(user_id):
        print(user_id)
        app_socket =request.environ.get("wsgi.websocket") #type:WebSocket
        if app_socket:
            user_socket_dict[user_id] = app_socket
        while True:
            app_data = app_socket.receive()
            toy_data_dict = json.loads(app_data)
            to_user = toy_data_dict.get("to_user")
            print("toy_data:" + app_data)
            usocket = user_socket_dict.get(to_user)
            usocket.send(app_data)
    
    
    @ws_app.route("/toy/<toy_id>")
    def toy(toy_id): # Toy 连接的位置
        print(toy_id)
        toy_socket = request.environ.get("wsgi.websocket")  # type:WebSocket
        if toy_socket:
            user_socket_dict[toy_id] = toy_socket
        while True:
            toy_data = toy_socket.receive()
            toy_data_dict = json.loads(toy_data)
            to_user = toy_data_dict.get("to_user")
    
            usocket = user_socket_dict.get(to_user)
            usocket.send(toy_data)
    
    @ws_app.route("/get_toy/")
    def get_toy():
    
        return  render_template("WebToy.html")
    
    
    
    
    
    
    
    if __name__ == '__main__':
        http_server = WSGIServer(("0.0.0.0",9528),ws_app,handler_class=WebSocketHandler)
        http_server.serve_forever()
    

      

  • 相关阅读:
    JQuery对id中含有特殊字符的转义处理
    jquery 将disabled的元素置为enabled的三种方法
    jeecg表单页面控件权限设置(请先看官方教程,如果能看懂就不用看这里了)
    Google调用explorer.exe打开本地文件
    C++ URLDecode和URLEncode实现——仅限gb2312,非utf8
    jeecg小吐槽续——自己折腾修改在线开发功能中“默认值”的使用
    jeecg小吐槽
    vue使用vant时间日期选择器,日期转化
    vue获取图片宽高
    微信公众号h5用户授权
  • 原文地址:https://www.cnblogs.com/qj696/p/11247601.html
Copyright © 2020-2023  润新知