本文整理汇总了Python中api_wrapper.ApiWrapper.api_call方法的典型用法代码示例。如果您正苦于以下问题:Python ApiWrapper.api_call方法的具体用法?Python ApiWrapper.api_call怎么用?Python ApiWrapper.api_call使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类api_wrapper.ApiWrapper
的用法示例。
在下文中一共展示了ApiWrapper.api_call方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UrfSummonerCrawler
# 需要导入模块: from api_wrapper import ApiWrapper [as 别名]
# 或者: from api_wrapper.ApiWrapper import api_call [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
#.........这里部分代码省略.........