def get_share_video(vid): """获取视频分享数据(GET) :uri: /share/video/<string:vid> :returns: {'video': object, 'hot_videos': list, 'comments': comments, 'game_url': url, 'app_url': url} """ video = Video.get_one(vid) if not video: return error.VideoNotExist cids = Comment.video_comment_ids(vid, 1, 10) comments = [c.format() for c in Comment.get_list(cids)] vids = Video.game_hotvideo_ids(str(video.game), 1, 10) videos = [v.format() for v in Video.get_list(vids)] video = video.format() _from = request.values.get('ywfrom', None) mgyxdt_url = 'http://g.10086.cn/s/clientd/?t=GH_JFDX' game_url = video['game'][ 'url'] if _from != 'miguyouxidating' else mgyxdt_url app_url = "http://video.cmgame.com/userfiles/wapapp/mgyw.apk" app_url = app_url if _from != 'miguyouxidating' else '#' return dict(video=video, hot_videos=videos, comments=comments, game_url=game_url, app_url=app_url)
def format(self): from wanx.models.user import User from wanx.models.comment import Comment from wanx.models.video import Video operator = User.get_one(str(self.operator), check_online=False) if self.ctype == 'comment': obj = Comment.get_one(str(self.obj_id), check_online=False) obj = obj and obj.format() elif self.ctype == 'video': obj = Video.get_one(str(self.obj_id), check_online=False) obj = obj and obj.format() elif self.ctype == 'reply': obj = Reply.get_one(str(self.obj_id), check_online=False) comment_id = obj and str(obj.comment) obj = obj and obj.format() if obj: comment = Comment.get_one(comment_id, check_online=False) obj['video_id'] = comment.video and str(comment.video) else: obj = None data = { 'msg_id': str(self._id), 'ctype': self.ctype, 'obj': obj, 'action': self.action, 'operator': operator and operator.format(), 'create_at': self.create_at } return data
def video_comments(vid): """获取视频评论 (GET) :uri: /videos/<string:vid>/comments/ :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'comments': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values maxs = params.get('maxs', None) maxs = time.time() if maxs is not None and int(float(maxs)) == 0 else maxs and float(maxs) page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) comments = list() cids = list() next_maxs = None while len(comments) < pagesize: cids = Comment.video_comment_ids(vid, page, pagesize, maxs) comments.extend([c.format() for c in Comment.get_list(cids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = Comment.get_one(cids[-1], check_online=False) if cids else None next_maxs = obj.create_at if obj else 1000 if len(cids) < pagesize: break else: break end_page = len(cids) < pagesize if maxs is not None: gift_logs = UserGiftLog.get_video_gifts(vid, maxs, pagesize) comments.extend([gl.format() for gl in gift_logs]) comments = sorted(comments, key=lambda x: x['create_at'], reverse=True)[:pagesize] next_maxs = comments[-1]['create_at'] if comments else 1000 end_page = (len(cids) + len(gift_logs)) < pagesize # 评论增加3个回复 for comment in comments: if 'comment_id' in comment: rids = Reply.comment_reply_ids(comment['comment_id'], 1, 4, 0) comment['replies'] = [r.format() for r in Reply.get_list(rids)] comment['type'] = 'comment' else: comment['type'] = 'gift' return {'comments': comments, 'end_page': end_page, 'maxs': next_maxs}
def delete_comment(): """删除评论 (GET|POST&LOGIN) :uri: /user/opt/delete-comment :param comment_id: 评论id :returns: {} """ user = request.authed_user params = request.values cid = params.get('comment_id', None) comment = Comment.get_one(cid) if not comment: return error.CommentNotExist if str(comment.author) != str(user._id): return error.AuthFailed comment.delete_model() # 更新活动评论数 avideos = ActivityVideo.get_activity_video(vid=str(comment.video)) for avideo in avideos: ts = time.time() aconfig = ActivityConfig.get_one(str(avideo['activity_id']), check_online=False) if aconfig and aconfig.status == const.ACTIVITY_BEGIN \ and (aconfig.begin_at < ts and aconfig.end_at > ts): avideo = ActivityVideo.get_one(avideo['_id'], check_online=False) avideo.update_model({'$inc': {'comment_count': -1}}) return {}
def comment_replies(cid): """获取评论所有回复 (GET&LOGIN) :uri: /comment/<string:cid>/replies :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'replies': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values maxs = params.get('maxs', None) maxs = 1000 if maxs is not None and int(float(maxs)) == 0 else maxs and float(maxs) page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) comment = Comment.get_one(cid) if not comment: return error.CommentNotExist replies = list() rids = list() while len(replies) < pagesize: rids = Reply.comment_reply_ids(cid, page, pagesize, maxs) replies.extend([r.format() for r in Reply.get_list(rids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = Reply.get_one(rids[-1], check_online=False) if rids else None maxs = obj.create_at if obj else 1000000000000 if len(rids) < pagesize: break else: break return {'replies': replies, 'end_page': len(rids) != pagesize, 'maxs': maxs}
def like_comment(): """赞评论 (GET|POST&LOGIN) :uri: /user/opt/like-comment :param comment_id: 被赞评论id :returns: {} """ user = request.authed_user cid = request.values.get('comment_id', None) comment = Comment.get_one(cid) if not comment: return error.CommentNotExist ulc = UserLikeComment.get_by_ship(str(user._id), cid) if not ulc: key = 'lock:like_comment:%s:%s' % (str(user._id), cid) with util.Lockit(Redis, key) as locked: if locked: return error.LikeCommentFailed ulc = UserLikeComment.init() ulc.source = ObjectId(str(user._id)) ulc.target = ObjectId(cid) ulc.create_model() # 发送点赞消息 Message.send_comment_msg(str(user._id), str(cid), 'like') return {}
def migu_create_comment(vid): """咪咕平台创建评论 (GET|POST) :uri: /migu/videos/<string:vid>/comments/submit :param user_id: 咪咕用户id :param content: 评论内容 :returns: {} """ params = request.values openid = params.get('user_id', '') content = params.get('content', '') if len(openid) < 1 or len(content) < 1: return error.InvalidArguments video = Video.get_one(vid) if not video: return error.VideoNotExist user = User.get_platform_user('migu', openid) if not user: info = dict(name='$mg$%s%s' % (openid[-4:], random.randint(1000, 9999))) user = User.create_platform_user('migu', openid, data=info) comment = Comment.init() comment.author = ObjectId(str(user._id)) comment.content = content comment.video = ObjectId(vid) comment.create_model() return {}
def create_comment(): """创建评论 (GET|POST&LOGIN) :uri: /user/opt/submit-comment :param video_id: 被评论视频id :param content: 评论内容 :returns: {'comment': object} """ user = request.authed_user params = request.values vid = params.get('video_id', None) content = params.get('content', None) if not vid or not content: return error.InvalidArguments if not Video.get_one(vid): return error.VideoNotExist # 敏感词检查 if Spam.filter_words(content, 'comment'): return error.InvalidContent comment = Comment.init() comment.author = ObjectId(str(user._id)) comment.content = content comment.video = ObjectId(vid) cid = comment.create_model() if cid: # 发送评论消息 Message.send_video_msg(str(user._id), str(vid), 'comment') # 任务检查 if user: UserTask.check_user_tasks(str(user._id), COMMENT_VIDEO, 1) # 更新活动评论数 avideos = ActivityVideo.get_activity_video(vid=vid) for avideo in avideos: ts = time.time() aconfig = ActivityConfig.get_one(str(avideo['activity_id']), check_online=False) if aconfig and aconfig.status == const.ACTIVITY_BEGIN \ and (aconfig.begin_at < ts and aconfig.end_at > ts): avideo = ActivityVideo.get_one(avideo['_id'], check_online=False) avideo.update_model({'$inc': {'comment_count': 1}}) return Comment.get_one(str(cid)).format()
def migu_video_comments(vid): """获取视频评论 (GET) :uri: /migu/videos/<string:vid>/comments/ :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'comments': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values maxs = params.get('maxs', None) maxs = time.time() if maxs is not None and int( float(maxs)) == 0 else maxs and float(maxs) page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) comments = list() cids = list() next_maxs = None while len(comments) < pagesize: cids = Comment.video_comment_ids(vid, page, pagesize, maxs) comments.extend([c.format() for c in Comment.get_list(cids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = Comment.get_one(cids[-1], check_online=False) if cids else None next_maxs = obj.create_at if obj else 1000 if len(cids) < pagesize: break else: break end_page = len(cids) < pagesize # 评论增加3个回复 for comment in comments: rids = Reply.comment_reply_ids(comment['comment_id'], 1, 4, 0) comment['replies'] = [r.format() for r in Reply.get_list(rids)] return {'comments': comments, 'end_page': end_page, 'maxs': next_maxs}
def send_comment_msg(cls, operator_id, obj_id, action='like'): owner_id = str(Comment.get_one(obj_id).author) obj = cls.init() obj.owner = ObjectId(owner_id) obj.ctype = 'comment' obj.obj_id = ObjectId(obj_id) obj.action = action obj.operator = ObjectId(operator_id) msg_id = obj.create_model() # 发送消息到队列 channel = User.USER_ASYNC_MSG % ({'uid': owner_id}) msg = dict(obj_type='Message', obj_id=str(msg_id), count=1) MRedis.publish(channel, json.dumps(msg))
def create_reply(): """创建评论的回复 (GET|POST&LOGIN) :uri: /replies/create :param comment_id: 评论id(可选) :param reply_id: 被用户回复的评论回复id(可选) :param content: 回复内容 :returns: {'reply': object} """ user = request.authed_user params = request.values cid = params.get('comment_id', None) reply_id = params.get('reply_id', None) content = params.get('content', None) if not content or (not cid and not reply_id): return error.InvalidArguments comment = Comment.get_one(cid) if cid else None if cid and not comment: return error.CommentNotExist reply = Reply.get_one(reply_id) if reply_id else None if reply_id and not reply: return error.ReplyNotExist # 敏感词检查 if Spam.filter_words(content, 'comment'): return error.InvalidContent new_reply = Reply.init() new_reply.owner = user._id new_reply.reply = reply._id if reply else None new_reply.comment = reply.comment if reply else comment._id new_reply.content = content rid = new_reply.create_model() if rid: # 发送消息 if reply: Message.send_reply_msg(str(user._id), str(reply._id), 'reply') else: Message.send_comment_msg(str(user._id), str(cid), 'reply') return {'reply': Reply.get_one(str(rid)).format() if rid else None}
def unlike_comment(): """取消赞评论 (GET|POST&LOGIN) :uri: /user/opt/unlike-comment :param comment_id: 被赞评论id :returns: {} """ user = request.authed_user cid = request.values.get('comment_id', None) comment = Comment.get_one(cid) if not comment: return error.CommentNotExist key = 'lock:unlike_comment:%s:%s' % (str(user._id), cid) with util.Lockit(Redis, key) as locked: if locked: return error.LikeCommentFailed('取消赞评论失败') ulc = UserLikeComment.get_by_ship(str(user._id), cid) ulc.delete_model() if ulc else None return {}
def report(): """举报 :uri: /user/report :param target_id: 被举报id :param type: 举报原因 :param content: 举报内容 :param source: 举报来源,0:来自视频,1:来自直播,2:来自评论,3:来自回复 :returns: {} """ params = request.values tid = params.get('target_id', None) _type = int(params.get('type', 0)) content = params.get('content', None) source = int(params.get('source', 0)) from_user = request.authed_user if not tid or not _type or source not in range(4): return error.InvalidArguments if source == 0: video = Video.get_one(tid) if not video: return error.VideoNotExist uid = video.author title = video.title elif source == 1: live = Xlive.get_live(tid) if not live: return error.LiveError('直播不存在') uid = live.get('user_id') title = live.get('name') elif source == 2: comment = Comment.get_one(tid) if not comment: return error.CommentNotExist uid = comment.author title = comment.content elif source == 3: reply = Reply.get_one(tid) if not reply: return error.ReplyNotExist uid = reply.owner title = reply.content key = 'lock:reports:object:%s' % (tid) with util.Lockit(Redis, key) as locked: if locked: return error.ReportVideoFailed('举报失败,请稍后再试') if ReportVideo.check_reported(tid, source, from_user._id): return error.ReportVideoExists rv = ReportVideo.init() rv.uid = uid rv.vid = tid rv.type = _type rv.content = content if content else '' rv.source = source rv.from_user = from_user._id rv.status = 0 rv.deleted = 0 rv.bannef = 0 rv.create_model() rpc_id = ReportConfig.get_limit_config(source) rpconfig = ReportConfig.get_one(rpc_id) if not rpconfig: return {} max_limit = rpconfig.max_limit if max_limit and ReportVideo.reach_max_limit(tid, max_limit): _templates = [ u'用户ID %(id)s,标题为"%(title)s"的视频正在被举报,请尽快审核', u'房间号为%(id)s,标题为"%(title)s"的直播正在被举报,请尽快审核', u'用户%(id)s针对"%(title)s"视频的评论正在被举报,请尽快审核', u'用户%(id)s针对"%(title)s"评论的回复正在被举报,请尽快审核' ] _id = tid if source == 1 else uid try: job = 'send_sms' data = dict(group=str(rpconfig.group), content=_templates[source] % { 'id': str(_id), 'title': title }) gm_client = gearman.GearmanClient( app.config['GEARMAN_SERVERS']) gm_client.submit_job(job, json.dumps(data), background=True, wait_until_complete=False, max_retries=5) except gearman.errors.GearmanError: pass except Exception, e: print_log('send_sms_err', str(e))
def share_page(vid): video = Video.get_one(vid) if not video: abort(404) cids = Comment.video_comment_ids(vid, 1, 10) comments = [c.format() for c in Comment.get_list(cids)] vids = Video.game_hotvideo_ids(str(video.game), 1, 10) recommend_videos = [v.format() for v in Video.get_list(vids)] video = video.format() _from = request.values.get('ywfrom', None) mgyxdt_url = 'http://g.10086.cn/s/clientd/?t=GH_JFDX' download_url = video['game'][ 'url'] if _from != 'miguyouxidating' else mgyxdt_url video_dict = { "nick_name": video['author'] and video['author']['nickname'], "user_logo": video['author'] and video['author']['logo'], "title": video['title'], "description": video['title'], "created_time": stamp_str(video['create_at']), "url": str(video['url']), "image_url": str(video['cover']), "favour": video.get("like_count", "0"), "comment": video['comment_count'], "vv": video['vv'], "game_download_url": download_url, "game_logo": video['game'] and video['game']['icon'], "game_name": video['game'] and video['game']['name'], "game_description": video['game'] and video['game']['description'], "game_video_count": video['game'] and video['game']['video_count'], "app_logo": video['game']['icon'], } comments_list = [] for c in comments: comments_list.append({ "nick_name": c['author'] and c['author']['nickname'], "user_icon": c['author'] and c['author']['logo'], "created_at": stamp_str(c['create_at']), "content": c['content'], "favour": c['like'] }) recommend_videos_list = [] for v in recommend_videos: recommend_videos_list.append({ "share_url": v['share_url'], "title": v['title'], "image_url": v['cover'], "video_id": v['video_id'], "vv": v['vv'] }) return render_template("share.html", static_url=app.config['STATIC_URL'], video=video_dict, comments=comments_list, recommends=recommend_videos_list, ywfrom=_from)