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 game_videos(gid): """获取游戏的视频 (GET) :uri: /games/<string:gid>/videos :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :param orderby: 排序方式 ('create_at': 创建时间, 'vv':播放次数) :returns: {'videos': list, 'end_page': bool, 'maxs': timestamp} """ params = request.values orderby = params.get('orderby', 'create_at') maxs = params.get('maxs', None) if orderby == 'vv': maxs = 10000000 if maxs is not None and int( maxs) == 0 else maxs and int(maxs) else: 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)) videos = list() vids = list() while len(videos) < pagesize: if orderby == 'vv': vids = Video.game_hotvideo_ids(gid, page, pagesize, maxs) else: vids = Video.game_video_ids(gid, page, pagesize, maxs) ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] videos.extend( [v.format(exclude_fields=ex_fields) for v in Video.get_list(vids)]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = Video.get_one(vids[-1], check_online=False) if vids else None if orderby == 'vv': maxs = obj.vv if obj else -1 else: maxs = obj.create_at if obj else 1000 if len(vids) < pagesize: break else: break return {'videos': videos, 'end_page': len(vids) != pagesize, 'maxs': maxs}
def migu_game_popular_videos(bid): """获取游戏热门视频 (GET) :uri: /migu/games/<string:bid>/popular/ :param page: 页码 :param nbr: 每页数量 :returns: {'videos': list, 'end_page': bool} """ params = request.values page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) game = Game.get_by_bid(bid) if not game: return error.GameNotExist vids = Video.game_hotvideo_ids(str(game._id), page, pagesize) videos = [v.format() for v in Video.get_list(vids)] ret = {'videos': videos, 'end_page': len(vids) != pagesize} return ret
def game_popular_videos(gid): """获取游戏人气视频 (GET) :uri: /games/<string:gid>/videos :param page: 页码 :param nbr: 每页数量 :returns: {'videos': list, 'end_page': bool} """ params = request.values page = int(params.get('page', 1)) pagesize = int(params.get('nbr', 10)) videos = list() vids = Video.game_hotvideo_ids(gid, page, pagesize) ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] videos.extend( [v.format(exclude_fields=ex_fields) for v in Video.get_list(vids)]) return {'videos': videos, 'end_page': len(vids) != pagesize}
def home(): """获取首页信息 (GET) :uri: /home :param os: 平台 :param channels: 渠道(可选) :param version_code: 版本号 :returns: {'banners': list, 'categories': list, 'sub_games': list, 'hot_games': list, 'hot_lives': list} """ user = request.authed_user ret = dict() # banner广告 params = request.values os = params.get('os', None) channels = params.get('channels', None) version_code = int(params.get('version_code', 0)) no_gamefy = False # 不返回游戏风云视频及内容 if not os or not version_code: no_gamefy = True elif (os == 'ios' and version_code < 5920) or (os == 'android' and version_code < 416): no_gamefy = True uid = None province = None if user: uid = str(user._id) phone = str(user.phone) if user.province: province = user.province if not user.province and util.is_mobile_phone(phone): province = Migu.get_user_info_by_account_name(phone) if not isinstance(province, error.ApiError): user.update_model({'$set': {'province': province}}) else: province = None banners = list() _ids = Banner.all_banner_ids() for b in Banner.get_list(_ids): if b.os and b.os != os: continue if (b.version_code_mix and b.version_code_mix > version_code) or \ (b.version_code_max and b.version_code_max < version_code): continue if channels and b.channels and channels not in b.channels: continue if b.login == 'login' and (not uid or not b.user_in_group(str(b.group), uid)): continue if b.province and province and province not in b.province: continue banners.append(b.format()) if version_code == 0 and not banners: _ids = Banner.all_banners_by_version() banners = [b.format() for b in Banner.get_list(_ids)] ret['banners'] = banners support_cates = ['video', 'game', 'user', 'show-video'] if no_gamefy: support_cates.pop(-1) categories = [] cate_ids = HomeCategory.all_category_ids() cates = HomeCategory.get_list(cate_ids) for cate in cates: ids = HomeCategoryConfig.category_object_ids(str(cate._id)) if not ids or cate.ctype not in support_cates: continue _category = cate.format() if cate.ctype in ['video', 'show-video']: ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] if no_gamefy: _category['objects'] = [ v.format(exclude_fields=ex_fields) for v in Video.get_list(ids) if v.author ] else: _category['objects'] = [ v.format(exclude_fields=ex_fields) for v in Video.get_list(ids) ] elif cate.ctype == 'game': ex_fields = ['subscribed'] _category['objects'] = [ g.format(exclude_fields=ex_fields) for g in Game.get_list(ids) ] elif cate.ctype == 'user': ex_fields = ['is_followed'] _category['objects'] = [ u.format(exclude_fields=ex_fields) for u in User.get_list(ids) ] _category['objects'] = _category['objects'][:4] categories.append(_category) ret['categories'] = categories # 兼容老版本 ret['hottest_of_today'] = [] # 热门直播 all_lives = Xlive.get_all_lives() hot_lives = all_lives[:4] if len(all_lives) >= 4 else all_lives[:2] hot_lives = hot_lives if len(hot_lives) > 1 else [] ret['hot_lives'] = [Xlive.format(l) for l in hot_lives] # 用户已订阅游戏 sub_game_ids = [] if user: sub_game_ids = UserSubGame.sub_game_ids(str(user._id)) tmp = [] for game_id in sub_game_ids: ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] vids = GameRecommendVideo.game_video_ids(game_id) or [] videos = [ v.format(exclude_fields=ex_fields) for v in Video.get_list(vids) ] # 补齐4个人气视频 if len(videos) < 4: hot_vids = Video.game_hotvideo_ids(game_id, 1, 4) for _vid in hot_vids: if len(videos) == 4: break if _vid in vids: continue v = Video.get_one(_vid, check_online=True) v and videos.append(v.format(exclude_fields=ex_fields)) if videos: tmp.append({'game': videos[0]['game'], 'videos': videos}) else: tmp = list() ret['sub_games'] = tmp # 热门游戏 gids = HotGame.hot_game_ids() # 去掉用户已订阅游戏 gids = [gid for gid in gids if gid not in sub_game_ids] tmp = [] for game_id in gids: ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] vids = GameRecommendVideo.game_video_ids(game_id) or [] videos = [ v.format(exclude_fields=ex_fields) for v in Video.get_list(vids) ] # 补齐4个人气视频 if len(videos) < 4: hot_vids = Video.game_hotvideo_ids(game_id, 1, 4) for _vid in hot_vids: if len(videos) == 4: break if _vid in vids: continue v = Video.get_one(_vid, check_online=True) v and videos.append(v.format(exclude_fields=ex_fields)) if videos: tmp.append({'game': videos[0]['game'], 'videos': videos}) ret['hot_games'] = tmp return ret
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)