def edit(): '''创建文章''' args = request.form print(args) id = args.get('id', None) if id: Video.update_video_by_id(**args) else: print('create') Video.create_video(**args) return return_model()
def delete_resource(cls, res_id, res_type): res = None if res_type == BaseConfig.TYPE_VIDEO_PLAY: res = Video.update_video_by_id( id=res_id, is_del=1 ) elif res_type == BaseConfig.TYPE_ARTICLE: res = Article.update_article_by_id( id=res_id, is_del=1 ) elif res_type == BaseConfig.TYPE_IMAGE: res = Image.update_image_by_id( id=res_id, is_del=1 ) elif res_type == BaseConfig.TYPE_AUDIO: res = Audio.update_audio_by_id( id=res_id, is_del=1 ) elif res_type == BaseConfig.TYPE_PUSH: res = Push.update_push_by_id( id=res_id, is_del=1 ) elif res_type == BaseConfig.TYPE_COLLECTION: res = Collection.update_by_id( id=res_id, is_del=1 ) return res
def list(): '''文章列表''' args = request.args page = http_util.get_param_int(args, 'page', BaseConfig.DEFAULT_PAGE) per_page = http_util.get_param_int(args, 'per_page', BaseConfig.DEFAULT_PER_PAGE) paginate = Video.query_video_paginate( user_id="", is_del=0, page=page, per_page=per_page ) videos = [] for item in paginate.items: detail = item.to_json() # detail['id'] = "{}".format(detail['id']) videos.append(detail) res = http_util.make_page_response(videos, paginate.total, page, per_page) return return_model( data=res )
def get_resource_list(cls, res_type): res = [] if res_type == BaseConfig.TYPE_VIDEO_PLAY: res = Video.get_videos() elif res_type == BaseConfig.TYPE_ARTICLE: res = Article.get_articles() return res
def get_videos(**params): videos = Video.query_videos(**params) print(videos) items = [] for item in videos: items.append(item.to_json()) return items
def related_video(): args = request.args key = http_util.check_params(args, 'res_id') if key: return http_util.return_param_not_found(key) res_id = args.get('res_id') # TODO 需要真正的相关逻辑 res = Video.query_related_videos(res_id) return http_util.return_model(data=res)
def save_video(): user_id = g.user_id args = request.json url = http_util.get_param(args, 'url', "") poster = http_util.get_param(args, 'poster', "") video = Video.create_video(user_id=user_id, url=url, poster=poster) # 记录用户上传视频 Action.create_action(user_id=user_id, type=BaseConfig.TYPE_ACTION_UPLOAD, res_id=video.id, res_type=BaseConfig.TYPE_VIDEO_PLAY) detail = Video.get_video_detail(video.id) User.update_user_by_id(id=user_id, last_upload=detail) return http_util.return_model()
def sync_video_poster(): items = Video.query_videos(is_del=0) for item in items: poster = item.poster print(poster) try: res = requests.get(poster) print(res) if res.status_code == 200: suffix = poster.rsplit('.', 1)[1] datetime_root = time.strftime("%Y%m%d/%H/%M%S", time.localtime()) url = put_object(key='{}/{}.{}'.format(datetime_root, int(time.time()), suffix), data=res.content, bucket_name=BaseConfig.ALIYUN_BUCKET_IMG) Video.update_video_by_id(id=item.id, poster=url) print(url) except BaseException as e: print(e)
def sync_index_by_type(type): """根据type刷新索引""" args = make_search_args(type) items = [] if type == BaseConfig.TYPE_USER: items = User.get_normal_users() elif type == BaseConfig.TYPE_VIDEO_PLAY: items = Video.get_videos() elif type == BaseConfig.TYPE_ARTICLE: items = Article.get_articles() elif type == BaseConfig.TYPE_AUDIO: items = Audio.get_items() elif type == BaseConfig.TYPE_COLLECTION: items = Collection.get_items() else: return False es.delete_index_by_type(**args) for item in items: args['id'] = item['res_id'] item = format_item(item) args['body'] = item es.sync_index(**args)
def change_online_status(cls, res_id, res_type, is_online): res = None if res_type == BaseConfig.TYPE_VIDEO_PLAY: res = Video.update_video_by_id( id=res_id, is_online=is_online ) elif res_type == BaseConfig.TYPE_ARTICLE: res = Article.update_article_by_id( id=res_id, is_online=is_online ) elif res_type == BaseConfig.TYPE_IMAGE: res = Image.update_image_by_id( id=res_id, is_online=is_online ) elif res_type == BaseConfig.TYPE_AUDIO: res = Audio.update_audio_by_id( id=res_id, is_online=is_online ) return res
def get_resource_detail(cls, res_id, res_type, source_include=[], **params): """获取资源详情""" detail = None login_user_id = g.user_id if res_type == BaseConfig.TYPE_VIDEO_PLAY: detail = Video.get_video_detail(res_id) elif res_type == BaseConfig.TYPE_ARTICLE: detail = Article.get_article_detail(res_id) if not detail: app.logger.error('{} not found'.format(res_id)) return None del detail['content'] elif res_type == BaseConfig.TYPE_AUDIO: detail = Audio.get_audio_detail(res_id) elif res_type == BaseConfig.TYPE_IMAGE: detail = Image.get_image_detail(res_id) elif res_type == BaseConfig.TYPE_COLLECTION: detail = Collection.get_collection_detail(res_id) subs = [] if 'items' in source_include: items = CollectionResource.query_items(collection_id=res_id) for item in items: sub_id = item.res_id sub_type = item.res_type sub_detail = Resource.get_resource_detail( res_id=sub_id, res_type=sub_type, source_include=['comment_count', 'view_count', 'like_count'], login_user_id=login_user_id ) if sub_detail: subs.append(sub_detail) detail['items'] = subs if not detail: return None # 添加评论 if 'comments' in source_include: comments = get_comments( res_id=res_id, start=BaseConfig.MAX_START, per_page=5 ) detail['comments'] = comments # 相关视频 if 'related_items' in source_include and res_type != BaseConfig.TYPE_COLLECTION: related_videos = Video.query_related_videos(res_id) detail['related_items'] = related_videos ext = detail.get('ext', {}) # 观看数量 if 'view_count' in source_include: count = Action.query_count( type=BaseConfig.TYPE_ACTION_VIEW, res_id=res_id ) ext['view_count'] = count # 点赞数量 if 'like_count' in source_include: count = LikeInfo.query_count( res_id=res_id, res_type=res_type ) ext['like_count'] = count # 评论数量 if 'comment_count' in source_include: count = Comment.query_count( res_id=res_id, res_type=res_type ) ext['comment_count'] = count detail['ext'] = ext # 关注状态 detail['is_like'] = 0 if login_user_id: is_like = LikeInfo.query_like( user_id=login_user_id, res_id=res_id, res_type=res_type ) if is_like: detail['is_like'] = 1 return detail