-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpython-apify-client.py
More file actions
77 lines (59 loc) · 2.25 KB
/
python-apify-client.py
File metadata and controls
77 lines (59 loc) · 2.25 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
"""Vinted Smart Scraper — Python example using the official Apify client.
Setup:
pip install apify-client
export APIFY_TOKEN="<your-token>"
python examples/python-apify-client.py
Output: same as the Node example — best-buy country and biggest arbitrage spread.
"""
from __future__ import annotations
import os
import sys
import statistics
from collections import defaultdict
from apify_client import ApifyClient
def main() -> None:
token = os.environ.get("APIFY_TOKEN")
if not token:
sys.exit("Set APIFY_TOKEN env var first")
client = ApifyClient(token)
input_data = {
"searchText": "nike air max 90",
"countries": ["fr", "es", "it"],
"maxResults": 100,
"crossCountry": True,
"currency": "EUR",
}
print(f"Starting actor with input: {input_data}")
run = client.actor("kazkn/vinted-smart-scraper").call(run_input=input_data)
items = list(client.dataset(run["defaultDatasetId"]).iterate_items())
if not items:
sys.exit("No items returned. Either no listings match or your token is rate-limited.")
# Aggregate by best-buy country
by_buy: dict[str, list[float]] = defaultdict(list)
for item in items:
country = item.get("bestBuyCountry") or item["country"]
by_buy[country].append(item["priceEur"])
print(f"\nFound {len(items)} items across {len(by_buy)} countries\n")
rows = sorted(
((c, statistics.median(p), len(p)) for c, p in by_buy.items()),
key=lambda r: r[1],
)
print(f"{'Country':<10} {'Median EUR':<12} {'Count':<6}")
print("-" * 30)
for country, median, count in rows:
print(f"{country:<10} {median:<12.2f} {count:<6}")
# Top spread
spreads = sorted(
(i for i in items if i.get("arbitrageSpread") is not None),
key=lambda i: i["arbitrageSpread"],
reverse=True,
)
if spreads:
top = spreads[0]
print("\nBiggest arbitrage spread:")
print(f" Buy in {top['bestBuyCountry']} at €{top['priceEur']:.2f}")
print(f" Sell in {top['bestSellCountry']} at €{top['bestSellPrice']:.2f}")
print(f" Spread: {top['arbitrageSpread'] * 100:.1f}%")
print(f" Listing: {top['permalink']}")
if __name__ == "__main__":
main()