def migu_home(): """获取首页信息 (GET) :uri: /migu/home/ :returns: {'popular': list, 'whats_new': list, 'hottest_of_today': list, 'recommend': list, 'tags': list} """ ret = dict() ret['popular'] = [] ret['whats_new'] = [] ret['hottest_of_today'] = [] ret['recommend'] = [] tags = [] cids = Category.all_category_ids() categories = Category.get_list(cids) for category in categories: gids = CategoryGame.category_game_ids(str(category._id)) games = [g.format() for g in Game.get_list(gids)] tags.append( dict(games=games, tag_id=str(category._id), name=category.name)) ret['tags'] = tags return ret
def recommend_subscribe_games(): """获取推荐订阅游戏 (GET) :uri: /recommend/subscribe/games :returns: {'games': list} """ gids = GameRecommendSubscribe.recommend_sub_ids() games = [g.format() for g in Game.get_list(gids)] return {'games': games}
def hot_games(): """获取游戏页的所有热门游戏 (GET) :uri: /games/hot_games :returns: {'games': list} """ gids = HotRecommendGame.hot_game_ids() games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] return {'games': games}
def online_games(): """获取所有在线游戏列表 (GET) :uri: /games :return: {'games': list} """ games = Game.online_games() gids = [str(gid) for gid, _ in games] games = [g.format() for g in Game.get_list(gids)] return {'games': games}
def category_games(cid): """获取分类下的所有游戏 (GET) :uri: /tags/<string:cid>/games :uri migu: /migu/tags/<string:cid>/games/ :returns: {'games': list} """ gids = CategoryGame.category_game_ids(cid) games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] return {'games': games}
def topic_games(): """获取专题游戏 (GET) :uri: /games/topic_games :param: topic_id: 专题ID :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :return: {'games': <Game>list, 'topic':<GameTopic>, 'end_page': bool, 'maxs': timestamp} """ tid = request.values.get('topic_id', None) maxs = request.values.get('maxs', None) page = int(request.values.get('page', 1)) pagesize = int(request.values.get('nbr', 10)) if not tid: return error.InvalidArguments topic = GameTopic.get_one(tid) if not topic: return error.GameTopicNotExist # 增加访问次数 count = int(topic.get('visitor_count', 0) + 1) topic.update_model({'$set': {'visitor_count': count}}) topic.visitor_count = count topic = topic.format() games = list() while len(games) < pagesize: ids = TopicGame.topic_game_ids(tid, page, pagesize, maxs) tgids, gids = [i[0] for i in ids], [i[1] for i in ids] gms = [g.format() for g in Game.get_list(gids)] games.extend(gms) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = TopicGame.get_one(tgids[-1], check_online=False) if tgids else None maxs = obj.order if obj else 0 if len(gids) < pagesize: break else: break return { 'games': games, 'topic': topic, 'end_page': len(gids) != pagesize, 'maxs': maxs }
def pop_game(): """设置常用游戏 (GET|POST&LOGIN) :uri: /user/opt/popular_game :param game_id: 游戏id(批量订阅游戏id以逗号隔开) :returns: {} """ user = request.authed_user gids = request.values.get('game_id', None) uid = UserPopularGame.get_user_id(user._id) game_ids_list = UserPopularGame.get_game_ids(user._id) if not gids: for game_id in game_ids_list: usg = UserPopularGame.get_by_ship(str(user._id), str(game_id)) usg.delete_model() if usg else None if not uid: usg = UserPopularGame.init() usg.source = ObjectId(str(user._id)) usg.create_model() return {} gids = [gid.strip() for gid in gids.split(',')] games = Game.get_list(gids, check_online=False) if not games: return error.GameNotExist key = 'lock:popgame:%s' % (str(user._id)) with util.Lockit(Redis, key) as locked: if locked: return error.PopGameFailed # 删除游戏 for game_id in game_ids_list: # if game_id not in gids: usg = UserPopularGame.get_by_ship(str(user._id), str(game_id)) usg.delete_model() if usg else None # 保存游戏 for index, game in enumerate(games): # usg = UserPopularGame.get_by_ship(str(user._id), str(game._id)) # if not usg: usg = UserPopularGame.init() usg.source = ObjectId(str(user._id)) usg.target = ObjectId(str(game._id)) usg.order = index usg.create_model() return {}
def format(self): _ids = GameRecommendConfigAdmin.all_module_game_ids(str(self._id)) games = [g.format() for g in Game.get_list(_ids)] if _ids else [] data = { 'module_id': str(self._id), 'module_label': urljoin(app.config.get("MEDIA_URL"), self.module_label), 'module_name': self.module_name, 'display_position': self.display_position, 'display_form': self.display_form, 'begin_at': self.begin_at, 'expire_at': self.expire_at, 'games': games } return data
def get_category_games(): """获取直播分类列表(游戏) (GET) :uri: /lives/cate-games :returns: {'games': list} """ user = request.authed_user gids = LiveHotGame.hot_game_ids() if user: for sgid in UserSubGame.sub_game_ids(str(user._id)): if sgid not in gids: gids.append(sgid) games = [g.format() for g in Game.get_list(gids)] return {'games': games}
def sub_game_videos(uid): """获取用户已订阅游戏的视频 (GET) :uri: /users/<string:uid>/subscriptions :returns: {'videos': list} """ gids = UserSubGame.sub_game_ids(uid) games = Game.get_list(gids) ret = [] for game in games: vids = Video.game_video_ids(str(game._id), 1, 4, time.time()) ex_fields = [ 'is_favored', 'is_liked', 'author__is_followed', 'game__subscribed' ] videos = [ v.format(exclude_fields=ex_fields) for v in Video.get_list(vids) ] ret.append({'game': game.format(), 'videos': videos}) return {'videos': ret}
def chess_games(): """获取棋牌分类下的游戏 (GET) :uri: /games/chess_games :param: chess_id: 棋牌分类ID :return: {'games': <Game>list, 'chess_config':<ChessConfig>} """ tid = request.values.get('chess_id', None) if not tid: return error.InvalidArguments chess_config = ChessConfig.get_one(tid) if not chess_config: return error.ChessConfigNotExist ids = ChessGames.chess_game_ids(tid) games = [g.format() for g in Game.get_list(ids)] return {'games': games, 'chess_config': chess_config.format()}
def vip_games(): """获取会员专区下的游戏 (GET) :uri: /games/vip_games :param: vip_id: 会员专区ID :return: {'games': <Game>list, 'vip_config':<VipConfig>} """ tid = request.values.get('vip_id', None) if not tid: return error.InvalidArguments vip_config = VipConfig.get_one(tid) if not vip_config: return error.VipConfigNotExist ids = VipGames.vip_game_ids(tid) games = [g.format() for g in Game.get_list(ids)] return {'games': games, 'vip_config': vip_config.format()}
def toplist_games(): """获取排行游戏 (GET) :uri: /games/toplist_games :param: toplist_id: 排行ID :return: {'games': <Game>list, 'toplist':<TopList>list} """ tid = request.values.get('toplist_id', None) if not tid: return error.InvalidArguments toplist = TopList.get_one(tid) if not toplist: return error.GameTopListNotExist ids = TopListGames.toplist_game_ids(tid) games = [g.format() for g in Game.get_list(ids)] return {'games': games, 'toplist': toplist.format()}
def get_hot_games(): """获取常用游戏列表(游戏) (GET) :uri: /lives/hot_games :returns: {'games': list} """ user = request.authed_user if user: uid = UserPopularGame.get_user_id(user._id) gids = UserPopularGame.get_game_ids(user._id) if not gids: if uid: gids = None else: gids = LiveHotGame.hot_game_ids() else: gids = LiveHotGame.hot_game_ids() games = [g.format() for g in Game.get_list(gids)] return {'games': games}
def channel_game(): """获取频道内容 (GET) :uri: /channels/games :returns: [{'tag_id': string, 'name': string, 'games': list}, ...] """ ret = list() cids = Category.all_category_ids() categories = Category.get_list(cids) for category in categories: gids = CategoryGame.category_game_ids(str(category._id)) games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] ret.append( dict(games=games, tag_id=str(category._id), name=category.name, icon_type=category.icon_type or 'icon')) return ret
def live_games(): """ 获取直播标签游戏列表 (GET) :uri: /user/opt/live_games :return: {'games': list} """ user = request.authed_user games = Game.live_games() # 设置直播标签的游戏 gids = [str(gid) for gid, _ in games] if user: pids = UserPopularGame.get_game_ids(user._id) if pids: [gids.remove(p) for p in pids if p in gids] else: pids = LiveHotGame.hot_game_ids() if pids: [gids.remove(p) for p in pids if p in gids] games = [g.format() for g in Game.get_list(gids)] return {'games': games}
def module_games(): """获取分类下的所有游戏 (GET) :uri: /games/module_games :param module_id: 游戏模块id :returns: {'games': list} """ params = request.values module_id = params.get('module_id', None) if not module_id: return error.InvalidArguments module = GameModule.get_one(module_id) if not module: return error.GameModuleNotExist gids = ModuleGame.module_game_ids(module_id) games = [ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids) ] return {'games': games}
def user_sub_games(uid): """获取用户已订阅游戏 (GET) :uri: /users/<string:uid>/games :param maxs: 最后时间, 0代表当前时间, 无此参数按page来分页 :param page: 页码(数据可能有重复, 建议按照maxs分页) :param nbr: 每页数量 :returns: {'games': 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)) games = list() gids = list() while len(games) < pagesize: gids = UserSubGame.sub_game_ids(uid, page, pagesize, maxs) # 下线游戏也需要展示在用户订阅列表中 games.extend([ g.format(exclude_fields=['subscribed']) for g in Game.get_list(gids, check_online=False) ]) # 如果按照maxs分页, 不足pagesize个记录则继续查询 if maxs is not None: obj = UserSubGame.get_by_ship(uid, gids[-1]) if gids else None maxs = obj.create_at if obj else 1000 if len(gids) < pagesize: break else: break return {'games': games, 'end_page': len(gids) != pagesize, 'maxs': maxs}
def sub_game(): """订阅游戏 (GET|POST&LOGIN) :uri: /user/opt/subscribe-game :param game_id: 游戏id(批量订阅游戏id以逗号隔开) :returns: {} """ user = request.authed_user gids = request.values.get('game_id', None) if not gids: return error.GameNotExist gids = [gid.strip() for gid in gids.split(',')] games = Game.get_list(gids, check_online=False) if not games: return error.GameNotExist key = 'lock:subgame:%s' % (str(user._id)) with util.Lockit(Redis, key) as locked: if locked: return error.SubGameFailed sub_num = 0 for game in games: usg = UserSubGame.get_by_ship(str(user._id), str(game._id)) if not usg: usg = UserSubGame.init() usg.source = ObjectId(str(user._id)) usg.target = ObjectId(str(game._id)) usg.create_model() sub_num += 1 # 订阅游戏任务检查 if user: UserTask.check_user_tasks(str(user._id), SUB_GAME, sub_num) return {}
def search(): """搜索 (GET|POST) :uri: /search :param type: 搜索类型{'all':全部, 'user':用户, 'game':游戏, 'video':视频, 'activity_video':活动视频, 'live_number':直播间房号} :param keyword: 关键字 :returns: {'user':list, 'game':list, 'video':list} """ ua = request.headers.get('User-Agent') params = request.values stype = params.get('type', 'all') keyword = params.get('keyword', '') keyword = keyword.strip() platform = params.get('os') version_code = params.get('version_code', 0) if not stype or not keyword: return error.InvalidArguments users = games = videos = activity_videos = lives = list() if stype in ['user', 'all']: uids = User.search(keyword) users = [u.format() for u in User.get_list(uids)] users = sorted(users, key=lambda x: x['follower_count']) if stype in ['game', 'all']: ua_filter = None if ua and platform == 'android' and int(version_code) >= 64: ua_filter = ua gids = Game.search(keyword, ua_filter) games = [g.format() for g in Game.get_list(gids, check_online=False)] if stype in ['video', 'all']: vids = Video.search(keyword) videos = [v.format() for v in Video.get_list(vids)] if stype in ['activity_video']: activity_id = params.get('activity_id', None) uids = User.search(keyword) _ids = ActivityVideo.get_activity_video_by_authors(uids, activity_id) avids = ActivityVideo.search(keyword, activity_id) avids.extend(_ids) activity_videos = [ v.format() for v in ActivityVideo.get_list(set(avids)) ] if stype in ['live_number', 'all']: uids = User.search_live_number(keyword) livers = [u.format() for u in User.get_list(uids)] livers = sorted(livers, key=lambda x: x['follower_count']) lives_map = {} for live in Xlive.get_all_lives(): lives_map.update({live['user_id']: live}) lives = list() ex_fields = ['user__is_followed', 'game__subscribed'] for uid in uids: uid = str(uid) if uid in lives_map: ulive = Xlive.format(lives_map[uid], exclude_fields=ex_fields) lives.append({'live': ulive, 'user': None}) continue lives.append({'live': None, 'user': User.get_one(uid).format()}) return { 'users': users, 'games': games, 'videos': videos, 'activity_videos': activity_videos, 'lives': lives }
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 game_home(): """ 获取游戏页首页内容 uri: /games/home :param os: 平台 :param channels: 渠道(可选) :param version_code: 版本号 :return: {'grids': list} """ user = request.authed_user params = request.values os = params.get('os', None) channels = params.get('channels', None) version_code = int(params.get('version_code', 0)) if not os or not version_code: return error.InvalidArguments 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 def my_filter(data): result = list() for b in data: 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 not province: continue if b.province and province and province not in b.province: continue result.append(b.format()) return result # 游戏页宫格 _ids = GameGrid.all_ids() grids = my_filter(GameGrid.get_list(_ids)) # 游戏页广告 _ids = GameAds.all_ad_ids() banners = my_filter(GameAds.get_list(_ids)) # 游戏页主推游戏 _ids = GameMainstay.all_ids() mainstays = my_filter(GameMainstay.get_list(_ids)) # 游戏页热门游戏 _ids = HotRecommendGame.hot_game_ids() hot_games = [g.format() for g in Game.get_list(_ids)] # 游戏页模块内容 _ids = GameModule.all_ids_by_os(os) modules = [ i.format() for i in GameModule.get_list(_ids) if ModuleGame.module_game_ids(str(i._id)) ] return { 'grids': grids, 'ads': banners, 'mainstays': mainstays, 'hot_games': hot_games, 'modules': modules }