弹幕代码:
@home.route("/tm/", methods=["GET", "POST"])
def tm():
import json
if request.method == "GET":
#获取弹幕消息队列
id = request.args.get('id')
key = "movie" + str(id)
if rd.llen(key):
msgs = rd.lrange(key, 0, 2999)
res = {
"code": 1,
"danmaku": [json.loads(v) for v in msgs]
}
else:
res = {
"code": 1,
"danmaku": []
}
resp = json.dumps(res)
if request.method == "POST":
#添加弹幕
data = json.loads(request.get_data())
msg = {
"__v": 0,
"author": data["author"],
"time": data["time"],
"text": data["text"],
"color": data["color"],
"type": data['type'],
"ip": request.remote_addr,
"_id": datetime.datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex,
"player": [
data["player"]
]
}
res = {
"code": 1,
"data": msg
}
resp = json.dumps(res)
rd.lpush("movie" + str(data["player"]), json.dumps(msg))
return Response(resp, mimetype='application/json')