-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcost_estimation.py
More file actions
58 lines (47 loc) · 1.89 KB
/
Copy pathcost_estimation.py
File metadata and controls
58 lines (47 loc) · 1.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""Cost estimation and cost control examples using the AlterLab Python SDK."""
import asyncio
from alterlab import AlterLab, AdvancedOptions, CostControls
async def main():
async with AlterLab(api_key="sk_test_...") as client:
# 1. Estimate cost before scraping
estimate = await client.estimate_cost(
"https://example.com",
mode="js",
advanced=AdvancedOptions(render_js=True, screenshot=True),
)
print(f"Estimated credits: {estimate['estimated_credits']}")
print(f"Max possible: {estimate['max_possible_credits']}")
print(f"Confidence: {estimate['confidence']}")
# Only scrape if cost is acceptable
if estimate["estimated_credits"] <= 10:
result = await client.scrape(
"https://example.com",
mode="js",
advanced=AdvancedOptions(render_js=True, screenshot=True),
)
print(f"Actual credits: {result['billing']['total_credits']}")
# 2. Set hard credit limits
result = await client.scrape(
"https://example.com",
cost_controls=CostControls(
max_credits=5,
max_tier="2",
fail_fast=True,
),
)
# 3. Optimize for cost (try cheaper tiers first)
result = await client.scrape(
"https://example.com",
cost_controls=CostControls(prefer_cost=True),
)
# 4. Optimize for speed (skip to reliable tier)
result = await client.scrape(
"https://example.com",
cost_controls=CostControls(prefer_speed=True),
)
# 5. Check credit balance
usage = await client.get_usage()
print(f"Credits remaining: {usage['credits_available']}")
print(f"Plan: {usage['subscription_tier']}")
if __name__ == "__main__":
asyncio.run(main())