Skip to content

Commit

Permalink
Merge pull request #21 from flbraun/other-leagues-data-support
Browse files Browse the repository at this point in the history
Support for Challenge HC, Standard and HC (requires restart)
  • Loading branch information
flbraun authored Nov 2, 2023
2 parents cb106ef + ef3c453 commit 98f607b
Show file tree
Hide file tree
Showing 13 changed files with 287 additions and 136 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/data.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
league: [challenge]
league: [challenge, challengehc, standard, hardcore]
steps:
- name: Checkout
uses: actions/checkout@v4
Expand Down
39 changes: 23 additions & 16 deletions data/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
import sys
from pathlib import Path

from .types import LeagueType
from .utils import EnumAction


DEFAULT_DATA_FILES = [f'data-{lt.value}.json' for lt in LeagueType]

logging.basicConfig(level=logging.INFO)

Expand All @@ -12,25 +17,17 @@

gen = subparsers.add_parser('gen')
gen.add_argument(
'league',
choices=[
'challenge',
'challenge-hc',
'standard',
'hc',
],
'league_type',
type=LeagueType,
action=EnumAction,
)

pub = subparsers.add_parser('pub')
pub.add_argument(
'filename',
nargs='+',
default=[
'data-challenge.json',
# 'data-challenge-hc.json',
# 'data-standard.json',
# 'data-hc.json',
],
default=DEFAULT_DATA_FILES,
choices=DEFAULT_DATA_FILES,
)

args = parser.parse_args()
Expand All @@ -46,25 +43,35 @@
from dataclasses import asdict

from .beasts import get_beasts
from .leagues import get_leagues
from .tools import get_tools
from .wiki import get_items

league = get_leagues()[args.league_type]
print(f'Aggregating data for {league.title} ({league.slug})...')

data = []

for num, entry in enumerate(itertools.chain(get_items(), get_beasts(), get_tools())):
for num, entry in enumerate(
itertools.chain(
get_items(league),
get_beasts(league),
get_tools(league),
),
):
data.append({'id': num, **asdict(entry)})

now = datetime.datetime.now(tz=datetime.UTC)
doc = {
'meta': {
'league': args.league,
'league': args.league_type.value,
'timestamp': datetime.datetime.timestamp(now),
'timestamp_human': now.isoformat(),
},
'data': data,
}

with pathlib.Path(f'data-{args.league}.json').open('w') as file:
with pathlib.Path(f'data-{args.league_type.value}.json').open('w') as file:
json.dump(doc, file)


Expand Down
9 changes: 5 additions & 4 deletions data/beasts.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
from collections.abc import Generator

from .leagues import League
from .ninja import get_ninja_index, make_ninja_url
from .trade import make_trade_url
from .types import NinjaCategory
from .utils import Entry, make_wiki_url


def get_beasts() -> Generator[Entry, None, None]:
index = get_ninja_index()
def get_beasts(league: League) -> Generator[Entry, None, None]:
index = get_ninja_index(league)

for beast in index.raw[NinjaCategory.BEASTS]:
yield Entry(
display_text=beast,
wiki_url=make_wiki_url(beast),
ninja_url=make_ninja_url(beast, None, NinjaCategory.BEASTS),
trade_url=make_trade_url(beast),
ninja_url=make_ninja_url(league, beast, None, NinjaCategory.BEASTS),
trade_url=make_trade_url(league, beast),
)
71 changes: 71 additions & 0 deletions data/leagues.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import dataclasses
import functools
import http

from tabulate import tabulate

from .types import LeagueType
from .utils import LoggedRequestsSession, slugify


@dataclasses.dataclass(frozen=True)
class League:
type_: LeagueType
title: str # e.g. "Ancestor"
slug: str # e.g. "ancestor"
is_hardcore: bool


@functools.cache
def get_leagues() -> dict[LeagueType, League]:
session = LoggedRequestsSession()

res = session.get('https://poe.ninja/api/data/getindexstate')
assert res.status_code == http.HTTPStatus.OK

res_parsed = res.json()

leagues = {
LeagueType.STANDARD: League(
type_=LeagueType.STANDARD,
title='Standard',
slug='standard',
is_hardcore=False,
),
LeagueType.HARDCORE: League(
type_=LeagueType.HARDCORE,
title='Hardcore',
slug='hardcore',
is_hardcore=True,
),
}

# challenge league usually is the first
challenge_league_name = res_parsed['economyLeagues'][0]['name']
assert challenge_league_name != 'Standard', challenge_league_name
assert 'Hardcore' not in challenge_league_name, challenge_league_name
assert 'Ruthless' not in challenge_league_name, challenge_league_name
assert 'HC' not in challenge_league_name, challenge_league_name

leagues[LeagueType.CHALLENGE] = League(
type_=LeagueType.CHALLENGE,
title=challenge_league_name,
slug=slugify(challenge_league_name),
is_hardcore=False,
)
leagues[LeagueType.CHALLENGE_HARDCORE] = League(
type_=LeagueType.CHALLENGE_HARDCORE,
title=f'Hardcore {challenge_league_name}',
slug=f'{leagues[LeagueType.CHALLENGE].slug}hc',
is_hardcore=True,
)

print(
tabulate(
[[league_type, league.title, league.slug, league.is_hardcore] for league_type, league in leagues.items()],
headers=('league type', 'human', 'slug', 'is hardcore'),
tablefmt='outline',
),
)

return leagues
Loading

0 comments on commit 98f607b

Please sign in to comment.