|
1 | 1 | from dotenv import load_dotenv |
2 | 2 | load_dotenv() |
3 | 3 |
|
4 | | -from scrapegraph_py import ScrapeGraphAI, MonitorCreateRequest, MarkdownFormatConfig |
| 4 | +import json |
| 5 | +import signal |
| 6 | +import time |
| 7 | +from scrapegraph_py import ScrapeGraphAI, MonitorCreateRequest, JsonFormatConfig |
5 | 8 |
|
6 | 9 | sgai = ScrapeGraphAI() |
7 | 10 |
|
8 | 11 | res = sgai.monitor.create(MonitorCreateRequest( |
9 | | - url="https://example.com", |
10 | | - name="Example Monitor with Webhook", |
11 | | - interval="0 */6 * * *", |
| 12 | + url="https://time.is/", |
| 13 | + name="Time Monitor with Webhook", |
| 14 | + interval="*/10 * * * *", |
12 | 15 | webhook_url="https://your-webhook-endpoint.com/hook", |
13 | | - formats=[MarkdownFormatConfig()], |
| 16 | + formats=[JsonFormatConfig( |
| 17 | + prompt="Extract the current time", |
| 18 | + schema={ |
| 19 | + "type": "object", |
| 20 | + "properties": { |
| 21 | + "time": {"type": "string"}, |
| 22 | + }, |
| 23 | + "required": ["time"], |
| 24 | + }, |
| 25 | + )], |
14 | 26 | )) |
15 | 27 |
|
16 | | -if res.status == "success": |
17 | | - print("Monitor created:", res.data.cron_id) |
18 | | - print("Status:", res.data.status) |
19 | | - print("Interval:", res.data.interval) |
20 | | - print("Webhook configured") |
21 | | -else: |
22 | | - print("Failed:", res.error) |
| 28 | +if res.status != "success" or not res.data: |
| 29 | + print("Failed to create monitor:", res.error) |
| 30 | + exit(1) |
| 31 | + |
| 32 | +monitor_id = res.data.cron_id |
| 33 | +print(f"Monitor created: {monitor_id}") |
| 34 | +print(f"Interval: {res.data.interval}") |
| 35 | +print("Webhook configured") |
| 36 | +print("\nPolling for activity (Ctrl+C to stop)...\n") |
| 37 | + |
| 38 | +running = True |
| 39 | + |
| 40 | +def cleanup(_sig, _frame): |
| 41 | + global running |
| 42 | + running = False |
| 43 | + |
| 44 | +signal.signal(signal.SIGINT, cleanup) |
| 45 | + |
| 46 | +while running: |
| 47 | + activity = sgai.monitor.activity(monitor_id) |
| 48 | + if activity.status == "success" and activity.data: |
| 49 | + for tick in activity.data.ticks: |
| 50 | + changes = "CHANGED" if tick.changed else "no change" |
| 51 | + print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)") |
| 52 | + diffs = tick.diffs.model_dump(exclude_none=True) |
| 53 | + if diffs: |
| 54 | + print(f" Diffs: {json.dumps(diffs, indent=2)}") |
| 55 | + time.sleep(30) |
| 56 | + |
| 57 | +print("\nStopping monitor...") |
| 58 | +sgai.monitor.delete(monitor_id) |
| 59 | +print("Monitor deleted") |
0 commit comments