import requests import re import json from bs4 import BeautifulSoup from time import sleep from pathlib import Path PATTERN_CHAMPIONGG = re.compile(r'^\s+matchupData\.championData = (.*)$') LOL_CHAMPS_DIR = r'C:\\League of Legends\\Config\\Champions\\' def getChampData(champ, position): page = requests.get('http://champion.gg/champion/%s/%s' % (champ, position)).text for line in page.split("\n"): if "matchupData.championData" in line: return json.loads(PATTERN_CHAMPIONGG.match(line).group(1)) def getChampsWithPositionsAndPatch(): r = requests.get('http://champion.gg') soup = BeautifulSoup(r.text, 'html.parser') patch = soup.find('div', class_='analysis-holder').find('strong').text champs = [] for div in soup.find_all('div', class_='champ-height'): champ_name = div.find('span', class_='champion-name').text pos = [link['href'].split('/')[-1] for link in div.find_all('a', href=True)[1:]] champs.append({champ_name: pos }) return (champs, patch) def makeItemSet(data, label): items = [] for item in data['items']: items.append({ "id": item['id'], "count": 1 }) return { "items": items, "type": "%s (%.2f %% - %d games)" % (label, data['winPercent'] * 100, data['games']) } def writeItemSet(id, pos, ver, data, path): item_set = { "map": "any", "blocks": [], "title": "CGG %s %s" % (pos, ver), "priority": False, "mode": "any", "type": "custom", "sortrank": 1, "champion": "%s" % id } if 'mostGames' in data['firstItems']: item_set['blocks'].append(makeItemSet(data['firstItems']['mostGames'], 'Most Frequent Starters')) if 'highestWinPercent' in data['firstItems']: item_set['blocks'].append(makeItemSet(data['firstItems']['highestWinPercent'], 'Highest Win % Starters')) if 'mostGames' in data['items']: item_set['blocks'].append(makeItemSet(data['items']['mostGames'], 'Most Frequent Core Build')) if 'highestWinPercent' in data['items']: item_set['blocks'].append(makeItemSet(data['items']['highestWinPercent'], 'Highest Win % Core Build')) item_set['blocks'].append({ "items": [{"id": "3340", "count": 1}, {"id": "3364", "count": 1}, { "id": "3363", "count": 1} ], "type": "Trinkets"}) with open('%sCGG_%s_%s.json' % (path, id, pos), 'w', newline='\n') as out: json.dump(item_set, out, indent=4) def main(): ver = requests.get('https://ddragon.leagueoflegends.com/realms/euw.json').json()['v'] data = requests.get('http://ddragon.leagueoflegends.com/cdn/%s/data/en_US/champion.json' % ver).json()['data'] lol_champs = [data[i] for i in data] (champs, patch) = getChampsWithPositionsAndPatch() for champ in champs: for name, poss in champ.items(): for c in lol_champs: if c['name'] == name: path = '%s%s\\Recommended\\' % (LOL_CHAMPS_DIR, name) Path(path).mkdir(parents=True, exist_ok=True) print(name) for pos in poss: print(pos) writeItemSet(c['id'], pos, patch, getChampData(c['id'], pos), path) sleep(.3) print() if __name__ == "__main__": main()