def get_video(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) all_video = db.session.query(Video).filter_by(uid=id_).order_by( db.desc(Video.upload_time)).all() video_list = [] if all_video: for video in all_video: video_item = { 'pv': video.id, 'title': video.title, 'type': video.type, 'duration': video.duration, 'likes': len(list(map(int, video.likes_user.split(',')))) if video.likes_user else 0, 'views': len(list(map(int, video.views.split(',')))) if video.views else 0, 'danmuku': video.danmuku, 'time': video.upload_time.strftime('%Y-%m-%d %H:%M:%S'), 'bucket_cover': get_bucket_token(video.cover) } video_list.append(video_item) data = {'video_list': video_list} return success(data=data, message="获取投稿成功")
def list_video_related(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video_list = [] video = db.session.query(Video).filter(Video.id == id_).first() all_video = db.session.query(Video).filter(Video.uid == video.uid).all() length = len(all_video) if length > 8: candidate_video = all_video[0:9] else: candidate_video = all_video[0:length] if video in candidate_video: candidate_video.remove(video) all_video = db.session.query(Video).filter(Video.id).filter( Video.type == 1).filter(Video.uid != video.uid).order_by( db.desc(Video.upload_time)).all() length = 8 - len(candidate_video) if len(all_video) <= 16 + length: video_temp = all_video[0:8 + length] for video_item in video_temp: candidate_video.append(video_item) else: i = 1 while i < length + 9 and i < len(all_video) + length: video_position = random.randint(0, length - 1) if all_video[video_position] in candidate_video: continue else: candidate_video.append(all_video[video_position]) i += 1 for video_item in candidate_video: user = db.session.query(User).filter_by(id=video_item.uid).first() video = { 'id': video_item.id, 'title': video_item.title, 'views': len(list(map(int, video_item.views.split(',')))) if video_item.views else 0, 'danmuku': video_item.danmuku, 'duration': video_item.duration, 'author_name': user.username, 'bucket_cover': get_bucket_token(video_item.cover) } video_list.append(video) data = {'video_list': video_list} return success(data=data, message="获取视频成功")
def get_cover(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video = db.session.query(Video).filter_by(id=id_).first() if video: cover_path = video.cover return success(message="视频封面", data=get_bucket_token(cover_path)) else: return params_error(message="未查到视频封面")
def get_background(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) user = db.session.query(User).filter_by(id=id_).first() if user: background_path = user.background return success(message="背景", data=get_bucket_token(background_path)) else: return params_error(message="未查到用户背景")
def get_video(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video = db.session.query(Video).filter_by(id=id_).first() user_id = g.user.uid if video: if video.views: views = list(map(int, video.views.split(','))) if user_id not in views: views.append(user_id) video.views = ','.join(str(i) for i in views) else: video.views = [user_id] video_path = video.video db.session.commit() return success(message="视频", data=get_bucket_token(video_path)) else: return params_error(message="未查到视频")
def upload_video_new(): if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = VideoNewUploadForm() if form.validate_for_api() and form.validate(): uid = g.user.uid user = db.session.query(User).filter_by(id=uid).first() if user.video_name_temp: bucket.delete_object(user.video_name_temp) # 保存视频 filename = form.filename.data if not filename.endswith('mp4') and not filename.endswith('mpeg'): return params_error(message="文件类型错误") time = datetime.now().strftime('%Y-%m-%d-%H-%M-%S') filename = 'uid-' + str(uid) + '-' + time + '-' + filename user.video_name_temp = filename db.session.commit() data = get_bucket_token(filename) return success(message="上传视频成功", data=data) else: return params_error(message=form.get_error())
def upload_avatar_new(): if request.method != 'POST': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) form = ImageUploadNewForm() if form.validate_for_api() and form.validate(): filename = form.filename.data uid = g.user.uid filename = secure_filename(filename) user = db.session.query(User).filter_by(id=uid).first() if user.avatar: bucket.delete_object(user.avatar) time = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') if filename.endswith('jpg'): avatar_name = str(uid) + '-' + time + '-avatar.jpg' elif filename.endswith('png'): avatar_name = str(uid) + '-' + time + '-avatar.png' else: return params_error(message="文件类型错误") data = {'bucket_avatar': get_bucket_token(avatar_name)} return success(data=data, message="上传头像成功") else: return params_error(message=form.get_error())
def get_details(id_): if request.method != 'GET': raise RequestMethodNotAllowed( msg="The method %s is not allowed for the requested URL" % request.method) video = db.session.query(Video).filter_by(id=id_).first() if video: title = video.title collections = video.collections coins = video.coins uid = video.uid author = db.session.query(User).filter_by(id=uid).first() if not author: return params_error(message="未查到作者信息") is_liked = False if video.likes_user: likes_user = list(map(int, video.likes_user.split(','))) if g.user.uid in likes_user: is_liked = True else: is_liked = False is_collected = False user = db.session.query(User).filter_by(id=uid).first() if user.collections: collections_list = list(map(int, user.collections.split(','))) if video.id in collections_list: is_collected = True else: is_collected = False is_followed = False if user.fans: fans = list(map(int, user.fans.split(','))) if g.user.uid in fans: is_followed = True data = { 'pv': video.id, 'title': title, 'type': video.type, 'sign': video.sign, 'duration': video.duration, 'likes': len(list(map(int, video.likes_user.split(',')))) if video.likes_user else 0, 'collections': collections, 'coins': coins, 'views': len(list(map(int, video.views.split(',')))) if video.views else 0, 'comments': video.comments, 'danmuku': video.danmuku, 'time': video.upload_time.strftime('%Y-%m-%d %H:%M:%S'), 'author': author.id, 'author_name': author.username, 'bucket_cover': get_bucket_token(video.cover), 'is_liked': is_liked, 'is_collected': is_collected, 'is_followed': is_followed } return success(message="详情", data=data) else: return params_error(message="未查到视频信息")