当前位置: 首页>>代码示例>>Python>>正文


Python ApiWrapper.game_by_summoner方法代码示例

本文整理汇总了Python中api_wrapper.ApiWrapper.game_by_summoner方法的典型用法代码示例。如果您正苦于以下问题:Python ApiWrapper.game_by_summoner方法的具体用法?Python ApiWrapper.game_by_summoner怎么用?Python ApiWrapper.game_by_summoner使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在api_wrapper.ApiWrapper的用法示例。


在下文中一共展示了ApiWrapper.game_by_summoner方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: UrfSummonerCrawler

# 需要导入模块: from api_wrapper import ApiWrapper [as 别名]
# 或者: from api_wrapper.ApiWrapper import game_by_summoner [as 别名]
class UrfSummonerCrawler(object):

    def __init__(self):
        self.api = ApiWrapper()
        self.processed_games = set()
        self.conn = sqlite3.connect(FILES.SQLLITE_FILE)
        self._setup_db()

        self._set_persistance()

    def _setup_db(self):
        self.conn.cursor().execute('''CREATE TABLE IF NOT EXISTS test_data(
                                        summ_id integer,
                                        champ_id integer,
                                        result integer)
                                   ''')

    def _set_persistance(self):
        """ Sets summoner_list, processed_games_list and processed_summoners_list
        """
        for dat, fname, default in DATS_FILES_AND_DEFAULTS:
            if os.path.exists(fname):
                with open(fname, 'rb') as f:
                    setattr(self, dat, pickle.load(f))
            else:
                setattr(self, dat, default)

    def _save_persistance(self):
        for dat, fname, _ in DATS_FILES_AND_DEFAULTS:
            with open(fname, 'wb') as f:
                pickle.dump(getattr(self, dat), f)

    def _insert_row(self, summoner, summ_champion, win):
        self.cur.execute(''' INSERT INTO test_data
                        (summ_id, champ_id, result)
                        VALUES (?, ?, ?)
                    ''', (summoner, summ_champion, win))

    def _extract_summ(self, game):
        summ_team = game['teamId']
        win = game['stats']['win']
        summ_champion = game['championId']
        return (summ_team, win, summ_champion)

    def _extract_fellow(self, fellow):
        fsumm = fellow['summonerId']
        fchamp = fellow['championId']
        fteam = fellow['teamId']
        return (fsumm, fchamp, fteam)

    def crawl(self):
        self.cur = self.conn.cursor()
        print('''
                 Beginning crawl...
                 From previous session (if one existed):
                 Processed games: {}
                 Prcesssed Summoners: {}
                 Summoners left to process: {} (This will grow as execution continues)

              '''.format(len(self.processed_games),
                         len(self.processed_summs),
                         len(self.summoners)))

        while self.summoners and len(self.processed_games) <= MAX_GAMES_TO_PROCESS:
            summoner = self.summoners.popleft()
            if summoner in self.processed_summs:
                # skip if we've looked at summ's recent games already
                continue

            url = self.api.game_by_summoner(summoner)

            recent_games = self.api.api_call(url)['games']
            for game in recent_games:
                game_id = game['gameId']
                # only parse URF games we haven't seen before
                if game['subType'] == 'URF' and game_id not in self.processed_games:
                    summ_team, win, summ_champion = self._extract_summ(game)
                    self._insert_row(summoner, summ_champion, win)

                    # Go through each fellow player
                    # and calculate if they won or lost based on if they
                    # are on the current summoners team.
                    for fellow in game['fellowPlayers']:
                        fsumm, fchamp, fteam = self._extract_fellow(fellow)
                        # Winners are fellows on the summoners team
                        if fteam == summ_team:
                            fwin = win
                        else:
                            fwin = not win

                        self._insert_row(fsumm, fchamp, fwin)
                        # Add the summoner to the list of summoners to process later
                        # because they've probably played more urf games
                        if summoner not in self.processed_summs:
                            self.summoners.append(fsumm)

                    self.processed_games.add(game_id)

                    if len(self.processed_games) % 100 == 0:
                        # Every 100 games, write info to db
#.........这里部分代码省略.........
开发者ID:Charleezy,项目名称:WinrateForFamiliarChamps,代码行数:103,代码来源:urf_by_summoner.py


注:本文中的api_wrapper.ApiWrapper.game_by_summoner方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。