1 """Helper for Epic Games Store."""
9 def format_game_data(raw_game_data: dict[str, Any], language: str) -> dict[str, Any]:
10 """Format raw API game data for Home Assistant users."""
14 for image
in raw_game_data[
"keyImages"]:
15 if image[
"type"] ==
"OfferImageTall":
16 img_portrait = image[
"url"]
17 if image[
"type"] ==
"OfferImageWide":
18 img_landscape = image[
"url"]
20 current_promotions = raw_game_data[
"promotions"][
"promotionalOffers"]
21 upcoming_promotions = raw_game_data[
"promotions"][
"upcomingPromotionalOffers"]
26 and raw_game_data[
"price"][
"totalPrice"][
"discountPrice"] == 0
28 promotion_data = current_promotions[0][
"promotionalOffers"][0]
30 promotion_data = (current_promotions
or upcoming_promotions)[0][
35 "title": raw_game_data[
"title"].replace(
"\xa0",
" "),
36 "description": raw_game_data[
"description"].strip().replace(
"\xa0",
" "),
37 "released_at": dt_util.parse_datetime(raw_game_data[
"effectiveDate"]),
38 "original_price": raw_game_data[
"price"][
"totalPrice"][
"fmtPrice"][
40 ].replace(
"\xa0",
" "),
41 "publisher": raw_game_data[
"seller"][
"name"],
43 "img_portrait": img_portrait,
44 "img_landscape": img_landscape,
45 "discount_type": (
"free" if is_free_game(raw_game_data)
else "discount")
48 "discount_start_at": dt_util.parse_datetime(promotion_data[
"startDate"])
51 "discount_end_at": dt_util.parse_datetime(promotion_data[
"endDate"])
57 def get_game_url(raw_game_data: dict[str, Any], language: str) -> str:
58 """Format raw API game data for Home Assistant users."""
59 url_bundle_or_product =
"bundles" if raw_game_data[
"offerType"] ==
"BUNDLE" else "p"
60 url_slug: str |
None =
None
62 url_slug = raw_game_data[
"offerMappings"][0][
"pageSlug"]
64 with contextlib.suppress(Exception):
65 url_slug = raw_game_data[
"catalogNs"][
"mappings"][0][
"pageSlug"]
68 url_slug = raw_game_data[
"productSlug"]
70 return f
"https://store.epicgames.com/{language}/{url_bundle_or_product}/{url_slug}"
74 """Return if the game is free or will be free."""
77 game[
"promotions"][
"promotionalOffers"]
78 and game[
"promotions"][
"promotionalOffers"][0][
"promotionalOffers"][0][
80 ][
"discountPercentage"]
84 game[
"price"][
"totalPrice"][
"discountPrice"] == 0
87 game[
"promotions"][
"upcomingPromotionalOffers"]
88 and game[
"promotions"][
"upcomingPromotionalOffers"][0][
"promotionalOffers"][0][
90 ][
"discountPercentage"]
str get_game_url(dict[str, Any] raw_game_data, str language)
bool is_free_game(dict[str, Any] game)
dict[str, Any] format_game_data(dict[str, Any] raw_game_data, str language)