From a9a7a3d1374f057c0baec7f74b16e0854b1ba819 Mon Sep 17 00:00:00 2001 From: solesensei Date: Sat, 29 Oct 2022 23:25:08 +0300 Subject: [PATCH] 6 crashes while fetching game data (#10) * Add checks before fetching game cover from `img_logo_url` * Add checks on NULL for `playtime_forever` option * up version * Add check for conflicting arguments --- README.md | 2 +- main.py | 2 ++ ngl/client.py | 11 +++++++---- ngl/games/steam.py | 32 ++++++++++++++++++++------------ 4 files changed, 30 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 929f1d3..4aee248 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Notion Game List ![](https://img.shields.io/badge/version-0.0.7-blue) ![](https://app.travis-ci.com/solesensei/notion-game-list.svg?branch=master) [![discuss](https://img.shields.io/reddit/subreddit-subscribers/notion?label=Discuss%20r%2Fnotion-games-list&style=social)](https://www.reddit.com/r/Notion/comments/jiy1sb/notion_games_list/?utm_source=share&utm_medium=web2x&context=3) +# Notion Game List ![](https://img.shields.io/badge/version-0.0.8-blue) ![](https://app.travis-ci.com/solesensei/notion-game-list.svg?branch=master) [![discuss](https://img.shields.io/reddit/subreddit-subscribers/notion?label=Discuss%20r%2Fnotion-games-list&style=social)](https://www.reddit.com/r/Notion/comments/jiy1sb/notion_games_list/?utm_source=share&utm_medium=web2x&context=3) All your games inside [Notion](https://www.notion.so/solesensei/Notion-Game-List-generated-0d0d39993755415bb8812563a2781d84). diff --git a/main.py b/main.py index 221c143..e5840a4 100644 --- a/main.py +++ b/main.py @@ -25,6 +25,8 @@ parser.add_argument("--steam-no-cache", help="Do not use cached fetched games", action="store_true") args = parser.parse_args() + assert not (args.skip_non_steam and args.use_only_library), "You can't use --skip-non-steam and --use-only-library together" + STEAM_USER = args.steam_user or STEAM_USER echo.y("Logging into Notion...") diff --git a/ngl/client.py b/ngl/client.py index 41e6f57..d8a97dc 100644 --- a/ngl/client.py +++ b/ngl/client.py @@ -104,13 +104,16 @@ def _parse_date(date_str: tp.Optional[str]): def add_game(self, game: GameInfo, game_page: CollectionViewPageBlock, use_bg_as_cover: bool = False) -> bool: row_data = {"title": game.name, "platforms": game.platforms, "release_date": self._parse_date(game.release_date), "notes": f"Playtime: {game.playtime}"} row = self._add_row(game_page.collection, **row_data) - row.icon = game.icon_uri + row.icon = game.icon_uri or self._gl_icon with self.client.as_atomic_transaction(): # Game cover image cover_img_uri = game.bg_uri or game.logo_uri if use_bg_as_cover else game.logo_uri - self.client.submit_transaction( - build_operation(row.id, path=["format", "page_cover"], command="set", args=cover_img_uri, table="block") - ) + if cover_img_uri: + self.client.submit_transaction( + build_operation(row.id, path=["format", "page_cover"], command="set", args=cover_img_uri, table="block") + ) + else: + echo.y(f"Game '{game.name}:{game.id}' does not have cover image") return True def import_game_list(self, game_list: tp.List[GameInfo], game_page: CollectionViewPageBlock, **kwargs) -> tp.List[GameInfo]: diff --git a/ngl/games/steam.py b/ngl/games/steam.py index 83f80ac..8014569 100644 --- a/ngl/games/steam.py +++ b/ngl/games/steam.py @@ -98,7 +98,7 @@ def _get_bg_image(self, game_id: TGameID) -> tp.Optional[str]: return None @staticmethod - def _playtime_format(playtime_in_minutes): + def _playtime_format(playtime_in_minutes: int) -> str: if playtime_in_minutes == 0: return "never" if playtime_in_minutes < 120: @@ -129,28 +129,36 @@ def _fetch_library_games(self, skip_non_steam: bool = False, skip_free_games: bo if game_id in self._games: continue echo.c(" " * 100 + f"\rFetching [{i}/{number_of_games}]: {g.name}", end="\r") - try: - steam_game = self.store.get_game_info(game_id) if not library_only else None - if steam_game is None and not library_only: - echo.m(f"Game {g.name} id:{game_id} not fetched from Steam store, skip it!") - self._store_skipped.append(game_id) - except SteamApiNotFoundError: - if skip_non_steam: + steam_game = None + if not library_only: + # Fetch game info from steam store + try: + steam_game = self.store.get_game_info(game_id) + except SteamApiNotFoundError: + pass + + if steam_game is None and skip_non_steam: echo.m(f"Game {g.name} id:{game_id} not found in Steam store, skip it") + self._store_skipped.append(game_id) continue + echo.r(f"Game {g.name} id:{game_id} not found in Steam store, fetching details from library") - steam_game = None - logo_uri = steam_game.header_image if steam_game is not None and steam_game.header_image else self._image_link(game_id, g.img_logo_url) + logo_uri = None + if steam_game is not None and steam_game.header_image: + logo_uri = steam_game.header_image + elif getattr(g, "img_logo_url", None): + logo_uri = self._image_link(game_id, g.img_logo_url) + game_info = GameInfo( id=game_id, name=g.name, platforms=[PLATFORM], release_date=steam_game.release_date.date if steam_game is not None else None, - playtime=self._playtime_format(g.playtime_forever), + playtime=self._playtime_format(g.playtime_forever) if getattr(g, "playtime_forever", None) is not None else None, logo_uri=logo_uri, bg_uri=self._get_bg_image(game_id), - icon_uri=self._image_link(game_id, g.img_icon_url), + icon_uri=self._image_link(game_id, g.img_icon_url) if getattr(g, "img_icon_url", None) is not None else None, free=steam_game.is_free if steam_game is not None else None, ) if steam_game is not None: