CGGItemSets/CGGItemSets.py

112 lines
4.1 KiB
Python
Raw Permalink Normal View History

import json
import logging
2018-06-03 13:53:41 +02:00
import random
import re
from pathlib import Path
from time import sleep
import requests
from bs4 import BeautifulSoup
2018-06-03 09:26:59 +02:00
LOL_CHAMPS_DIR = 'C:\\League of Legends\\Config\\Champions\\'
2018-06-02 18:22:57 +02:00
PATTERN_CHAMPIONGG = re.compile(r'^\s+matchupData\.championData = (.*)$', re.MULTILINE)
HEADERS = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:60.0) Gecko/20100101 Firefox/60.0'}
2018-06-03 09:26:59 +02:00
ITEMS_TYPE = {
'Most Frequent Starters': ['firstItems', 'mostGames'],
'Highest Win % Starters': ['firstItems', 'highestWinPercent'],
'Most Frequent Core Build': ['items', 'mostGames'],
'Highest Win % Core Build': ['items', 'highestWinPercent'],
}
2018-06-03 13:38:05 +02:00
CONSUMABLES = [2003, 2004, 2055, 2031, 2032, 2033, 2138, 2140, 2139]
TRINKETS = [3340, 3364, 3363]
2018-06-03 13:53:41 +02:00
def getChampData(champ, position, session):
page = session.get('https://champion.gg/champion/%s/%s?league=' % (champ, position)).text
2018-06-02 18:22:57 +02:00
return json.loads(PATTERN_CHAMPIONGG.search(page).group(1))
2018-06-03 13:53:41 +02:00
def getChampsWithPositionsAndPatch(session):
soup = BeautifulSoup(session.get('https://champion.gg').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:]]
2018-06-03 09:26:59 +02:00
champs.append({champ_name: pos})
return (champs, patch)
def makeItemSet(data, label):
return {
2018-06-03 09:26:59 +02:00
"items": [{"id": item['id'], "count": 1} for item in data['items']],
"type": "%s (%.2f%% - %d games)" % (label, data['winPercent'] * 100, data['games'])
}
def makeItemSetFromList(list, label, key, data):
return {
2018-06-03 09:26:59 +02:00
"items": [{"id": str(id), "count": 1} for id in list],
"type": label % '.'.join([data['skills']['skillInfo'][int(k)-1]['key'] for k in data['skills'][key]['order']])
}
2018-06-03 13:53:41 +02:00
def writeItemSet(id, pos, ver, dir, session):
2018-06-04 13:31:01 +02:00
logging.info('Retrieving data for %s at %s', id, pos)
2018-06-03 13:53:41 +02:00
data = getChampData(id, pos, session)
item_set = {
"title": "CGG %s %s - %.2f%%" % (pos, ver, data['stats']['winRate'] * 100),
"type": "custom",
"map": "any",
"mode": "any",
"priority": False,
"sortrank": 0,
"blocks": []
}
2018-06-03 09:26:59 +02:00
for label, path in ITEMS_TYPE.items():
if path[1] in data[path[0]]:
item_set['blocks'].append(makeItemSet(data[path[0]][path[1]], label))
2018-06-03 13:38:05 +02:00
item_set['blocks'].append(makeItemSetFromList(CONSUMABLES, "Consumables | Frequent: %s", 'mostGames', data))
item_set['blocks'].append(makeItemSetFromList(TRINKETS, "Trinkets | Wins: %s", 'highestWinPercent', data))
2018-06-04 13:31:01 +02:00
logging.info('Writing item set for %s at %s', id, pos)
2018-06-03 13:38:05 +02:00
with open(dir.joinpath('CGG_%s_%s.json' % (id, pos)), 'w', newline='\n') as out:
json.dump(item_set, out, indent=4)
def main():
logging.basicConfig(format='%(asctime)s:%(levelname)s:%(message)s', level=logging.INFO)
logging.info('CGG Item Sets')
2018-06-03 13:53:41 +02:00
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]
2018-06-04 13:31:01 +02:00
logging.info('LoL version: %s', ver)
logging.info('LoL numbers of champs: %d', len(lol_champs))
2018-06-03 13:53:41 +02:00
(champs, patch) = getChampsWithPositionsAndPatch(session)
2018-06-04 13:31:01 +02:00
logging.info('CGG version: %s', patch)
logging.info('CGG numbers of champs: %d', len(champs))
for champ in champs:
for name, positions in champ.items():
c = next((x for x in lol_champs if x['name'] == name), None)
if c is not None:
2018-06-03 13:38:05 +02:00
path = Path(LOL_CHAMPS_DIR, c['id'], 'Recommended')
path.mkdir(parents=True, exist_ok=True)
for pos in positions:
2018-06-03 13:53:41 +02:00
writeItemSet(c['id'], pos, patch, path, session)
sleep(random.uniform(.3, .4))
else:
2018-06-04 13:31:01 +02:00
logging.error('%s not found in LoL champs', name)
if __name__ == "__main__":
main()