Home Assistant Unofficial Reference 2024.12.1
helper.py
Go to the documentation of this file.
1 """Helper for Epic Games Store."""
2 
3 import contextlib
4 from typing import Any
5 
6 from homeassistant.util import dt as dt_util
7 
8 
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."""
11  img_portrait = None
12  img_landscape = None
13 
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"]
19 
20  current_promotions = raw_game_data["promotions"]["promotionalOffers"]
21  upcoming_promotions = raw_game_data["promotions"]["upcomingPromotionalOffers"]
22 
23  promotion_data = {}
24  if (
25  current_promotions
26  and raw_game_data["price"]["totalPrice"]["discountPrice"] == 0
27  ):
28  promotion_data = current_promotions[0]["promotionalOffers"][0]
29  else:
30  promotion_data = (current_promotions or upcoming_promotions)[0][
31  "promotionalOffers"
32  ][0]
33 
34  return {
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"][
39  "originalPrice"
40  ].replace("\xa0", " "),
41  "publisher": raw_game_data["seller"]["name"],
42  "url": get_game_url(raw_game_data, language),
43  "img_portrait": img_portrait,
44  "img_landscape": img_landscape,
45  "discount_type": ("free" if is_free_game(raw_game_data) else "discount")
46  if promotion_data
47  else None,
48  "discount_start_at": dt_util.parse_datetime(promotion_data["startDate"])
49  if promotion_data
50  else None,
51  "discount_end_at": dt_util.parse_datetime(promotion_data["endDate"])
52  if promotion_data
53  else None,
54  }
55 
56 
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
61  try:
62  url_slug = raw_game_data["offerMappings"][0]["pageSlug"]
63  except Exception: # noqa: BLE001
64  with contextlib.suppress(Exception):
65  url_slug = raw_game_data["catalogNs"]["mappings"][0]["pageSlug"]
66 
67  if not url_slug:
68  url_slug = raw_game_data["productSlug"]
69 
70  return f"https://store.epicgames.com/{language}/{url_bundle_or_product}/{url_slug}"
71 
72 
73 def is_free_game(game: dict[str, Any]) -> bool:
74  """Return if the game is free or will be free."""
75  return (
76  # Current free game(s)
77  game["promotions"]["promotionalOffers"]
78  and game["promotions"]["promotionalOffers"][0]["promotionalOffers"][0][
79  "discountSetting"
80  ]["discountPercentage"]
81  == 0
82  and
83  # Checking current price, maybe not necessary
84  game["price"]["totalPrice"]["discountPrice"] == 0
85  ) or (
86  # Upcoming free game(s)
87  game["promotions"]["upcomingPromotionalOffers"]
88  and game["promotions"]["upcomingPromotionalOffers"][0]["promotionalOffers"][0][
89  "discountSetting"
90  ]["discountPercentage"]
91  == 0
92  )
str get_game_url(dict[str, Any] raw_game_data, str language)
Definition: helper.py:57
bool is_free_game(dict[str, Any] game)
Definition: helper.py:73
dict[str, Any] format_game_data(dict[str, Any] raw_game_data, str language)
Definition: helper.py:9