| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for https://www.steamgriddb.com""" |
| 8 |
|
| 9 |
from .common import Extractor, Message |
| 10 |
from .. import text |
| 11 |
|
| 12 |
|
| 13 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?steamgriddb\.com" |
| 14 |
LANGUAGE_CODES = ( |
| 15 |
"aa", "ab", "ae", "af", "ak", "am", "an", "ar", "as", "av", "ay", "az", |
| 16 |
"ba", "be", "bg", "bh", "bi", "bm", "bn", "bo", "br", "bs", "ca", "ce", |
| 17 |
"ch", "co", "cr", "cs", "cu", "cv", "cy", "da", "de", "dv", "dz", "ee", |
| 18 |
"el", "en", "eo", "es", "et", "eu", "fa", "ff", "fi", "fj", "fo", "fr", |
| 19 |
"fy", "ga", "gd", "gl", "gn", "gu", "gv", "ha", "he", "hi", "ho", "hr", |
| 20 |
"ht", "hu", "hy", "hz", "ia", "id", "ie", "ig", "ii", "ik", "io", "is", |
| 21 |
"it", "iu", "ja", "jv", "ka", "kg", "ki", "kj", "kk", "kl", "km", "kn", |
| 22 |
"ko", "kr", "ks", "ku", "kv", "kw", "ky", "la", "lb", "lg", "li", "ln", |
| 23 |
"lo", "lt", "lu", "lv", "mg", "mh", "mi", "mk", "ml", "mn", "mr", "ms", |
| 24 |
"mt", "my", "na", "nb", "nd", "ne", "ng", "nl", "nn", "no", "nr", "nv", |
| 25 |
"ny", "oc", "oj", "om", "or", "os", "pa", "pi", "pl", "ps", "pt", "qu", |
| 26 |
"rm", "rn", "ro", "ru", "rw", "sa", "sc", "sd", "se", "sg", "si", "sk", |
| 27 |
"sl", "sm", "sn", "so", "sq", "sr", "ss", "st", "su", "sv", "sw", "ta", |
| 28 |
"te", "tg", "th", "ti", "tk", "tl", "tn", "to", "tr", "ts", "tt", "tw", |
| 29 |
"ty", "ug", "uk", "ur", "uz", "ve", "vi", "vo", "wa", "wo", "xh", "yi", |
| 30 |
"yo", "za", "zh", "zu", |
| 31 |
) |
| 32 |
FILE_EXT_TO_MIME = { |
| 33 |
"png": "image/png", |
| 34 |
"jpeg": "image/jpeg", |
| 35 |
"jpg": "image/jpeg", |
| 36 |
"webp": "image/webp", |
| 37 |
"ico": "image/vnd.microsoft.icon", |
| 38 |
"all": "all", |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
class SteamgriddbExtractor(Extractor): |
| 43 |
"""Base class for SteamGridDB""" |
| 44 |
category = "steamgriddb" |
| 45 |
directory_fmt = ("{category}", "{subcategory}", "{game[id]}") |
| 46 |
filename_fmt = "{game[id]}_{id}_{num:>02}.{extension}" |
| 47 |
archive_fmt = "{filename}" |
| 48 |
root = "https://www.steamgriddb.com" |
| 49 |
|
| 50 |
def _init(self): |
| 51 |
self.cookies_update({ |
| 52 |
"userprefs": "%7B%22adult%22%3Afalse%7D", |
| 53 |
}) |
| 54 |
|
| 55 |
def items(self): |
| 56 |
download_fake_png = self.config("download-fake-png", True) |
| 57 |
|
| 58 |
for asset in self.assets(): |
| 59 |
fake_png = download_fake_png and asset.get("fake_png") |
| 60 |
|
| 61 |
asset["count"] = 2 if fake_png else 1 |
| 62 |
yield Message.Directory, "", asset |
| 63 |
|
| 64 |
asset["num"] = 1 |
| 65 |
url = asset["url"] |
| 66 |
yield Message.Url, url, text.nameext_from_url(url, asset) |
| 67 |
|
| 68 |
if fake_png: |
| 69 |
asset["num"] = 2 |
| 70 |
asset["_http_adjust_extension"] = False |
| 71 |
url = fake_png |
| 72 |
yield Message.Url, url, text.nameext_from_url(url, asset) |
| 73 |
|
| 74 |
def _call(self, endpoint, **kwargs): |
| 75 |
data = self.request_json(self.root + endpoint, **kwargs) |
| 76 |
if not data["success"]: |
| 77 |
raise self.exc.AbortExtraction(data["error"]) |
| 78 |
return data["data"] |
| 79 |
|
| 80 |
|
| 81 |
class SteamgriddbAssetsExtractor(SteamgriddbExtractor): |
| 82 |
"""Base class for extracting a list of assets""" |
| 83 |
|
| 84 |
def __init__(self, match): |
| 85 |
SteamgriddbExtractor.__init__(self, match) |
| 86 |
list_type = match[1] |
| 87 |
id = int(match[2]) |
| 88 |
self.game_id = id if list_type == "game" else None |
| 89 |
self.collection_id = id if list_type == "collection" else None |
| 90 |
self.page = int(p) if (p := match[3]) else 1 |
| 91 |
|
| 92 |
def assets(self): |
| 93 |
limit = 48 |
| 94 |
page = min(self.page - 1, 0) |
| 95 |
|
| 96 |
sort = self.config("sort", "score_desc") |
| 97 |
if sort not in ("score_desc", "score_asc", "score_old_desc", |
| 98 |
"score_old_asc", "age_desc", "age_asc"): |
| 99 |
raise self.exc.AbortExtraction(f"Invalid sort '{sort}'") |
| 100 |
|
| 101 |
json = { |
| 102 |
"static" : self.config("static", True), |
| 103 |
"animated": self.config("animated", True), |
| 104 |
"humor" : self.config("humor", True), |
| 105 |
"nsfw" : self.config("nsfw", True), |
| 106 |
"epilepsy": self.config("epilepsy", True), |
| 107 |
"untagged": self.config("untagged", True), |
| 108 |
|
| 109 |
"asset_type": self.asset_type, |
| 110 |
"limit": limit, |
| 111 |
"order": sort, |
| 112 |
} |
| 113 |
if self.valid_dimensions: |
| 114 |
json["dimensions"] = self.config_list( |
| 115 |
"dimensions", "dimension", self.valid_dimensions) |
| 116 |
json["styles"] = self.config_list("styles", "style", self.valid_styles) |
| 117 |
json["languages"] = self.config_list( |
| 118 |
"languages", "language", LANGUAGE_CODES) |
| 119 |
file_types = self.config_list( |
| 120 |
"file-types", "file type", self.valid_file_types) |
| 121 |
json["mime"] = [FILE_EXT_TO_MIME[i] for i in file_types] |
| 122 |
|
| 123 |
if self.game_id: |
| 124 |
json["game_id"] = [self.game_id] |
| 125 |
else: |
| 126 |
json["collection_id"] = self.collection_id |
| 127 |
|
| 128 |
while True: |
| 129 |
json["page"] = page |
| 130 |
|
| 131 |
data = self._call( |
| 132 |
"/api/public/search/assets", method="POST", json=json) |
| 133 |
for asset in data["assets"]: |
| 134 |
if not asset.get("game"): |
| 135 |
asset["game"] = data["game"] |
| 136 |
yield asset |
| 137 |
|
| 138 |
if data["total"] <= limit * page: |
| 139 |
break |
| 140 |
page += 1 |
| 141 |
|
| 142 |
def config_list(self, key, type_name, valid_values): |
| 143 |
value = self.config(key) |
| 144 |
if isinstance(value, str): |
| 145 |
value = value.split(",") |
| 146 |
|
| 147 |
if value is None or "all" in value: |
| 148 |
return ["all"] |
| 149 |
|
| 150 |
for i in value: |
| 151 |
if i not in valid_values: |
| 152 |
raise self.exc.AbortExtraction(f"Invalid {type_name} '{i}'") |
| 153 |
|
| 154 |
return value |
| 155 |
|
| 156 |
|
| 157 |
class SteamgriddbAssetExtractor(SteamgriddbExtractor): |
| 158 |
"""Extractor for a single asset""" |
| 159 |
subcategory = "asset" |
| 160 |
pattern = BASE_PATTERN + r"/(grid|hero|logo|icon)/(\d+)" |
| 161 |
example = "https://www.steamgriddb.com/grid/1234" |
| 162 |
|
| 163 |
def __init__(self, match): |
| 164 |
SteamgriddbExtractor.__init__(self, match) |
| 165 |
self.asset_type = match[1] |
| 166 |
self.asset_id = match[2] |
| 167 |
|
| 168 |
def assets(self): |
| 169 |
endpoint = "/api/public/asset/" + self.asset_type + "/" + self.asset_id |
| 170 |
asset = self._call(endpoint)["asset"] |
| 171 |
if asset is None: |
| 172 |
raise self.exc.NotFoundError( |
| 173 |
f"asset ({self.asset_type}:{self.asset_id})") |
| 174 |
return (asset,) |
| 175 |
|
| 176 |
|
| 177 |
class SteamgriddbGridsExtractor(SteamgriddbAssetsExtractor): |
| 178 |
subcategory = "grids" |
| 179 |
asset_type = "grid" |
| 180 |
pattern = BASE_PATTERN + r"/(game|collection)/(\d+)/grids(?:/(\d+))?" |
| 181 |
example = "https://www.steamgriddb.com/game/1234/grids" |
| 182 |
valid_dimensions = ("460x215", "920x430", "600x900", "342x482", "660x930", |
| 183 |
"512x512", "1024x1024") |
| 184 |
valid_styles = ("alternate", "blurred", "no_logo", "material", |
| 185 |
"white_logo") |
| 186 |
valid_file_types = ("png", "jpeg", "jpg", "webp") |
| 187 |
|
| 188 |
|
| 189 |
class SteamgriddbHeroesExtractor(SteamgriddbAssetsExtractor): |
| 190 |
subcategory = "heroes" |
| 191 |
asset_type = "hero" |
| 192 |
pattern = BASE_PATTERN + r"/(game|collection)/(\d+)/heroes(?:/(\d+))?" |
| 193 |
example = "https://www.steamgriddb.com/game/1234/heroes" |
| 194 |
valid_dimensions = ("1920x620", "3840x1240", "1600x650") |
| 195 |
valid_styles = ("alternate", "blurred", "material") |
| 196 |
valid_file_types = ("png", "jpeg", "jpg", "webp") |
| 197 |
|
| 198 |
|
| 199 |
class SteamgriddbLogosExtractor(SteamgriddbAssetsExtractor): |
| 200 |
subcategory = "logos" |
| 201 |
asset_type = "logo" |
| 202 |
pattern = BASE_PATTERN + r"/(game|collection)/(\d+)/logos(?:/(\d+))?" |
| 203 |
example = "https://www.steamgriddb.com/game/1234/logos" |
| 204 |
valid_dimensions = None |
| 205 |
valid_styles = ("official", "white", "black", "custom") |
| 206 |
valid_file_types = ("png", "webp") |
| 207 |
|
| 208 |
|
| 209 |
class SteamgriddbIconsExtractor(SteamgriddbAssetsExtractor): |
| 210 |
subcategory = "icons" |
| 211 |
asset_type = "icon" |
| 212 |
pattern = BASE_PATTERN + r"/(game|collection)/(\d+)/icons(?:/(\d+))?" |
| 213 |
example = "https://www.steamgriddb.com/game/1234/icons" |
| 214 |
valid_dimensions = [f"{i}x{i}" for i in (8, 10, 14, 16, 20, 24, |
| 215 |
28, 32, 35, 40, 48, 54, 56, 57, 60, 64, 72, 76, 80, 90, |
| 216 |
96, 100, 114, 120, 128, 144, 150, 152, 160, 180, 192, |
| 217 |
194, 256, 310, 512, 768, 1024)] |
| 218 |
valid_styles = ("official", "custom") |
| 219 |
valid_file_types = ("png", "ico") |