def tm(): """ 弹幕 """ # 获取弹幕消息 if request.method == "GET": id = request.args.get('id') key = "movie" + str(id) log('rd.llen(key)', rd.llen(key)) if rd.llen(key): msgs = rd.lrange(key, 0, 2999) log('msgs', rd.lrange(key, 0, 2999)) res = { "code": 1, "danmaku": [json.loads(v.decode('utf-8')) for v in msgs] } else: res = {"code": 1, "danmaku": []} resp = json.dumps(res) # 提交新弹幕 if request.method == "POST": d = request.get_data().decode('utf-8') log('d', d) data = json.loads(d) 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')
def danmu(): 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, 'danmuku': []} 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.now().strftime("%Y%m%d%H%M%S") + 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')
def tm(): """ 弹幕消息处理 """ import json if request.method == "GET": # 获取弹幕消息队列 id = request.args.get('id') # 存放在redis队列中的键值 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.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex, "player": [data["player"]] } res = {"code": 1, "data": msg} resp = json.dumps(res) # 将添加的弹幕推入redis的队列中 rd.lpush("movie" + str(data["player"]), json.dumps(msg)) return Response(resp, mimetype='application/json')
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': data['type'], '_id': 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')
def tm(): if request.method == "GET": # 获取弹幕消息队列 id = request.args.get("id") global key key = "movie" + str(id) if rd.llen(key): msgs = rd.lrange(key, 0, 2999) res = { "code": 0, "data": [eval(v.decode("utf8")) for v in msgs] } else: res = { "code": 0, "data": [] } resp = json.dumps(res) if request.method == "POST": # 添加弹幕 data = json.loads(request.get_data()) msg = [data["time"], data["type"], data["color"], data["author"], data["text"], request.remote_addr, datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex] res = { "code": 0, "data": msg } resp = json.dumps(res) rd.lpush(key, json.dumps(msg)) return Response(resp, mimetype="application/json")
def tm(): from flask import Response from app import rd import json import datetime import time resp = '' if request.method == "GET": # 获取弹幕 movie_id = request.args.get('id') # 用id来获取弹幕消息队列,也就是js中danmaku配置的id key = "movie{}:barrage".format(movie_id) # 拼接形成键值用于存放在redis队列中 if rd.llen(key): msgs = rd.lrange(key, 0, 2999) tm_data = [] for msg in msgs: msg = json.loads(msg) # print(msg) tmp_data = [msg['time'], msg['type'], msg['date'], msg['author'], msg['text']] tm_data.append(tmp_data) # print(tm_data) res = { "code": 0, # 参照官网http://dplayer.js.org/#/ 获取弹幕的消息格式 # "data": [[6.978, 0, 16777215, "DIYgod", "1111111111111111111"], # [16.338, 0, 16777215, "DIYgod", "测试"], # [8.177, 0, 16777215, "DIYgod", "测试"], # [7.358, 0, 16777215, "DIYgod", "1"], # [15.748338, 0, 16777215, "DIYgod", "owo"]], "data": tm_data, } else: print('Redis中暂无内容') res = { "code": 1, # 无内容code为1 "data": [] } resp = json.dumps(res) if request.method == "POST": # 添加弹幕 data = json.loads(request.get_data()) # print(data) msg = { "__v": 0, "author": data["author"], "time": data["time"], # 发送弹幕视频播放进度时间 "date": int(time.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['id'] } res = { "code": 0, "data": msg } resp = json.dumps(res) rd.lpush("movie{}:barrage".format(data['id']), json.dumps(msg)) # 将添加的弹幕推入redis的队列中 return Response(resp, mimetype='application/json')
def danmu(): if request.method == "GET": id = request.args.get("id") key = "movie" + str(id) """ 弹幕格式 { "code": 0, "data": [ [ time, type, color, author, text ] ] } """ if rd.llen(key): msgs = rd.lrange(key, 0, 2999) res = { "code": 0, "data": [[ json.loads(i)["time"], json.loads(i)["type"], json.loads(i)["color"], json.loads(i)["author"], json.loads(i)["text"] ] for i in msgs] } else: res = {"code": 0, "data": []} res_json = json.dumps(res) if request.method == "POST": data = json.loads(request.get_data()) msg = { "id": data["id"], "author": data["author"], "time": data["time"], "text": data["text"], "color": data["color"], "type": data["type"] } res = {"code": 1, "data": msg} res_json = json.dumps(res) rd.lpush("movie" + str(data["id"]), json.dumps(msg)) return Response(res_json, mimetype="application/json")
def save_history(user_id, query_url, addtime): value = { 'user': user_id, 'url': query_url, 'addtime': addtime.strftime('%Y-%m-%d %H:%M:%S') } value = value.__repr__() rd.rpush(Config.REDIS_HISTORY, value) if rd.llen(Config.REDIS_HISTORY) > Config.HISTORY_MAX_LEN: rd.lpop(Config.REDIS_HISTORY) return True
def danmu(): if request.method == "GET": # 获取弹幕队列 id = request.args.get('id') # 存放在redis队列中的键值 key = "video:" + str(id) # Llen 获取列表长度 if rd.llen(key): # Lrange 获取列表指定范围内的元素 msgs = rd.lrange(key, 0, 2999) # msgs 是 bytes 的 list, json.loads(v) 得到 单条弹幕的 dict dict_list = [json.loads(v) for v in msgs] res = { "code": 0, "danmaku": [danmu_factory(d) for d in dict_list] } else: res = { "code": 0, "danmaku": [] } elif request.method == "POST": # 添加弹幕, 将 JSON 转换回 Python 数据结构 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, "referer": request.base_url, "_id": datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex, "player": [ data["player"] ] } res = { "code": 0, "data": msg, "msg": '发送弹幕成功' } # 将添加的弹幕作为值转换为 JSON 推入redis的队列中 # Lpush 将一个或多个值插入到列表头部 rd.lpush("video:" + str(data["player"]), json.dumps(msg)) resp = json.dumps(res) return Response(resp, mimetype='application/json')
def tm(): import json if request.method == "GET": #获取弹幕消息队列 id = request.args.get('id') key = "movie" + str(id) # 如果长度大于0,取3000条 if rd.llen(key): msgs = rd.lrange(key, 0, 2999) # 封装一个json的响应信息 res = {"code": 1, "danmaku": [json.loads(v) for v in msgs]} else: res = {"code": 1, "danmaku": []} resp = json.dumps(res) if request.method == "POST": #添加弹幕 print(request.get_data().decode()) data = json.loads(request.get_data().decode()) 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) # print(2) # 将其推入到redis的队列 当中 rd.lpush("movie", json.dumps(msg)) # print(1) return Response(resp, mimetype='application/json')
def tm(): import json if request.method == "GET": # 获取弹幕消息队列 mid = request.args.get("id") key = "movie" + str(mid) if rd.llen(key): msgs = rd.lrange(key, 0, 2999) res = {"code": 0, "data": [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, "_id": datetime.datetime.now().strftime("%Y%m%d%H%M%S") + uuid.uuid4().hex, "author": data["author"], "time": data["time"], "text": data["text"], "color": data["color"], "type": data["type"], "ip": request.remote_addr, "player": data["id"] } res = {"code": 0, "danmaku": msg} resp = json.dumps(res) msg = [ data["time"], data["type"], data["color"], data["author"], data["text"] ] rd.lpush("movie" + str(data["id"]), json.dumps(msg)) return Response(resp, mimetype="application/json")
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')
def tm(): import json if request.method == "GET": #Get old bullet message queue 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": #Add new bullet message queue 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)) #add bullet message to redis queue return Response(resp, mimetype='application/json')
def dan_man(): import json if request.method == 'GET': id = request.args.get('id') key = 'movie' + str(id) # 拼接key值 if rd.llen(key): # 如果有 msgs = rd.lrange(key, 0, 2999) res = {"code": 1, "danmuku": [json.loads(v) for v in msgs]} else: res = {"code": 1, "danmuku": []} 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')
def get_history(): return rd.lrange(Config.REDIS_HISTORY, 0, rd.llen(Config.REDIS_HISTORY) - 1)