import time
import traceback

from pickle import dump, load
from selenium import webdriver
from selenium.webdriver.common.by import By

site_list = [
    'https://las.op.gg/champions/aatrox/top',
    'https://las.op.gg/champions/ahri/mid',
    'https://las.op.gg/champions/akali/top',
    'https://las.op.gg/champions/akshan/mid',
    'https://las.op.gg/champions/akshan/top',
    'https://las.op.gg/champions/alistar/support',
    'https://las.op.gg/champions/amumu/support',
    'https://las.op.gg/champions/anivia/mid',
    'https://las.op.gg/champions/annie/mid',
    'https://las.op.gg/champions/aphelios/adc',
    'https://las.op.gg/champions/ashe/adc',
    'https://las.op.gg/champions/aurelionsol/mid',
    'https://las.op.gg/champions/azir/mid',
    'https://las.op.gg/champions/bard/support',
    'https://las.op.gg/champions/blitzcrank/support',
    'https://las.op.gg/champions/brand/support',
    'https://las.op.gg/champions/braum/support',
    'https://las.op.gg/champions/caitlyn/adc',
    'https://las.op.gg/champions/camille/top',
    'https://las.op.gg/champions/cassiopeia/mid',
    'https://las.op.gg/champions/cassiopeia/top',
    'https://las.op.gg/champions/chogath/top',
    'https://las.op.gg/champions/corki/mid',
    'https://las.op.gg/champions/darius/top',
    'https://las.op.gg/champions/diana/jungle',
    'https://las.op.gg/champions/draven/adc',
    'https://las.op.gg/champions/drmundo/top',
    'https://las.op.gg/champions/ekko/jungle',
    'https://las.op.gg/champions/elise/jungle',
    'https://las.op.gg/champions/evelynn/jungle',
    'https://las.op.gg/champions/ezreal/adc',
    'https://las.op.gg/champions/fiddlesticks/jungle',
    'https://las.op.gg/champions/fiora/top',
    'https://las.op.gg/champions/fizz/mid',
    'https://las.op.gg/champions/galio/mid',
    'https://las.op.gg/champions/gangplank/top',
    'https://las.op.gg/champions/garen/top',
    'https://las.op.gg/champions/gnar/top',
    'https://las.op.gg/champions/gragas/top',
    'https://las.op.gg/champions/graves/jungle',
    'https://las.op.gg/champions/graves/top',
    'https://las.op.gg/champions/gwen/top',
    'https://las.op.gg/champions/hecarim/jungle',
    'https://las.op.gg/champions/heimerdinger/top',
    'https://las.op.gg/champions/illaoi/top',
    'https://las.op.gg/champions/irelia/top',
    'https://las.op.gg/champions/ivern/jungle',
    'https://las.op.gg/champions/janna/support',
    'https://las.op.gg/champions/jarvaniv/jungle',
    'https://las.op.gg/champions/jax/top',
    'https://las.op.gg/champions/jayce/top',
    'https://las.op.gg/champions/jhin/adc',
    'https://las.op.gg/champions/jinx/adc',
    'https://las.op.gg/champions/kaisa/adc',
    'https://las.op.gg/champions/kalista/adc',
    'https://las.op.gg/champions/karma/support',
    'https://las.op.gg/champions/karthus/jungle',
    'https://las.op.gg/champions/kassadin/mid',
    'https://las.op.gg/champions/katarina/mid',
    'https://las.op.gg/champions/kayle/top',
    'https://las.op.gg/champions/kayn/jungle',
    'https://las.op.gg/champions/kennen/top',
    'https://las.op.gg/champions/khazix/jungle',
    'https://las.op.gg/champions/kindred/jungle',
    'https://las.op.gg/champions/kled/top',
    'https://las.op.gg/champions/kogmaw/adc',
    'https://las.op.gg/champions/leblanc/mid',
    'https://las.op.gg/champions/leesin/jungle',
    'https://las.op.gg/champions/leona/support',
    'https://las.op.gg/champions/lillia/jungle',
    'https://las.op.gg/champions/lillia/top',
    'https://las.op.gg/champions/lissandra/mid',
    'https://las.op.gg/champions/lucian/adc',
    'https://las.op.gg/champions/lulu/support',
    'https://las.op.gg/champions/lux/support',
    'https://las.op.gg/champions/malphite/top',
    'https://las.op.gg/champions/malzahar/mid',
    'https://las.op.gg/champions/maokai/support',
    'https://las.op.gg/champions/maokai/top',
    'https://las.op.gg/champions/masteryi/jungle',
    'https://las.op.gg/champions/missfortune/adc',
    'https://las.op.gg/champions/monkeyking/jungle',
    'https://las.op.gg/champions/monkeyking/top',
    'https://las.op.gg/champions/mordekaiser/top',
    'https://las.op.gg/champions/morgana/support',
    'https://las.op.gg/champions/nami/support',
    'https://las.op.gg/champions/nasus/top',
    'https://las.op.gg/champions/nautilus/support',
    'https://las.op.gg/champions/neeko/mid',
    'https://las.op.gg/champions/nidalee/jungle',
    'https://las.op.gg/champions/nocturne/jungle',
    'https://las.op.gg/champions/nunu/jungle',
    'https://las.op.gg/champions/olaf/jungle',
    'https://las.op.gg/champions/orianna/mid',
    'https://las.op.gg/champions/ornn/top',
    'https://las.op.gg/champions/pantheon/support',
    'https://las.op.gg/champions/pantheon/top',
    'https://las.op.gg/champions/poppy/jungle',
    'https://las.op.gg/champions/poppy/top',
    'https://las.op.gg/champions/pyke/support',
    'https://las.op.gg/champions/qiyana/mid',
    'https://las.op.gg/champions/quinn/top',
    'https://las.op.gg/champions/rakan/support',
    'https://las.op.gg/champions/rammus/jungle',
    'https://las.op.gg/champions/reksai/jungle',
    'https://las.op.gg/champions/rell/support',
    'https://las.op.gg/champions/renata/support',
    'https://las.op.gg/champions/renekton/top',
    'https://las.op.gg/champions/rengar/top',
    'https://las.op.gg/champions/riven/top',
    'https://las.op.gg/champions/rumble/top',
    'https://las.op.gg/champions/ryze/mid',
    'https://las.op.gg/champions/ryze/top',
    'https://las.op.gg/champions/samira/adc',
    'https://las.op.gg/champions/sejuani/jungle',
    'https://las.op.gg/champions/sejuani/top',
    'https://las.op.gg/champions/senna/support',
    'https://las.op.gg/champions/seraphine/support',
    'https://las.op.gg/champions/sett/top',
    'https://las.op.gg/champions/shaco/jungle',
    'https://las.op.gg/champions/shen/top',
    'https://las.op.gg/champions/shyvana/jungle',
    'https://las.op.gg/champions/singed/top',
    'https://las.op.gg/champions/sion/top',
    'https://las.op.gg/champions/sivir/adc',
    'https://las.op.gg/champions/sona/support',
    'https://las.op.gg/champions/soraka/support',
    'https://las.op.gg/champions/swain/support',
    'https://las.op.gg/champions/sylas/mid',
    'https://las.op.gg/champions/sylas/top',
    'https://las.op.gg/champions/syndra/mid',
    'https://las.op.gg/champions/tahmkench/top',
    'https://las.op.gg/champions/taliyah/jungle',
    'https://las.op.gg/champions/talon/jungle',
    'https://las.op.gg/champions/taric/support',
    'https://las.op.gg/champions/teemo/top',
    'https://las.op.gg/champions/thresh/support',
    'https://las.op.gg/champions/tristana/adc',
    'https://las.op.gg/champions/trundle/jungle',
    'https://las.op.gg/champions/trundle/top',
    'https://las.op.gg/champions/tryndamere/top',
    'https://las.op.gg/champions/twistedfate/mid',
    'https://las.op.gg/champions/twitch/adc',
    'https://las.op.gg/champions/udyr/jungle',
    'https://las.op.gg/champions/urgot/top',
    'https://las.op.gg/champions/varus/adc',
    'https://las.op.gg/champions/vayne/adc',
    'https://las.op.gg/champions/vayne/top',
    'https://las.op.gg/champions/veigar/mid',
    'https://las.op.gg/champions/velkoz/support',
    'https://las.op.gg/champions/vex/mid',
    'https://las.op.gg/champions/vi/jungle',
    'https://las.op.gg/champions/viego/jungle',
    'https://las.op.gg/champions/viktor/mid',
    'https://las.op.gg/champions/viktor/top',
    'https://las.op.gg/champions/vladimir/mid',
    'https://las.op.gg/champions/vladimir/top',
    'https://las.op.gg/champions/volibear/top',
    'https://las.op.gg/champions/warwick/jungle',
    'https://las.op.gg/champions/warwick/top',
    'https://las.op.gg/champions/xayah/adc',
    'https://las.op.gg/champions/xerath/support',
    'https://las.op.gg/champions/xinzhao/jungle',
    'https://las.op.gg/champions/xinzhao/top',
    'https://las.op.gg/champions/yasuo/mid',
    'https://las.op.gg/champions/yasuo/top',
    'https://las.op.gg/champions/yone/mid',
    'https://las.op.gg/champions/yone/top',
    'https://las.op.gg/champions/yorick/top',
    'https://las.op.gg/champions/yuumi/support',
    'https://las.op.gg/champions/zac/jungle',
    'https://las.op.gg/champions/zac/top',
    'https://las.op.gg/champions/zed/mid',
    'https://las.op.gg/champions/zeri/adc',
    'https://las.op.gg/champions/ziggs/adc',
    'https://las.op.gg/champions/zilean/support',
    'https://las.op.gg/champions/zoe/mid',
    'https://las.op.gg/champions/zyra/support',
]

try:
    with open("data.pickle", "rb+") as data_file:
        dict_matchups = load(data_file)
except FileNotFoundError:
    dict_matchups = {}
session = webdriver.Chrome()
for site in site_list:
    try:
        champion = site.split("/")[4]
        if "jungle" in site or champion in dict_matchups:
            continue
        dict_matchups[champion] = {}

        session.get(site + "/counters")
        matchups = session.find_elements(By.XPATH, '//a[@class="link_display"]')

        for matchup in matchups:
            session.execute_script("arguments[0].scrollIntoView();", matchup)
            matchup.click()
            time.sleep(3)
            name = matchup.find_element(By.XPATH, '//div[@class="champion champion--counter"]/div[@class="name"]')
            winrate = session.find_element(By.XPATH, "//td[@class='data data--opponent']")
            dict_matchups[champion][name.text] = float(winrate.text.strip('%'))
    except:
        traceback.print_exc()
    with open("data.pickle", "wb+") as data_file:
        dump(dict_matchups, data_file)
session.close()
with open("data.pickle", "wb+") as data_file:
    dump(dict_matchups, data_file)
