Collection of Python code to re-use across Python-based scrapers
Repo openzim/python-scraperlib, read from GitHub 8 h ago. openzim has not joined Take My Repo.
VideoWebmMedium preset: VP9 at 240k with the height capped at 720, a middle ground between VideoWebmLow and VideoWebmHigh (#271)ArticleUrlRewriter.denormalize/defuzzify (and their JS ports in wombatSetup.js), the best-effort reverse of normalize/the fuzzy rules, reconstructing a plausible (not guaranteed exact) original URL from a ZIM path (#396, #409)wombat.js so extractOriginalURL defers to a new wb_info.extract_original_url_function hook (the same way rewriteUrl already defers to wb_info.rewrite_function), letting it call denormalize/defuzzify for cases its own pywb-oriented heuristic can never handle: CSS url() reads, document.referrer, dynamically created elements, and location.replace()/.assign()/updateLocation (#396, #409)SITE_JS_PATCHES mechanism in JsRewriter, to patch specific sites' JS content at rewrite time (#394)crypto.randomUUID() when missing in a non-secure context (plain HTTP, non-localhost) (#391)crypto.subtle.digest() (SHA-1/SHA-256) when missing in a non-secure context (#393)VideoMp4High now encodes at CRF 28 instead of 20, which regularly produced a file larger than its input, and adds -movflags +faststart (#271)VideoWebmLow, VideoWebmMedium, VideoWebmHigh) now use -row-mt 1 for faster VP9 encoding (#271)VideoWebmLow to v4 and VideoWebmHigh to v3 following the -row-mt addition (#271)import zimscraperlib.zim no longer imports yt-dlp, beautifulsoup4 or lxml, and import zimscraperlib.inputs no longer imports yt-dlp: stream_file, get_retry_adapter and find_title_in_file are now imported where they are used (#403)this.<prop> so it reliably detects a leading newline (and prepends the needed ;) regardless of the amount of whitespace, and no longer wrongly rewrites this when preceded by ./$ and several spaces (#381)this.<prop> with the cleaner implementation wabac.js landed instead of ours (webrecorder/wabac.js#398, superseding our own webrecorder/wabac.js#395): a leading this.<prop> at the very start of a script is now rewritten too, and a preceding $ only blocks the rewrite when directly adjacent (a . still blocks it regardless of the amount of whitespace in between) (#404)wombat.js so element properties (img.src, script.src, link.href, a.href and its derived .pathname/.search/..., Attr.value) return and forget a server-side-rewriter-recorded original the same way getAttribute already does (#386, webrecorder/wombat#240)export const/let/var/function/async function/* as module code (EXPORT_RX only recognized export {...}, export default and export class), fixing JS rewriting wrongly injecting a non-module let arguments; wrapper into ES modules using these common export forms, which is a syntax error in strict/module code (#387)%2B with a raw + in query strings (#389)wombat.js so retrieveWBOSRCAsURL (the helper behind the above element-property fix) resolves a recorded original against the frame's true original URL (wb_info.url) instead of its replay-time ownerDocument, fixing it returning/using a URL under the wrong host for a page whose own URL was changed by a fuzzy rule (e.g. YouTube embeds), which broke playback (#398)wombat.js so CSSStyleSheet.prototype.replaceSync/.replace() (the Constructable Stylesheets API used by Shadow DOM web components) run their CSS text through the existing rewriteStyle() helper, fixing url() references left unrewritten and 404ing on replay (#410, webrecorder/wombat#249)ArticleUrlRewriter.normalize() no longer decodes a percent-encoded / (%2F) in the path into a literal /, fixing a content collision between e.g. /foo%2Fbar (one segment, containing a literal /) and /foo/bar (two segments), which used to normalize to the same ZIM path (#396, #406)ArticleUrlRewriter.escape_dot_segments() now also escapes a literal _./_.. (and further escaped forms like __./__..) segment with one more leading _, fixing a content collision with the escaped form of an actual ./.. dot-segment, which used to normalize to the same ZIM path (#396, #406)ArticleUrlRewriter.normalize() now prefixes every ZIM path with a constant replay:// marker, and wombatSetup.js's urlRewriteFunction/head_insert.html do the same, fixing wombat.js computing an empty or single-/ replay prefix ("wb_rel_prefix") for a ZIM served with no extra "collection" path segment (e.g. a bare kiwix.app/<path>), which corrupted any innerHTML/outerHTML read on the page by deleting arbitrary /text/ spans, and caused related, quieter bugs in rewriteUrl/rewriteCookie (#396, #408, openzim/zimit#589)<interactive-example> widget always 404ing on replay due to a missing deterministic permalink attribute (#394)ValueError messages (#405)wombat.js so fetch() converts POST to GET like XMLHttpRequest already does, fixing YouTube embedded player video playback (#379, webrecorder/wombat#242)zim.Creator abandoned before finishing (e.g. after an item path duplication) from hanging forever at garbage collection or interpreter exit: its libzim worker threads are now drained with the GIL released and the partially written ZIM is removed (#300)pymupdf-lite instead of pymupdf: same pymupdf module, without pymupdf 2.0 layout engine and the heavy dependencies (onnxruntime, numpy, ...) it pulled into every scraper; a scraper also depending on the full pymupdf must keep both at the same version (#372)get_epub_index_data helper and automatic indexing of ePub items, like PDFs already are (#333)User-Agent in stream_file when downloading from upload.wikimedia.org (#214)save_large_file accepts arbitrary HTTP headers and sends the default Wikimedia-compliant User-Agent for upload.wikimedia.org, like stream_file (#360)save_large_file through a new on_progress callback and periodic logging, and stream the download to a .part file that is only moved into place once complete (#348)get_pdf_title and get_epub_title helpers to get a document's title from its own metadata without extracting its full text content, cheaper than get_pdf_index_data/get_epub_index_data when only the title is needed (openzim/warc2zim#462, #359)save_large_file to stream and resume the download itself instead of running wget once, whose -t counter is a budget for the whole download: a flaky connection now keeps resuming and the retry budget only counts attempts that make no progress, so long downloads are no longer abandoned mid-way (#348)const, let, class and function declarations across the wombat block, so other scripts on the page can still see them, re-declaring classes with let so they can be reassigned (#329, #370, webrecorder/wabac.js#380)<script>'s original src in __wb_orig_src, so wombat can return it from getAttribute("src") and bundlers that identify chunks by it (Next.js/Turbopack, Vite) still work (openzim/warc2zim#473, #337)src, href and xlink:href in __wb_orig_<attr>, not just a script's src, so wombat can return it instead of a broken URL (openzim/warc2zim#413, #338)# characters of the path or querystring when rewriting document URIs, instead of dropping everything after them (#341)content-language <meta> that is not the first <meta> of the page (#356)poster attribute (e.g. of <video>) when rewriting HTML, like src and href (#339)// into a single / when normalizing URLs into ZIM paths, in both Python and JS rewriting (#340)// the way browsers do, instead of collapsing them like urllib.parse.urljoin does (#346)Creator.finish() no longer silently discards errors raised by libzim while finalizing the ZIM, so a failure during finalization is no longer reported as a successful run (openzim/warc2zim#462, #354). and .. path segments (e.g. folded into the path from a querystring) with a leading _ when normalizing URLs into ZIM paths, so they are kept as literal entry names instead of being resolved away as navigation, in both Python and JS rewriting (#321)zimscraperlib.image.optimize_xxx methods (#273)libcairo across all platforms (#152)hatch shell, clarify commands must be run from local clone root (#153)Creator.config_indexing docstring to reflect that only the full-text index is toggled; title indexing is always performed by libzim (#294)format_for to not modify input BytesIO position (#296)### Changed