Files
2025-04-16 16:07:14 +02:00

87 lines
2.2 KiB
Python

import json
import sys
import requests
import re
import random
import aiohttp
import asyncio
import os
class Champion:
Name: str
Role: [str]
def __init__(self, name: str, roles: [str]):
self.Name = name
self.Role = roles
Lanes = ["Top", "Mid", "Bot", "Support", "Jgl"]
async def fetch_champion(session, url_entry):
try:
async with session.get(url_entry, timeout=5) as resp:
data = await resp.json()
if data.get("name") == "None":
return None
return Champion(data["name"], data["roles"])
except Exception as e:
print(f'Could not get {url_entry}: {e}')
return None
async def get_champions_async(base_url: str, sub_urls: [str]) -> [Champion]:
champions = []
async with aiohttp.ClientSession() as session:
tasks = []
for entry in sub_urls:
full_url = f'{base_url}/{entry}'
tasks.append(fetch_champion(session, full_url))
results = await asyncio.gather(*tasks)
for champ in results:
if champ is not None:
champions.append(champ)
return champions
def get_player_list() -> [Champion]:
url = "https://raw.communitydragon.org/latest/plugins/rcp-be-lol-game-data/global/default/v1/champions"
response = requests.get(url)
data = response.text
data = data[data.index('id="list"')::]
data = data[data.index("<tr><td") + 1::]
data = data[:data.index('</main>')]
data = data[:data.rindex('</td></tr>')]
datalist = data.split("\r\n")
champions: [Champion] = []
sub_urls = []
regex_pattern = re.compile(r'-?\d+\.json', re.IGNORECASE)
for line in datalist:
match = regex_pattern.search(line)
if match:
sub_urls.append(match.group(0))
champions = asyncio.run(get_champions_async(url, sub_urls))
return champions
if __name__ == "__main__":
print("Generator started, gathering Champions!")
print("This can take some time!")
champions: [Champion] = get_player_list()
champ: Champion = random.choice(champions)
print(f'You will play:\n\tChampion: {champ.Name}\n\tRole: {random.choice(champ.Role)}\n\tLane: {random.choice(Lanes)}')
if(os.name == "nt"):
os.system("pause")