Compare commits
3 commits
2598c0397b
...
d842605098
| Author | SHA1 | Date | |
|---|---|---|---|
| d842605098 | |||
| e3281f7587 | |||
| 75f0463bc6 |
2 changed files with 37 additions and 48 deletions
|
|
@ -10,13 +10,18 @@ extends BoxArtProvider
|
||||||
## (the itch.io API does not expose them), caches the result in
|
## (the itch.io API does not expose them), caches the result in
|
||||||
## itch_art_meta.json, and uses HTTPImageFetcher for async image downloads.
|
## itch_art_meta.json, and uses HTTPImageFetcher for async image downloads.
|
||||||
|
|
||||||
const _boxart_dir := "user://boxart/itch"
|
const BOXART_DIR := "user://boxart/itch"
|
||||||
const _meta_cache_file := "itch_art_meta.json"
|
const META_CACHE_FILE := "itch_art_meta.json"
|
||||||
|
const HTTP_TIMEOUT_MS := 15000
|
||||||
|
const SCREENSHOT_REGEX_PATTERN := "https://img\\.itch\\.zone/aW1hZ2Uv[^\"]*/original/[^\"]*"
|
||||||
|
const CONNECT_POLL_DELAY_MS := 50
|
||||||
|
const READ_POLL_DELAY_MS := 10
|
||||||
|
const LOG_URL_MAX_LEN := 80
|
||||||
|
|
||||||
@export var use_caching: bool = true
|
@export var use_caching: bool = true
|
||||||
var http_image := HTTPImageFetcher.new()
|
var http_image := HTTPImageFetcher.new()
|
||||||
var _cache_dir := "itch_art"
|
var _meta_cache_dir := "itch_art"
|
||||||
var _enriched: Dictionary = {}
|
var _enriched_meta: Dictionary = {}
|
||||||
|
|
||||||
var layout_map: Dictionary = {
|
var layout_map: Dictionary = {
|
||||||
LAYOUT.GRID_PORTRAIT: "-portrait",
|
LAYOUT.GRID_PORTRAIT: "-portrait",
|
||||||
|
|
@ -28,14 +33,14 @@ var layout_map: Dictionary = {
|
||||||
|
|
||||||
func _init() -> void:
|
func _init() -> void:
|
||||||
super()
|
super()
|
||||||
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(_boxart_dir))
|
DirAccess.make_dir_recursive_absolute(ProjectSettings.globalize_path(BOXART_DIR))
|
||||||
provider_id = "itch"
|
provider_id = "itch"
|
||||||
logger_name = "BoxArtItch"
|
logger_name = "BoxArtItch"
|
||||||
|
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
super()
|
super()
|
||||||
_enriched = _load_enriched_meta()
|
_enriched_meta = _load_enriched_meta()
|
||||||
logger.info("itch.io Art Provider loaded")
|
logger.info("itch.io Art Provider loaded")
|
||||||
add_child(http_image)
|
add_child(http_image)
|
||||||
|
|
||||||
|
|
@ -45,7 +50,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
logger.error("Unsupported boxart layout: {0}".format([kind]))
|
logger.error("Unsupported boxart layout: {0}".format([kind]))
|
||||||
return null
|
return null
|
||||||
|
|
||||||
var game := _itch_game(item)
|
var game := _find_game_dict(item)
|
||||||
if game.is_empty():
|
if game.is_empty():
|
||||||
logger.warn("No itch.io game metadata found for: " + item.name)
|
logger.warn("No itch.io game metadata found for: " + item.name)
|
||||||
return null
|
return null
|
||||||
|
|
@ -58,15 +63,14 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
return null
|
return null
|
||||||
|
|
||||||
# Enrich on first access: scrape the itch.io page for screenshots.
|
# Enrich on first access: scrape the itch.io page for screenshots.
|
||||||
var meta: Dictionary = _enriched.get(game_id, {})
|
var meta: Dictionary = _enriched_meta.get(game_id, {})
|
||||||
if meta.is_empty() and game.has("url"):
|
if meta.is_empty() and game.has("url"):
|
||||||
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
|
logger.info("Enriching metadata for: %s (url=%s)" % [title, game.get("url", "")])
|
||||||
meta = _fetch_game_page_metadata(game)
|
meta = _fetch_game_page_metadata(game)
|
||||||
# Cache even empty results to avoid re-scraping on every request.
|
|
||||||
if meta.is_empty():
|
if meta.is_empty():
|
||||||
meta = {"screenshots": []}
|
logger.warn("Page scrape returned no metadata for: %s" % title)
|
||||||
logger.info("Page scrape returned no screenshots for: %s (cached as empty)" % title)
|
else:
|
||||||
_enriched[game_id] = meta
|
_enriched_meta[game_id] = meta
|
||||||
_save_enriched_meta(game_id, meta)
|
_save_enriched_meta(game_id, meta)
|
||||||
elif not meta.is_empty():
|
elif not meta.is_empty():
|
||||||
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
|
logger.info("Using cached metadata for: %s (%d screenshots)" % [title, meta.get("screenshots", []).size()])
|
||||||
|
|
@ -85,7 +89,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
if cover_url.is_empty():
|
if cover_url.is_empty():
|
||||||
logger.warn("No cover URL for: %s (id=%d)" % [title, game_id])
|
logger.warn("No cover URL for: %s (id=%d)" % [title, game_id])
|
||||||
return null
|
return null
|
||||||
logger.info("cover_url=%s" % cover_url.left(80))
|
logger.info("cover_url=%s" % cover_url.left(LOG_URL_MAX_LEN))
|
||||||
|
|
||||||
var cache_flags := Cache.FLAGS.NONE
|
var cache_flags := Cache.FLAGS.NONE
|
||||||
if use_caching:
|
if use_caching:
|
||||||
|
|
@ -98,7 +102,7 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
var tex_b := await http_image.fetch(non_gif_screenshots[1], cache_flags)
|
var tex_b := await http_image.fetch(non_gif_screenshots[1], cache_flags)
|
||||||
if tex_a != null and tex_b != null:
|
if tex_a != null and tex_b != null:
|
||||||
return _combine_side_by_side(tex_a, tex_b)
|
return _combine_side_by_side(tex_a, tex_b)
|
||||||
# Fall through to single-image path if either failed.
|
# Banner combine failed — fall back to single-image path.
|
||||||
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
|
logger.warn("Banner screenshot fetch failed: tex_a=%s tex_b=%s" % [str(tex_a != null), str(tex_b != null)])
|
||||||
if tex_a != null:
|
if tex_a != null:
|
||||||
return tex_a
|
return tex_a
|
||||||
|
|
@ -110,15 +114,15 @@ func get_boxart(item: LibraryItem, kind: LAYOUT) -> Texture2D:
|
||||||
logger.warn("URL for layout %s is empty (cover=%s)" % [str(kind), cover_url])
|
logger.warn("URL for layout %s is empty (cover=%s)" % [str(kind), cover_url])
|
||||||
return null
|
return null
|
||||||
|
|
||||||
logger.info("Fetching itch.io box art for: %s layout=%s url=%s" % [item.name, str(kind), url.left(80)])
|
logger.info("Fetching itch.io box art for: %s layout=%s url=%s" % [item.name, str(kind), url.left(LOG_URL_MAX_LEN)])
|
||||||
var texture: Texture2D = await http_image.fetch(url, cache_flags)
|
var texture: Texture2D = await http_image.fetch(url, cache_flags)
|
||||||
if texture == null:
|
if texture == null:
|
||||||
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(80)])
|
logger.warn("Image download returned null for: %s url=%s" % [item.name, url.left(LOG_URL_MAX_LEN)])
|
||||||
return texture
|
return texture
|
||||||
|
|
||||||
|
|
||||||
## Finds the itch.io game dict from the library item's launch items.
|
## Finds the itch.io game dict from the library item's launch items.
|
||||||
func _itch_game(item: LibraryItem) -> Dictionary:
|
func _find_game_dict(item: LibraryItem) -> Dictionary:
|
||||||
if item.launch_items.is_empty():
|
if item.launch_items.is_empty():
|
||||||
logger.warn("Item has no launch items: " + item.name)
|
logger.warn("Item has no launch items: " + item.name)
|
||||||
return {}
|
return {}
|
||||||
|
|
@ -155,17 +159,6 @@ func _url_for_layout(kind: LAYOUT, cover_url: String, screenshots: Array) -> Str
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|
||||||
func _cover_url(game: Dictionary) -> String:
|
|
||||||
var still: String = game.get("stillCoverUrl", "")
|
|
||||||
if not still.is_empty() and not still.to_lower().ends_with(".gif"):
|
|
||||||
return still
|
|
||||||
# stillCoverUrl is empty or is a GIF — prefer it if non-GIF alternatives
|
|
||||||
# aren't available, but log a warning.
|
|
||||||
if not still.is_empty():
|
|
||||||
logger.debug("stillCoverUrl is a GIF, will try alternatives: " + still)
|
|
||||||
return game.get("coverUrl", "")
|
|
||||||
|
|
||||||
|
|
||||||
## Returns a non-GIF URL suitable for portrait/logo from the game data.
|
## Returns a non-GIF URL suitable for portrait/logo from the game data.
|
||||||
## Falls back through: stillCoverUrl → coverUrl (non-GIF) → first non-GIF
|
## Falls back through: stillCoverUrl → coverUrl (non-GIF) → first non-GIF
|
||||||
## screenshot.
|
## screenshot.
|
||||||
|
|
@ -188,11 +181,11 @@ func _best_portrait_url(game: Dictionary, screenshots: Array) -> String:
|
||||||
|
|
||||||
## Combines two textures side-by-side into a single banner image.
|
## Combines two textures side-by-side into a single banner image.
|
||||||
## Both images are scaled to the same height, then placed left and right.
|
## Both images are scaled to the same height, then placed left and right.
|
||||||
func _combine_side_by_side(a: Texture2D, b: Texture2D) -> Texture2D:
|
func _combine_side_by_side(left: Texture2D, right: Texture2D) -> Texture2D:
|
||||||
var img_a := a.get_image()
|
var img_a := left.get_image()
|
||||||
var img_b := b.get_image()
|
var img_b := right.get_image()
|
||||||
if img_a == null or img_b == null:
|
if img_a == null or img_b == null:
|
||||||
return a
|
return left
|
||||||
|
|
||||||
# Scale both to the same height (use the taller one).
|
# Scale both to the same height (use the taller one).
|
||||||
var target_h: int = max(img_a.get_height(), img_b.get_height())
|
var target_h: int = max(img_a.get_height(), img_b.get_height())
|
||||||
|
|
@ -226,28 +219,28 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
if err != OK:
|
if err != OK:
|
||||||
logger.warn("HTTP connect failed for %s: %d" % [page_url, err])
|
logger.warn("HTTP connect failed for %s: %d" % [page_url, err])
|
||||||
return {}
|
return {}
|
||||||
var deadline := Time.get_ticks_msec() + 15000
|
var deadline := Time.get_ticks_msec() + HTTP_TIMEOUT_MS
|
||||||
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
|
while http.get_status() == HTTPClient.STATUS_CONNECTING or http.get_status() == HTTPClient.STATUS_RESOLVING:
|
||||||
http.poll()
|
http.poll()
|
||||||
if Time.get_ticks_msec() > deadline:
|
if Time.get_ticks_msec() > deadline:
|
||||||
http.close()
|
http.close()
|
||||||
logger.warn("HTTP connect timed out for: " + page_url)
|
logger.warn("HTTP connect timed out for: " + page_url)
|
||||||
return {}
|
return {}
|
||||||
OS.delay_msec(50)
|
OS.delay_msec(CONNECT_POLL_DELAY_MS)
|
||||||
if http.get_status() != HTTPClient.STATUS_CONNECTED:
|
if http.get_status() != HTTPClient.STATUS_CONNECTED:
|
||||||
logger.warn("HTTP not connected for %s: status=%d" % [page_url, http.get_status()])
|
logger.warn("HTTP not connected for %s: status=%d" % [page_url, http.get_status()])
|
||||||
http.close()
|
http.close()
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
http.request(HTTPClient.METHOD_GET, "/" + "/".join(parts.slice(3)), PackedStringArray())
|
http.request(HTTPClient.METHOD_GET, "/" + "/".join(parts.slice(3)), PackedStringArray())
|
||||||
deadline = Time.get_ticks_msec() + 15000
|
deadline = Time.get_ticks_msec() + HTTP_TIMEOUT_MS
|
||||||
while http.get_status() == HTTPClient.STATUS_REQUESTING:
|
while http.get_status() == HTTPClient.STATUS_REQUESTING:
|
||||||
http.poll()
|
http.poll()
|
||||||
if Time.get_ticks_msec() > deadline:
|
if Time.get_ticks_msec() > deadline:
|
||||||
http.close()
|
http.close()
|
||||||
logger.warn("HTTP request timed out for: " + page_url)
|
logger.warn("HTTP request timed out for: " + page_url)
|
||||||
return {}
|
return {}
|
||||||
OS.delay_msec(10)
|
OS.delay_msec(READ_POLL_DELAY_MS)
|
||||||
|
|
||||||
var body := PackedByteArray()
|
var body := PackedByteArray()
|
||||||
while http.get_status() == HTTPClient.STATUS_BODY:
|
while http.get_status() == HTTPClient.STATUS_BODY:
|
||||||
|
|
@ -258,7 +251,7 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
http.close()
|
http.close()
|
||||||
logger.warn("HTTP body read timed out for: " + page_url)
|
logger.warn("HTTP body read timed out for: " + page_url)
|
||||||
return {}
|
return {}
|
||||||
OS.delay_msec(10)
|
OS.delay_msec(READ_POLL_DELAY_MS)
|
||||||
continue
|
continue
|
||||||
body.append_array(chunk)
|
body.append_array(chunk)
|
||||||
http.close()
|
http.close()
|
||||||
|
|
@ -268,12 +261,8 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
logger.warn("Empty HTML response for: " + page_url)
|
logger.warn("Empty HTML response for: " + page_url)
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
if html.find("challenge-platform") != -1 or html.find("cf-browser-verification") != -1:
|
|
||||||
logger.warn("Cloudflare challenge detected for: " + page_url)
|
|
||||||
return {}
|
|
||||||
|
|
||||||
var shot_re := RegEx.new()
|
var shot_re := RegEx.new()
|
||||||
shot_re.compile("https://img\\.itch\\.zone/aW1hZ2Uv[^\"]*/original/[^\"]*")
|
shot_re.compile(SCREENSHOT_REGEX_PATTERN)
|
||||||
var screenshots: Array = []
|
var screenshots: Array = []
|
||||||
for m in shot_re.search_all(html):
|
for m in shot_re.search_all(html):
|
||||||
var url: String = m.get_string(0)
|
var url: String = m.get_string(0)
|
||||||
|
|
@ -287,7 +276,7 @@ func _fetch_game_page_metadata(game: Dictionary) -> Dictionary:
|
||||||
|
|
||||||
|
|
||||||
func _load_enriched_meta() -> Dictionary:
|
func _load_enriched_meta() -> Dictionary:
|
||||||
var raw: Variant = Cache.get_json(_cache_dir, _meta_cache_file)
|
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
|
||||||
if typeof(raw) != TYPE_DICTIONARY:
|
if typeof(raw) != TYPE_DICTIONARY:
|
||||||
return {}
|
return {}
|
||||||
var out := {}
|
var out := {}
|
||||||
|
|
@ -297,8 +286,8 @@ func _load_enriched_meta() -> Dictionary:
|
||||||
|
|
||||||
|
|
||||||
func _save_enriched_meta(game_id: int, meta: Dictionary) -> void:
|
func _save_enriched_meta(game_id: int, meta: Dictionary) -> void:
|
||||||
var raw: Variant = Cache.get_json(_cache_dir, _meta_cache_file)
|
var raw: Variant = Cache.get_json(_meta_cache_dir, META_CACHE_FILE)
|
||||||
if typeof(raw) != TYPE_DICTIONARY:
|
if typeof(raw) != TYPE_DICTIONARY:
|
||||||
raw = {}
|
raw = {}
|
||||||
raw[str(game_id)] = meta
|
raw[str(game_id)] = meta
|
||||||
Cache.save_json(_cache_dir, _meta_cache_file, raw)
|
Cache.save_json(_meta_cache_dir, META_CACHE_FILE, raw)
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,6 @@ var icon := preload("res://plugins/itch-artprovider/assets/itch.svg")
|
||||||
|
|
||||||
func _ready() -> void:
|
func _ready() -> void:
|
||||||
logger = Log.get_logger("ItchArt", Log.LEVEL.INFO)
|
logger = Log.get_logger("ItchArt", Log.LEVEL.INFO)
|
||||||
var boxart: BoxArtProvider = load("res://plugins/itch-artprovider/core/boxart_itch.tscn").instantiate()
|
var boxart_provider: BoxArtProvider = load("res://plugins/itch-artprovider/core/boxart_itch.tscn").instantiate()
|
||||||
add_child(boxart)
|
add_child(boxart_provider)
|
||||||
logger.info("itch.io Art Provider loaded")
|
logger.info("itch.io Art Provider loaded")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue