import requests
import bs4
import json
import os

page = requests.get('https://helldivers.fandom.com/wiki/Stratagem_Codes_(Helldivers_2)')
soup = bs4.BeautifulSoup(page.content, 'html.parser')

data = []

rows = [list(e.find_all('tr'))[2:] for e in soup.find_all('tbody')]
rows = [e for row in rows for e in row]
for i, row in enumerate(rows):
    print(f'{i}/{len(rows)}')
    row: bs4.Tag
    datas = list(row.find_all('td'))

    img = datas[0].find('img')
    if img is None:
        continue
    if 'data-src' in img.attrs:
        img = img.attrs['data-src']
    else:
        img = img.attrs['src']
    img = img[:img.find('/revision')]
    img_name = img[img.rfind('/')+1:]

    name = datas[1].find('a')
    if name is None:
        name = datas[1].find('span')
    name = name.text

    dirs = []
    for d in datas[2].find_all('img'):
        alt = d.attrs['alt']
        if alt == 'Arrow 1 D':
            dirs.append('DOWN')
        elif alt == 'Arrow 2 L':
            dirs.append('LEFT')
        elif alt == 'Arrow 3 R':
            dirs.append('RIGHT')
        else:
            dirs.append('UP')

    if not os.path.exists(img_name):
        req = requests.get(img)
        with open(img_name, 'wb') as f:
            f.write(req.content)

    data.append((name, img_name, dirs))

with open('data.gd', 'w') as f:
    f.write('extends Node\n\nconst data := [\n')
    for d in data:
        k = ', '.join(['Global.DIRS.' + k for k in d[2]])
        f.write(f'    [\'{d[0]}\', preload(\'res://game/gems/{d[1]}\'), [{k}]],\n')
    f.write(']\n')
