学到弹幕了,但是添加的弹幕都没有反应。
来源:11-4 .电影弹幕
深圳_电信_王觉
2017-08-13
# home.view.py @home.route("/video/<int:id>/<int:page>", methods=["GET", "POST"]) def video(id=None, page=None): movie = Movie.query.join(Tag).filter( Tag.id == Movie.tag_id, Movie.id == int(id) ).first_or_404() comment_count = Comment.query.count() movie.playnum = movie.playnum + 1 if page is None: page = 1 page_data = Comment.query.join( Movie ).join( User ).filter( Movie.id == Comment.movie_id, User.id == Comment.user_id ).order_by( Comment.addtime.desc() ).paginate(page=page, per_page=10) form = CommentForm() if "user" in session and form.validate_on_submit(): data = form.data comment = Comment( content=data['content'], movie_id=movie.id, user_id=session['user_id'] ) db.session.add(comment) db.session.commit() movie.commentnum = movie.commentnum + 1 db.session.add(movie) db.session.commit() flash("添加评论成功", "success") return redirect(url_for("home.video", id=movie.id, page=1)) return render_template("home/video.html", movie=movie, form=form, page=1, page_data=page_data, comment_count=comment_count) @home.route("/tm/<int:id>/", methods=["GET", "POST"]) def tm(id=None): 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')
# __init__.py from flask import Flask,render_template from flask_sqlalchemy import SQLAlchemy import pymysql import os, sys from flask.ext.redis import FlaskRedis reload(sys) sys.setdefaultencoding("utf8") app = Flask(__name__) app.config["SQLALCHEMY_DATABASE_URI"] = "mysql+pymysql://root:@localhost:3306/douban" app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = True app.config["SECRET_KEY"] = '0123456789' app.config["UP_DIR"] = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static/uploads/") app.config["FC_DIR"] = os.path.join(os.path.abspath(os.path.dirname(__file__)), "static/uploads/users/") app.config['REDIS_URL'] = "redis://localhost:6379/0" app.debug = True db = SQLAlchemy(app) rd = FlaskRedis(app) from app.home import home as home_blueprint from app.admin import admin as admin_blueprint app.register_blueprint(home_blueprint) app.register_blueprint(admin_blueprint, url_prefix="/admin") @app.errorhandler(404) def page_not_found(error): return render_template("home/404.html"), 404
这一章节讲解的不是很清楚,老师稍后可以补充一下吗?比如redis的方法都还没能学到呢,谢谢老师,辛苦了
写回答
2回答
-
同学,你好
key表示redis队列的键,key值我们采用的形式是电影movie拼接电影ID
rd.llen(key)表示统计队列的长度
rd.lrange(key, 0, 2999)表示从redis队列中取出3000条数据
rd.lpush("movie" + str(data["player"]), json.dumps(msg))表示把数据压入队列
具体发送弹幕和获取弹幕的接口格式,我们可以参考dplayer官网的接口格式!
00 -
Ghost
2017-08-13
res = { "code": 1, "danmaku": [json.loads(v) for v in msgs] } data = json.loads(request.get_data())
我遇到过的问题是编码问题,这两个地方化需要先对数据解码
res = { "code": 1, "danmaku": [json.loads(v.decode('utf-8')) for v in msgs] } data = json.loads(request.get_data().decode('utf-8'))
具体的可以在网页进行调试观察
122017-09-11
相似问题