122 lines
3.3 KiB
Python
122 lines
3.3 KiB
Python
"""Warm the local cover cache.
|
|
|
|
The web server already caches covers on first request. This script can prefetch
|
|
them in advance for games that have no art in the Steam client cache.
|
|
|
|
python tools/cache_covers.py --all
|
|
python tools/cache_covers.py --limit 200
|
|
"""
|
|
|
|
import argparse
|
|
import os
|
|
import sys
|
|
import time
|
|
import urllib.request
|
|
|
|
for stream in (sys.stdout, sys.stderr):
|
|
try:
|
|
stream.reconfigure(encoding="utf-8")
|
|
except Exception:
|
|
pass
|
|
|
|
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
import db
|
|
import steam_source
|
|
|
|
CDN = "https://cdn.cloudflare.steamstatic.com/steam/apps"
|
|
CACHE_NAMES = steam_source.COVER_ORDER
|
|
CDN_NAMES = (
|
|
"library_600x900.jpg",
|
|
"library_capsule.jpg",
|
|
"header.jpg",
|
|
"library_header.jpg",
|
|
"library_hero.jpg",
|
|
"logo.png",
|
|
)
|
|
|
|
|
|
def has_cached(path):
|
|
return any(os.path.isfile(os.path.join(path, name)) for name in CACHE_NAMES)
|
|
|
|
|
|
def download(url, path):
|
|
try:
|
|
req = urllib.request.Request(url, headers={"User-Agent": "Mozilla/5.0"})
|
|
with urllib.request.urlopen(req, timeout=20) as resp:
|
|
data = resp.read()
|
|
if not data:
|
|
return False
|
|
os.makedirs(os.path.dirname(path), exist_ok=True)
|
|
tmp = path + ".tmp"
|
|
with open(tmp, "wb") as f:
|
|
f.write(data)
|
|
os.replace(tmp, path)
|
|
return True
|
|
except Exception:
|
|
return False
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser(description="Prefetch game covers")
|
|
ap.add_argument("--db", default=db.DEFAULT_DB)
|
|
ap.add_argument("--limit", type=int)
|
|
ap.add_argument("--all", action="store_true")
|
|
ap.add_argument("--delay", type=float, default=0.1)
|
|
args = ap.parse_args()
|
|
|
|
base = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
cache_dir = os.path.join(base, "cache", "covers")
|
|
try:
|
|
steam_root = steam_source.find_steam_root()
|
|
lib_cache = os.path.join(steam_root, "appcache", "librarycache")
|
|
except SystemExit:
|
|
lib_cache = None
|
|
|
|
con = db.connect(args.db)
|
|
db.init(con)
|
|
rows = con.execute("""
|
|
SELECT g.appid, g.name, d.header_image
|
|
FROM games g
|
|
LEFT JOIN game_details d ON d.appid = g.appid
|
|
WHERE LOWER(g.type) = 'game'
|
|
ORDER BY COALESCE(g.review_pct, 0) DESC
|
|
""").fetchall()
|
|
if not args.all and args.limit:
|
|
rows = rows[: args.limit]
|
|
print(f"Games: {len(rows)}")
|
|
|
|
downloaded = skipped = failed = 0
|
|
for row in rows:
|
|
appid = row["appid"]
|
|
cached = os.path.join(cache_dir, str(appid))
|
|
if has_cached(cached) or steam_source.find_cover(appid, lib_cache):
|
|
skipped += 1
|
|
continue
|
|
|
|
got = False
|
|
header_image = row["header_image"]
|
|
if header_image:
|
|
got = download(header_image, os.path.join(cached, "header.jpg"))
|
|
time.sleep(args.delay)
|
|
|
|
if not got:
|
|
for name in CDN_NAMES:
|
|
url = f"{CDN}/{appid}/{name}"
|
|
got = download(url, os.path.join(cached, name))
|
|
time.sleep(args.delay)
|
|
if got:
|
|
break
|
|
|
|
if got:
|
|
downloaded += 1
|
|
else:
|
|
failed += 1
|
|
|
|
print(f"downloaded={downloaded} skipped={skipped} failed={failed}")
|
|
print(f"cache: {cache_dir}")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|