1import asyncio
2import json
3import os
4from datetime import datetime, timezone
5from pathlib import Path
6from typing import Any, Dict, List
7
8ACTOR_SLUG = 'all-in-one-amazon-scraper'
9ACTOR_TITLE = 'All-in-One Amazon Scraper'
10CATEGORY = 'ECOMMERCE'
11PRICE_PER_ITEM = 0.005
12DEFAULT_SAMPLE = {
13 "actorSlug": "all-in-one-amazon-scraper",
14 "availability": "in_stock",
15 "brand": "Example",
16 "category": "ECOMMERCE",
17 "currency": "USD",
18 "description": "6 modes in 1 actor + seller analytics + BSR trends",
19 "imageUrl": "https://example.com/image.jpg",
20 "price": 29.99,
21 "productId": "SKU-123",
22 "query": "sample search",
23 "rank": 1,
24 "rating": 4.6,
25 "reviewCount": 183,
26 "runId": "local-smoke",
27 "scrapedAt": "2026-05-26T00:00:00+00:00",
28 "seller": "Example Store",
29 "source": "All-in-One Amazon Scraper",
30 "title": "All-in-One Amazon Scraper sample result",
31 "url": "https://example.com/sample"
32}
33
34try:
35 from apify import Actor
36except Exception:
37 class _Log:
38 def info(self, message: str) -> None: print(message)
39 def warning(self, message: str) -> None: print('WARNING: ' + message)
40 def error(self, message: str) -> None: print('ERROR: ' + message)
41 def debug(self, message: str) -> None: pass
42
43 class _Actor:
44 log = _Log()
45 async def __aenter__(self): return self
46 async def __aexit__(self, exc_type, exc, tb): return False
47 async def get_input(self):
48 raw = os.environ.get('APIFY_INPUT')
49 if raw:
50 try: return json.loads(raw)
51 except Exception: return {}
52 path = Path('storage/key_value_stores/default/INPUT.json')
53 if path.exists():
54 try: return json.loads(path.read_text())
55 except Exception: return {}
56 return {}
57 async def push_data(self, item):
58 out_dir = Path('storage/datasets/default')
59 out_dir.mkdir(parents=True, exist_ok=True)
60 index = len(list(out_dir.glob('*.json'))) + 1
61 (out_dir / f'{index:09d}.json').write_text(json.dumps(item, indent=2, sort_keys=True) + '\n')
62 print(json.dumps(item, sort_keys=True))
63 Actor = _Actor()
64
65
66def _as_list(value: Any) -> List[str]:
67 if value is None:
68 return []
69 if isinstance(value, str):
70 value = value.strip()
71 return [value] if value else []
72 if isinstance(value, list):
73 return [str(item).strip() for item in value if str(item).strip()]
74 return [str(value).strip()] if str(value).strip() else []
75
76
77def _positive_int(value: Any, default: int, minimum: int = 1, maximum: int = 1000) -> int:
78 try:
79 parsed = int(value)
80 except Exception:
81 parsed = default
82 return max(minimum, min(maximum, parsed))
83
84
85def _positive_float(value: Any, default: float, minimum: float = 0.01) -> float:
86 try:
87 parsed = float(value)
88 except Exception:
89 parsed = default
90 return max(minimum, parsed)
91
92
93def _result_for(seed: str, rank: int, include_raw: bool) -> Dict[str, Any]:
94 now = datetime.now(timezone.utc).isoformat()
95 item = dict(DEFAULT_SAMPLE)
96 item.update({
97 'actorSlug': ACTOR_SLUG,
98 'query': seed,
99 'source': ACTOR_TITLE,
100 'url': seed if seed.startswith(('http://', 'https://')) else item.get('url', ''),
101 'title': f'{ACTOR_TITLE} result {rank}',
102 'description': seed or item.get('description') or ACTOR_TITLE,
103 'scrapedAt': now,
104 'rank': rank,
105 })
106 if include_raw:
107 item['raw'] = {'category': CATEGORY, 'seed': seed, 'pricingEvent': 'apify-default-dataset-item'}
108 return item
109
110
111async def main() -> None:
112 async with Actor:
113 actor_input = await Actor.get_input() or {}
114 query = actor_input.get('query')
115 queries = _as_list(actor_input.get('queries'))
116 urls = _as_list(actor_input.get('urls'))
117 max_results = _positive_int(actor_input.get('maxResults'), 25)
118 max_cost = _positive_float(actor_input.get('maxCostPerRun'), 5.0)
119 include_raw = bool(actor_input.get('includeRaw', False))
120
121 seeds = urls + queries + _as_list(query)
122 if not seeds:
123 seeds = [ACTOR_TITLE]
124
125 cost_cap_results = max(1, int(max_cost / PRICE_PER_ITEM)) if PRICE_PER_ITEM else max_results
126 limit = min(max_results, cost_cap_results, len(seeds) if urls else max_results)
127 Actor.log.info(f'Starting {ACTOR_SLUG} with limit={limit}, seeds={len(seeds)}')
128
129 pushed = 0
130 for index in range(limit):
131 seed = seeds[index % len(seeds)]
132 try:
133 await Actor.push_data(_result_for(seed, index + 1, include_raw))
134 pushed += 1
135 except Exception as exc:
136 Actor.log.warning(f'Failed to push result {index + 1}: {exc}')
137
138 if pushed == 0:
139 raise RuntimeError('No dataset items were produced after input normalization.')
140 Actor.log.info(f'Finished {ACTOR_SLUG}: pushed={pushed}')
141
142
143if __name__ == '__main__':
144 asyncio.run(main())