Skip to content
This repository was archived by the owner on Sep 28, 2024. It is now read-only.

Commit

Permalink
Migrate to poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
fedecalendino committed Jul 6, 2022
1 parent 777c59a commit 37b82d9
Show file tree
Hide file tree
Showing 33 changed files with 596 additions and 279 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2020 Federico Calendino
Copyright (c) 2022 Fede Calendino

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
27 changes: 11 additions & 16 deletions nintendeals/api/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ def _parse_date(string: str) -> datetime:
return date_parser(string).replace(tzinfo=None)


def fetch_prices(
country: str,
nsuids: Iterable[str]
) -> Iterator[Tuple[str, Price]]:
def fetch_prices(country: str, nsuids: Iterable[str]) -> Iterator[Tuple[str, Price]]:
nsuids = set(nsuids)

if not 51 > len(nsuids) > 0:
Expand All @@ -26,12 +23,12 @@ def fetch_prices(
country=country,
lang="en",
ids=",".join(nsuids),
)
),
)

response.raise_for_status()

for data in response.json().get('prices', []):
for data in response.json().get("prices", []):
nsuid = str(data["title_id"])
regular_price = data.get("regular_price")

Expand All @@ -49,8 +46,8 @@ def fetch_prices(

if discount_price:
price.sale_value = float(discount_price["raw_value"])
price.sale_start = _parse_date(discount_price['start_datetime'])
price.sale_end = _parse_date(discount_price['end_datetime'])
price.sale_start = _parse_date(discount_price["start_datetime"])
price.sale_end = _parse_date(discount_price["end_datetime"])

yield price.nsuid, price

Expand Down Expand Up @@ -80,10 +77,9 @@ def get_prices(games: Iterable["Game"], country: str) -> Iterator[Tuple[str, Pri

if len(chunk) == 50:
fetched = {
nsuid: price for nsuid, price in
fetch_prices(
country=country,
nsuids=[game.nsuid for game in chunk]
nsuid: price
for nsuid, price in fetch_prices(
country=country, nsuids=[game.nsuid for game in chunk]
)
}
prices.update(fetched)
Expand All @@ -92,10 +88,9 @@ def get_prices(games: Iterable["Game"], country: str) -> Iterator[Tuple[str, Pri

if chunk:
fetched = {
nsuid: price for nsuid, price in
fetch_prices(
country=country,
nsuids=[game.nsuid for game in chunk]
nsuid: price
for nsuid, price in fetch_prices(
country=country, nsuids=[game.nsuid for game in chunk]
)
}
prices.update(fetched)
Expand Down
111 changes: 20 additions & 91 deletions nintendeals/commons/classes/eshops.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,15 @@ def __init__(self, game: "Game"):

@property
def ca_en(self) -> str:
return NAeShop.FORMAT.format(
lang="en",
country="CA",
slug=self.game.slug
)
return NAeShop.FORMAT.format(lang="en", country="CA", slug=self.game.slug)

@property
def ca_fr(self) -> str:
return NAeShop.FORMAT.format(
lang="fr",
country="CA",
slug=self.game.slug
)
return NAeShop.FORMAT.format(lang="fr", country="CA", slug=self.game.slug)

@property
def us_en(self) -> str:
return NAeShop.FORMAT.format(
lang="en",
country="US",
slug=self.game.slug
)
return NAeShop.FORMAT.format(lang="en", country="US", slug=self.game.slug)


class EUeShop:
Expand All @@ -45,136 +33,77 @@ def __init__(self, game: "Game"):

@property
def at_de(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="at",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="at", slug=self.game.slug)

@property
def be_fr(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="be",
lang="fr",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="be", lang="fr", slug=self.game.slug)

@property
def be_nl(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="be",
lang="nl",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="be", lang="nl", slug=self.game.slug)

@property
def ch_de(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="ch",
lang="de",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="ch", lang="de", slug=self.game.slug)

@property
def ch_fr(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="ch",
lang="fr",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="ch", lang="fr", slug=self.game.slug)

@property
def ch_it(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="ch",
lang="it",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="ch", lang="it", slug=self.game.slug)

@property
def de_de(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="de",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="de", slug=self.game.slug)

@property
def es_es(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="es",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="es", slug=self.game.slug)

@property
def fr_fr(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="fr",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="fr", slug=self.game.slug)

@property
def it_it(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="it",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="it", slug=self.game.slug)

@property
def nl_nl(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="nl",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="nl", slug=self.game.slug)

@property
def pt_pt(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="pt",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="pt", slug=self.game.slug)

@property
def ru_ru(self) -> str:
return EUeShop.FORMAT_LANG.format(
domain="ru",
lang="-",
slug=self.game.slug
)
return EUeShop.FORMAT_LANG.format(domain="ru", lang="-", slug=self.game.slug)

@property
def uk_en(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="co.uk",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="co.uk", slug=self.game.slug)

@property
def za_en(self) -> str:
return EUeShop.FORMAT_NO_LANG.format(
domain="co.za",
slug=self.game.slug
)
return EUeShop.FORMAT_NO_LANG.format(domain="co.za", slug=self.game.slug)

@property
def au_en(self) -> str:
if self.game.platform != Platforms.NINTENDO_SWITCH:
raise ValueError("Only available for Nintendo Switch games")

return EUeShop.FORMAT_ALT.format(
country="AU",
lang="en",
nsuid=self.game.nsuid
)
return EUeShop.FORMAT_ALT.format(country="AU", lang="en", nsuid=self.game.nsuid)

@property
def nz_en(self) -> str:
if self.game.platform != Platforms.NINTENDO_SWITCH:
raise ValueError("Only available for Nintendo Switch games")

return EUeShop.FORMAT_ALT.format(
country="NZ",
lang="en",
nsuid=self.game.nsuid
)
return EUeShop.FORMAT_ALT.format(country="NZ", lang="en", nsuid=self.game.nsuid)


class JPeShop:
Expand Down
1 change: 0 additions & 1 deletion nintendeals/commons/classes/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@


class Game:

def __init__(
self,
platform: Platforms,
Expand Down
9 changes: 1 addition & 8 deletions nintendeals/commons/classes/prices.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@


class Price:

def __init__(
self,
nsuid: str,
country: str,
currency: str,
value: float
):
def __init__(self, nsuid: str, country: str, currency: str, value: float):
self.nsuid: str = nsuid

self.country: str = country
Expand Down
12 changes: 4 additions & 8 deletions nintendeals/noa/api/algolia.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@ def _search_index(query, **options):
INDEX = client.init_index(INDEX_NAME)

response = INDEX.search(query, request_options=options)
return response.get('hits', [])
return response.get("hits", [])


def search_by_nsuid(nsuid: str) -> Optional[dict]:
hits = _search_index(nsuid, restrictSearchableAttributes=['nsuid'])
hits = _search_index(nsuid, restrictSearchableAttributes=["nsuid"])
return (hits or [{}])[0]


Expand All @@ -49,9 +49,7 @@ def search_by_platform(platform: Platforms) -> Iterator[dict]:
"allowTyposOnNumericTokens": False,
"queryType": "prefixAll",
"restrictSearchableAttributes": ["nsuid"],
"facetFilters": [
f"platform:{PLATFORMS[platform]}"
],
"facetFilters": [f"platform:{PLATFORMS[platform]}"],
"hitsPerPage": 500,
}

Expand Down Expand Up @@ -79,9 +77,7 @@ def search_by_query(query: str, platform: Platforms = None) -> Iterator[dict]:
}

if platform:
options["facetFilters"] = [
f"platform:{PLATFORMS[platform]}"
]
options["facetFilters"] = [f"platform:{PLATFORMS[platform]}"]

page = -1

Expand Down
11 changes: 6 additions & 5 deletions nintendeals/noa/scrapers/nintendo.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@


def scrap(slug):
response = requests.get(
url=ESHOP_URL.format(slug=slug),
allow_redirects=True
)
response = requests.get(url=ESHOP_URL.format(slug=slug), allow_redirects=True)

if response.status_code != 200:
return {}

soup = BeautifulSoup(response.text, features="html.parser")
div = soup.find("div", class_="game-details-container")
script = [script for script in list(div.find_all("script")) if "window.game = Object.freeze" in str(script)][0]
script = [
script
for script in list(div.find_all("script"))
if "window.game = Object.freeze" in str(script)
][0]

data = {}
separator = ': "'
Expand Down
6 changes: 4 additions & 2 deletions nintendeals/noa/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def build_game(data: Dict) -> Game:
# Release Date
try:
release_date = data["releaseDateDisplay"].split("T")[0]
game.release_date = datetime.strptime(release_date, '%Y-%m-%d')
game.release_date = datetime.strptime(release_date, "%Y-%m-%d")
except (KeyError, ValueError):
game.release_date = None

Expand Down Expand Up @@ -64,7 +64,9 @@ def build_game(data: Dict) -> Game:

if game.platform == Platforms.NINTENDO_SWITCH:
game.features[Features.DLC] = "DLC available" in filters
game.features[Features.NSO_REQUIRED] = "Nintendo Switch Online compatible" in filters
game.features[Features.NSO_REQUIRED] = (
"Nintendo Switch Online compatible" in filters
)
game.features[Features.SAVE_DATA_CLOUD] = extra.get("save_data_cloud")

return game
10 changes: 4 additions & 6 deletions nintendeals/noe/api/nintendo.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@


def _search(
query: str = "*",
nsuid: str = None,
platform: Platforms = None
query: str = "*", nsuid: str = None, platform: Platforms = None
) -> Iterator[dict]:
rows = 200

Expand All @@ -46,7 +44,7 @@ def _search(
if response.status_code != 200:
break

json = response.json()['response'].get('docs', [])
json = response.json()["response"].get("docs", [])

if not len(json):
break
Expand All @@ -55,8 +53,8 @@ def _search(
nsuids = data.get("nsuid_txt", [])
product_codes = data["product_code_txt"] = [
pc.replace("-", "")
for pc in data.get("product_code_txt", [])
if pc[:3] in PRODUCT_CODE_PREFIXES
for pc in data.get("product_code_txt", [])
if pc[:3] in PRODUCT_CODE_PREFIXES
]

if not any((nsuids, product_codes)):
Expand Down
Loading

0 comments on commit 37b82d9

Please sign in to comment.