|
|
|
|
@@ -27,6 +27,7 @@ class WaczArchiverEnricher(Enricher, Archiver):
|
|
|
|
|
def configs() -> dict:
|
|
|
|
|
return {
|
|
|
|
|
"profile": {"default": None, "help": "browsertrix-profile (for profile generation see https://github.com/webrecorder/browsertrix-crawler#creating-and-using-browser-profiles)."},
|
|
|
|
|
"docker_commands": {"default": None, "help":"if a custom docker invocation is needed"},
|
|
|
|
|
"timeout": {"default": 120, "help": "timeout for WACZ generation in seconds"},
|
|
|
|
|
"extract_media": {"default": True, "help": "If enabled all the images/videos/audio present in the WACZ archive will be extracted into separate Media. The .wacz file will be kept untouched."}
|
|
|
|
|
}
|
|
|
|
|
@@ -46,51 +47,45 @@ class WaczArchiverEnricher(Enricher, Archiver):
|
|
|
|
|
url = to_enrich.get_url()
|
|
|
|
|
|
|
|
|
|
collection = str(uuid.uuid4())[0:8]
|
|
|
|
|
browsertrix_home = os.path.abspath(ArchivingContext.get_tmp_dir())
|
|
|
|
|
browsertrix_home_host = os.environ.get('BROWSERTRIX_HOME_HOST') or os.path.abspath(ArchivingContext.get_tmp_dir())
|
|
|
|
|
browsertrix_home_container = os.environ.get('BROWSERTRIX_HOME_CONTAINER') or browsertrix_home_host
|
|
|
|
|
|
|
|
|
|
if os.getenv('RUNNING_IN_DOCKER'):
|
|
|
|
|
cmd = [
|
|
|
|
|
"crawl",
|
|
|
|
|
"--url", url,
|
|
|
|
|
"--scopeType", "page",
|
|
|
|
|
"--generateWACZ",
|
|
|
|
|
"--text",
|
|
|
|
|
"--screenshot", "fullPage",
|
|
|
|
|
"--collection", collection,
|
|
|
|
|
"--id", collection,
|
|
|
|
|
"--saveState", "never",
|
|
|
|
|
"--behaviors", "autoscroll,autoplay,autofetch,siteSpecific",
|
|
|
|
|
"--behaviorTimeout", str(self.timeout),
|
|
|
|
|
"--timeout", str(self.timeout)]
|
|
|
|
|
|
|
|
|
|
# call docker if explicitly enabled or we are running on the host (not in docker)
|
|
|
|
|
use_docker = os.environ.get('WACZ_ENABLE_DOCKER') or not os.environ.get('RUNNING_IN_DOCKER')
|
|
|
|
|
|
|
|
|
|
if use_docker:
|
|
|
|
|
logger.debug(f"generating WACZ in Docker for {url=}")
|
|
|
|
|
logger.debug(f"{browsertrix_home_host=} {browsertrix_home_container=}")
|
|
|
|
|
if self.docker_commands:
|
|
|
|
|
cmd = self.docker_commands + cmd
|
|
|
|
|
else:
|
|
|
|
|
cmd = ["docker", "run", "--rm", "-v", f"{browsertrix_home_host}:/crawls/", "webrecorder/browsertrix-crawler"] + cmd
|
|
|
|
|
|
|
|
|
|
if self.profile:
|
|
|
|
|
profile_fn = os.path.join(browsertrix_home_container, "profile.tar.gz")
|
|
|
|
|
logger.debug(f"copying {self.profile} to {profile_fn}")
|
|
|
|
|
shutil.copyfile(self.profile, profile_fn)
|
|
|
|
|
cmd.extend(["--profile", os.path.join("/crawls", "profile.tar.gz")])
|
|
|
|
|
|
|
|
|
|
else:
|
|
|
|
|
logger.debug(f"generating WACZ without Docker for {url=}")
|
|
|
|
|
|
|
|
|
|
cmd = [
|
|
|
|
|
"crawl",
|
|
|
|
|
"--url", url,
|
|
|
|
|
"--scopeType", "page",
|
|
|
|
|
"--generateWACZ",
|
|
|
|
|
"--text",
|
|
|
|
|
"--screenshot", "fullPage",
|
|
|
|
|
"--collection", collection,
|
|
|
|
|
"--id", collection,
|
|
|
|
|
"--saveState", "never",
|
|
|
|
|
"--behaviors", "autoscroll,autoplay,autofetch,siteSpecific",
|
|
|
|
|
"--behaviorTimeout", str(self.timeout),
|
|
|
|
|
"--timeout", str(self.timeout)]
|
|
|
|
|
|
|
|
|
|
if self.profile:
|
|
|
|
|
cmd.extend(["--profile", os.path.join("/app", str(self.profile))])
|
|
|
|
|
else:
|
|
|
|
|
logger.debug(f"generating WACZ in Docker for {url=}")
|
|
|
|
|
|
|
|
|
|
cmd = [
|
|
|
|
|
"docker", "run",
|
|
|
|
|
"--rm", # delete container once it has completed running
|
|
|
|
|
"-v", f"{browsertrix_home}:/crawls/",
|
|
|
|
|
# "-it", # this leads to "the input device is not a TTY"
|
|
|
|
|
"webrecorder/browsertrix-crawler", "crawl",
|
|
|
|
|
"--url", url,
|
|
|
|
|
"--scopeType", "page",
|
|
|
|
|
"--generateWACZ",
|
|
|
|
|
"--text",
|
|
|
|
|
"--screenshot", "fullPage",
|
|
|
|
|
"--collection", collection,
|
|
|
|
|
"--behaviors", "autoscroll,autoplay,autofetch,siteSpecific",
|
|
|
|
|
"--behaviorTimeout", str(self.timeout),
|
|
|
|
|
"--timeout", str(self.timeout)
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
if self.profile:
|
|
|
|
|
profile_fn = os.path.join(browsertrix_home, "profile.tar.gz")
|
|
|
|
|
shutil.copyfile(self.profile, profile_fn)
|
|
|
|
|
cmd.extend(["--profile", os.path.join("/crawls", "profile.tar.gz")])
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
logger.info(f"Running browsertrix-crawler: {' '.join(cmd)}")
|
|
|
|
|
@@ -99,18 +94,18 @@ class WaczArchiverEnricher(Enricher, Archiver):
|
|
|
|
|
logger.error(f"WACZ generation failed: {e}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
if os.getenv('RUNNING_IN_DOCKER'):
|
|
|
|
|
filename = os.path.join("collections", collection, f"{collection}.wacz")
|
|
|
|
|
if use_docker:
|
|
|
|
|
wacz_fn = os.path.join(browsertrix_home_container, "collections", collection, f"{collection}.wacz")
|
|
|
|
|
else:
|
|
|
|
|
filename = os.path.join(browsertrix_home, "collections", collection, f"{collection}.wacz")
|
|
|
|
|
wacz_fn = os.path.join("collections", collection, f"{collection}.wacz")
|
|
|
|
|
|
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
|
logger.warning(f"Unable to locate and upload WACZ {filename=}")
|
|
|
|
|
if not os.path.exists(wacz_fn):
|
|
|
|
|
logger.warning(f"Unable to locate and upload WACZ {wacz_fn=}")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
to_enrich.add_media(Media(filename), "browsertrix")
|
|
|
|
|
to_enrich.add_media(Media(wacz_fn), "browsertrix")
|
|
|
|
|
if self.extract_media:
|
|
|
|
|
self.extract_media_from_wacz(to_enrich, filename)
|
|
|
|
|
self.extract_media_from_wacz(to_enrich, wacz_fn)
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
def extract_media_from_wacz(self, to_enrich: Metadata, wacz_filename: str) -> None:
|
|
|
|
|
|