Files
smartguide/service/scripts/news/ude/run_pipeline.py
kallweit 82d2c508b8
LocationBasedServices/smartguide/pipeline/head This commit looks good
feat(jobs): add per-job dockerfiles and requirements and unify logging format
2026-07-23 22:59:06 +02:00

80 lines
2.6 KiB
Python

import subprocess
import sys
import os
import shutil
import time
import argparse
import logging
logging.basicConfig(level=logging.INFO, format="%(asctime)s %(levelname)s %(message)s")
logger = logging.getLogger(__name__)
def run_command(command, cwd=None):
"""Run a shell command and exit if it fails."""
result = subprocess.run(command,
shell=True,
cwd=cwd,
#capture_output=True, #supresses output from subprocess
text=True)
if result.returncode != 0:
logger.error(f"Command failed: {command}")
logger.error(f"Error output:\n{result.stderr}")
sys.exit(result.returncode)
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Crawl the UDE 'aktuell' news pages and upload the articles to Elasticsearch."
)
parser.add_argument(
"--skip-upload",
action="store_true",
help="Only crawl into out/*.json and never connect to Elasticsearch",
)
parser.add_argument(
"--max-items",
type=int,
default=None,
help="Stop each spider after N items (for short test runs)",
)
args = parser.parse_args()
start_time = time.time()
base_dir = os.path.dirname(os.path.abspath(__file__))
crawler_dir = os.path.join(base_dir, "aktuell")
out_dir = os.path.join(crawler_dir, "out")
if os.path.exists(out_dir):
shutil.rmtree(out_dir)
os.makedirs(out_dir)
output_file_links = os.path.join(out_dir, "links.json")
output_file_meta = os.path.join(out_dir, "meta.json")
limit = f" -s CLOSESPIDER_ITEMCOUNT={args.max_items}" if args.max_items else ""
logger.info("Running Scrapy link crawler...")
run_command(f"scrapy crawl aktuell-links -O \"{output_file_links}\"{limit}", cwd=crawler_dir)
logger.info("Running Scrapy meta crawler...")
run_command(f"scrapy crawl aktuell-metadata -O \"{output_file_meta}\"{limit}", cwd=crawler_dir)
if args.skip_upload:
logger.info("Skipping upload (--skip-upload): Elasticsearch was not contacted.")
else:
logger.info("Running upload script")
run_command(f"{sys.executable} upload_aktuell.py {output_file_meta}", cwd=base_dir)
end_time = time.time()
runtime_seconds = end_time - start_time
runtime_hours = runtime_seconds / 3600
logger.info(f"Runtime: {runtime_hours:.2f} hours")
if args.skip_upload:
logger.info("Finished crawling UDE news articles (no upload).")
else:
logger.info("Finished crawling and uploading UDE news articles.")