Download outreach-agent/scraper/client.py from Org-Exp/tokenizer: direct link, hf CLI and curl.
- Browser
- Download file 1.93 kB
-
https://huggingface.co/datasets/Org-Exp/tokenizer/resolve/main/outreach-agent/scraper/client.py
- Command line
-
hf download hf://datasets/Org-Exp/tokenizer/outreach-agent/scraper/client.py
-
curl -L -o client.py https://huggingface.co/datasets/Org-Exp/tokenizer/resolve/main/outreach-agent/scraper/client.py
1.93 kB
| import os | |
| from typing import Any, Dict, Iterable, Optional | |
| from apify_client import ApifyClient | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| client: Optional[ApifyClient] = None | |
| def create_client() -> ApifyClient: | |
| """Create or return the shared Apify client connection.""" | |
| global client | |
| if client is None: | |
| token = os.getenv("APIFY_API_TOKEN") | |
| if not token: | |
| raise RuntimeError( | |
| "APIFY_API_TOKEN is not set. Add it to your environment or .env file." | |
| ) | |
| client = ApifyClient(token) | |
| return client | |
| class BaseClient: | |
| """Common Apify client wrapper for actor-based scrapers.""" | |
| actor_id: str = "" | |
| def __init__( | |
| self, | |
| client: Optional[ApifyClient] = None, | |
| actor_id: Optional[str] = None, | |
| default_run_input: Optional[Dict[str, Any]] = None, | |
| ) -> None: | |
| self.client = client if client is not None else create_client() | |
| self.actor_id = actor_id or self.actor_id | |
| self.default_run_input = default_run_input or {} | |
| def build_run_input(self, run_input: Optional[Dict[str, Any]] = None) -> Dict[str, Any]: | |
| combined_input = dict(self.default_run_input) | |
| if run_input: | |
| combined_input.update(run_input) | |
| return combined_input | |
| def run(self, run_input: Optional[Dict[str, Any]] = None) -> Iterable[Dict[str, Any]]: | |
| if not self.actor_id: | |
| raise NotImplementedError("No Apify actor is configured for this scraper.") | |
| actor_input = self.build_run_input(run_input) | |
| run = self.client.actor(self.actor_id).call(run_input=actor_input) | |
| if run.status != "SUCCEEDED": | |
| raise RuntimeError(f"Actor run failed with status: {run.status}") | |
| dataset_id = run.default_dataset_id | |
| if not dataset_id: | |
| return [] | |
| return list(self.client.dataset(dataset_id).iterate_items()) | |