Repository navigation
Expand file tree
/
Copy pathmonitor_prices.py
More file actions
47 lines (36 loc) · 2.06 KB
/
Copy pathmonitor_prices.py
File metadata and controls
47 lines (36 loc) · 2.06 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
"""Amazon price tracker: the price drops, rises and new products since the previous run.
Runs the same search in price-monitoring mode. The Actor keeps the last price
of every product in a key-value store of your Apify account, so this script
can run from cron on any machine. The first run records the reference; later
runs return only the new products and the price changes, appended to a CSV
history.
export APIFY_TOKEN="<your-apify-token>"
python monitor_prices.py "robot vacuum" --price-max 300 --store amazon-robot-vacuums
"""
from scrape import COLUMNS, build_input, make_parser, run_actor, write_csv
MONITOR_COLUMNS = ["changeType", "previousPrice", "priceDelta", "priceDeltaPercent"]
def main():
parser = make_parser("Amazon price tracker: new products and price changes since the previous run.")
parser.add_argument("--store", default="amazon-price-monitor",
help="key-value store of your Apify account that keeps the previous prices "
"(one name per schedule)")
parser.add_argument("--history", default="amazon_price_changes.csv",
help="CSV file the changes are appended to (default amazon_price_changes.csv)")
args = parser.parse_args()
run_input = build_input(args)
run_input["monitorPrices"] = True
run_input["onlyChanges"] = True
run_input["monitorStoreName"] = args.store
rows = run_actor(run_input, args.max_charge)
if not rows:
print("Nothing moved since the previous run.")
return
write_csv(rows, args.history, COLUMNS + MONITOR_COLUMNS, append=True)
changed = [row for row in rows if row["changeType"] == "price_changed"]
new = len(rows) - len(changed)
print(f"Price changes: {len(changed)}, new products: {new} (appended to {args.history})")
for row in sorted(changed, key=lambda row: row["priceDeltaPercent"] or 0):
print(f'{row["previousPrice"]} -> {row["price"]} {row["currency"]} '
f'({row["priceDeltaPercent"]} %) {row["title"][:80]} {row["url"]}')
if __name__ == "__main__":
main()