def unlike_video(): """取消赞视频 (GET|POST&LOGIN) :uri: /user/opt/like-video :param video_id: 被赞视频id :returns: {} """ user = request.authed_user vid = request.values.get('video_id', None) video = Video.get_one(vid) if not video: return error.VideoNotExist key = 'lock:unlike_video:%s:%s' % (str(user._id), vid) with util.Lockit(Redis, key) as locked: if locked: return error.LikeVideoFailed('取消赞失败') ulv = UserLikeVideo.get_by_ship(str(user._id), vid) ulv.delete_model() if ulv else None # 更新活动点赞数 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': {'like_count': -1}}) return {}
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 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 delete_videos(aid): """删除我的参赛视频(POST&LOGIN) :uri: activity/<string:aid>/delete/videos :param activity_video: 活动参赛视频id :return: {} """ user = request.authed_user avid = request.values.get('activity_video', None) activity_video = ActivityVideo.get_one(avid) if not activity_video: return error.ActivityVideoNotExist if str(activity_video.author) != str(user._id): return error.AuthFailed activity_video.delete_model() return {}
def activity_create_video(aid): """创建活动视频 (POST&LOGIN) :uri: activity/<string:aid>/new-video :param video_id: 原始视频id :returns: object """ user = request.authed_user vid = request.values['video_id'] video = Video.get_one(str(vid)) if not video: return error.VideoNotExist if str(user._id) != str(video.author): return error.AuthFailed activity_video = ActivityVideo.get_activity_video_by_vid(vid) if activity_video: return error.ActivityVideoExist activity_video = ActivityVideo.init() activity_video.title = video.title activity_video.video_id = ObjectId(vid) activity_video.like_count = video.like activity_video.comment_count = video.comment_count activity_video.vv = video.vv activity_video.author = ObjectId(str(user._id)) activity_video.activity_id = ObjectId(str(aid)) activity_video.cover = video.cover activity_video.duration = video.duration activity_video.game = video.game avid = activity_video.create_model() video.update_model({'$set': {'activity_ids': [avid]}}) # 任务检查 if user: UserTask.check_user_tasks(str(user._id), JOIN_COLLECT, 1, str(video.game), str(aid)) return ActivityVideo.get_one(avid, check_online=False).format()
def like_video(): """赞视频 (GET|POST&LOGIN) :uri: /user/opt/like-video :param video_id: 被赞视频id :returns: {} """ user = request.authed_user vid = request.values.get('video_id', None) video = Video.get_one(vid) if not video: return error.VideoNotExist ulv = UserLikeVideo.get_by_ship(str(user._id), vid) if not ulv: key = 'lock:like_video:%s:%s' % (str(user._id), vid) with util.Lockit(Redis, key) as locked: if locked: return error.LikeVideoFailed ulv = UserLikeVideo.init() ulv.source = ObjectId(str(user._id)) ulv.target = ObjectId(vid) _id = ulv.create_model() if _id: # 发送点赞消息 Message.send_video_msg(str(user._id), str(vid), 'like') # 更新活动点赞数 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': {'like_count': 1}}) return {}
def play_video(vid): """播放视频 (GET) :uri: /videos/<string:vid>/play :returns: redirect(real_url) """ ut = request.values.get("ut", None) uid = User.uid_from_token(ut) start = int(time.time() * 1000) video = Video.get_one(vid) if not video: result = { 'status': error.VideoNotExist.errno, 'errmsg': error.VideoNotExist.errmsg, 'data': {}, 'time': int(time.time() * 1000) - start, } return jsonify(result) video.update_model({'$inc': {'vv': 1}}) # 如果是栏目视频,给对应频道增加播放量 channel = ShowChannel.get_one(video.channel) channel and channel.update_model({'$inc': {'play_count': 1}}) # 观看视频任务检查 if uid: UserTask.check_user_tasks(uid, PLAY_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': {'vv': 1}}) return redirect(video.real_url())
def latest_video(aid): """获取最新参赛视频 :uri: activity/<string:aid>/video/current :param maxs: 最后时间, 0代表当前时间 :param nbr: 每页数量 :param device: 终端ID :returns: {'activity_videos': list, 'end_page': bool, 'activity_config': Object, '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) pagesize = int(params.get('nbr', 10)) activity_config = ActivityConfig.get_one(aid, check_online=False) if not activity_config: return error.ActivityNotExist avideos = list() avids = list() while len(avideos) < pagesize: avids = ActivityVideo.latest_video_ids(aid, pagesize, maxs) avideos.extend([v.format() for v in ActivityVideo.get_list(avids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = ActivityVideo.get_one(avids[-1]) if avids else None maxs = obj.create_at if obj else 1000 if len(avids) < pagesize: break else: break return { 'activity_videos': avideos, 'end_page': len(avids) != pagesize, 'activity_config': activity_config.format(), 'maxs': maxs }
def user_videos(aid): """获取我的参赛视频(GET&LOGIN) :uri: activity/<string:aid>/videos :param type: 活动结束时参数(type=end) :param device: 终端ID :return: {'activity_videos': list, 'activity_config': Object} """ user = request.authed_user type = request.values.get('type', None) activity_config = ActivityConfig.get_one(str(aid), check_online=False) if not activity_config: return error.ActivityNotExist sort = activity_config.sort activity_videos = ActivityVideo.user_compete_video_ids(aid, user._id) if not type: top_compete_videos = ActivityVideo.top_video_ids(aid, sort) videos = list() for activity_video in activity_videos: avid = str(activity_video['_id']) activity_video = ActivityVideo.get_one(avid) activity_video = activity_video.format() if type: videos.append(activity_video) continue if avid in top_compete_videos: top = top_compete_videos.index(avid) activity_video['top'] = top + 1 videos.append(activity_video) else: continue return { 'activity_videos': videos, 'activity_config': activity_config.format() }