🎨 Better Steam code
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
from abc import abstractmethod
|
from abc import abstractmethod
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
from time import time
|
from time import time
|
||||||
@@ -34,25 +35,25 @@ class SteamSourceIterator(SourceIterator):
|
|||||||
self.manifests = set()
|
self.manifests = set()
|
||||||
|
|
||||||
# Get dirs that contain steam app manifests
|
# Get dirs that contain steam app manifests
|
||||||
manifests_dirs = set()
|
|
||||||
libraryfolders_path = self.source.location / "steamapps" / "libraryfolders.vdf"
|
libraryfolders_path = self.source.location / "steamapps" / "libraryfolders.vdf"
|
||||||
with open(libraryfolders_path, "r") as file:
|
with open(libraryfolders_path, "r") as file:
|
||||||
for line in file.readlines():
|
contents = file.read()
|
||||||
line = line.strip()
|
steamapps_dirs = [
|
||||||
prefix = '"path"'
|
Path(path) / "steamapps"
|
||||||
if not line.startswith(prefix):
|
for path in re.findall('"path"\s+"(.*)"\n', contents, re.IGNORECASE)
|
||||||
continue
|
]
|
||||||
library_folder = Path(line[len(prefix) :].strip()[1:-1])
|
|
||||||
manifests_dir = library_folder / "steamapps"
|
|
||||||
if not (manifests_dir).is_dir():
|
|
||||||
continue
|
|
||||||
manifests_dirs.add(manifests_dir)
|
|
||||||
|
|
||||||
# Get app manifests
|
# Get app manifests
|
||||||
for manifests_dir in manifests_dirs:
|
for steamapps_dir in steamapps_dirs:
|
||||||
for child in manifests_dir.iterdir():
|
if not steamapps_dir.is_dir():
|
||||||
if child.is_file() and "appmanifest" in child.name:
|
continue
|
||||||
self.manifests.add(child)
|
self.manifests.update(
|
||||||
|
[
|
||||||
|
manifest
|
||||||
|
for manifest in steamapps_dir.glob("appmanifest_*.acf")
|
||||||
|
if manifest.is_file()
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
self.manifests_iterator = iter(self.manifests)
|
self.manifests_iterator = iter(self.manifests)
|
||||||
|
|
||||||
@@ -71,7 +72,7 @@ class SteamSourceIterator(SourceIterator):
|
|||||||
return None
|
return None
|
||||||
|
|
||||||
# Skip non installed games
|
# Skip non installed games
|
||||||
if not int(local_data["StateFlags"]) & self.installed_state_mask:
|
if not int(local_data["stateflags"]) & self.installed_state_mask:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# Build game from local data
|
# Build game from local data
|
||||||
@@ -101,9 +102,9 @@ class SteamSourceIterator(SourceIterator):
|
|||||||
# TODO move to its own manager
|
# TODO move to its own manager
|
||||||
try:
|
try:
|
||||||
online_data = steam.get_api_data(appid=appid)
|
online_data = steam.get_api_data(appid=appid)
|
||||||
except (HTTPError, JSONDecodeError, SteamGameNotFoundError):
|
except (HTTPError, JSONDecodeError):
|
||||||
pass
|
pass
|
||||||
except SteamNotAGameError:
|
except (SteamNotAGameError, SteamGameNotFoundError):
|
||||||
game.update_values({"blacklisted": True})
|
game.update_values({"blacklisted": True})
|
||||||
else:
|
else:
|
||||||
game.update_values(online_data)
|
game.update_values(online_data)
|
||||||
|
|||||||
@@ -23,12 +23,16 @@ class SteamInvalidManifestError(SteamError):
|
|||||||
|
|
||||||
|
|
||||||
class SteamManifestData(TypedDict):
|
class SteamManifestData(TypedDict):
|
||||||
|
"""Dict returned by SteamHelper.get_manifest_data"""
|
||||||
|
|
||||||
name: str
|
name: str
|
||||||
appid: str
|
appid: str
|
||||||
StateFlags: str
|
stateflags: str
|
||||||
|
|
||||||
|
|
||||||
class SteamAPIData(TypedDict):
|
class SteamAPIData(TypedDict):
|
||||||
|
"""Dict returned by SteamHelper.get_api_data"""
|
||||||
|
|
||||||
developers: str
|
developers: str
|
||||||
|
|
||||||
|
|
||||||
@@ -47,7 +51,7 @@ class SteamHelper:
|
|||||||
|
|
||||||
for key in SteamManifestData.__required_keys__:
|
for key in SteamManifestData.__required_keys__:
|
||||||
regex = f'"{key}"\s+"(.*)"\n'
|
regex = f'"{key}"\s+"(.*)"\n'
|
||||||
if (match := re.search(regex, contents)) is None:
|
if (match := re.search(regex, contents, re.IGNORECASE)) is None:
|
||||||
raise SteamInvalidManifestError()
|
raise SteamInvalidManifestError()
|
||||||
data[key] = match.group(1)
|
data[key] = match.group(1)
|
||||||
|
|
||||||
@@ -68,9 +72,11 @@ class SteamHelper:
|
|||||||
raise error
|
raise error
|
||||||
|
|
||||||
if not data["success"]:
|
if not data["success"]:
|
||||||
|
logging.debug("Appid %s not found", appid)
|
||||||
raise SteamGameNotFoundError()
|
raise SteamGameNotFoundError()
|
||||||
|
|
||||||
if data["data"]["type"] != "game":
|
if data["data"]["type"] != "game":
|
||||||
|
logging.debug("Appid %s is not a game", appid)
|
||||||
raise SteamNotAGameError()
|
raise SteamNotAGameError()
|
||||||
|
|
||||||
# Return API values we're interested in
|
# Return API values we're interested in
|
||||||
|
|||||||
Reference in New Issue
Block a user