diff --git a/CGGItemSets.py b/CGGItemSets.py index be88cff..99f7877 100644 --- a/CGGItemSets.py +++ b/CGGItemSets.py @@ -1,5 +1,6 @@ import json import logging +import random import re from pathlib import Path from time import sleep @@ -20,12 +21,12 @@ ITEMS_TYPE = { CONSUMABLES = [2003, 2004, 2055, 2031, 2032, 2033, 2138, 2140, 2139] TRINKETS = [3340, 3364, 3363] -def getChampData(champ, position): - page = requests.get('https://champion.gg/champion/%s/%s?league=' % (champ, position), headers=HEADERS).text +def getChampData(champ, position, session): + page = session.get('https://champion.gg/champion/%s/%s?league=' % (champ, position)).text return json.loads(PATTERN_CHAMPIONGG.search(page).group(1)) -def getChampsWithPositionsAndPatch(): - soup = BeautifulSoup(requests.get('https://champion.gg', headers=HEADERS).text, 'html.parser') +def getChampsWithPositionsAndPatch(session): + soup = BeautifulSoup(session.get('https://champion.gg').text, 'html.parser') patch = soup.find('div', class_='analysis-holder').find('strong').text @@ -49,9 +50,9 @@ def makeItemSetFromList(list, label, key, data): "type": label % '.'.join([data['skills']['skillInfo'][int(k)-1]['key'] for k in data['skills'][key]['order']]) } -def writeItemSet(id, pos, ver, dir): +def writeItemSet(id, pos, ver, dir, session): logging.info('Retrieving data for %s at %s' % (id, pos)) - data = getChampData(id, pos) + data = getChampData(id, pos, session) item_set = { "title": "CGG %s %s - %.2f%% Winrate" % (pos, ver, data['stats']['winRate'] * 100), @@ -79,14 +80,17 @@ def main(): logging.info('CGG Item Sets') - ver = requests.get('https://ddragon.leagueoflegends.com/realms/euw.json', headers=HEADERS).json()['v'] - data = requests.get('http://ddragon.leagueoflegends.com/cdn/%s/data/en_US/champion.json' % ver, headers=HEADERS).json()['data'] + session = requests.Session() + session.headers.update(HEADERS) + + ver = session.get('https://ddragon.leagueoflegends.com/realms/euw.json').json()['v'] + data = session.get('http://ddragon.leagueoflegends.com/cdn/%s/data/en_US/champion.json' % ver).json()['data'] lol_champs = [data[i] for i in data] logging.info('LoL version: %s' % ver) logging.info('LoL numbers of champs: %d' % len(lol_champs)) - (champs, patch) = getChampsWithPositionsAndPatch() + (champs, patch) = getChampsWithPositionsAndPatch(session) logging.info('CGG version: %s' % patch) logging.info('CGG numbers of champs: %d' % len(champs)) @@ -98,8 +102,8 @@ def main(): path = Path(LOL_CHAMPS_DIR, c['id'], 'Recommended') path.mkdir(parents=True, exist_ok=True) for pos in positions: - writeItemSet(c['id'], pos, patch, path) - sleep(.3) + writeItemSet(c['id'], pos, patch, path, session) + sleep(random.uniform(.3, .4)) else: logging.error('%s not found in LoL champs' % name)