-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathmonitor_basic_async.py
More file actions
61 lines (52 loc) · 2.1 KB
/
monitor_basic_async.py
File metadata and controls
61 lines (52 loc) · 2.1 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
from dotenv import load_dotenv
load_dotenv()
import asyncio
import json
from scrapegraph_py import AsyncScrapeGraphAI, MonitorCreateRequest, JsonFormatConfig
async def main():
async with AsyncScrapeGraphAI() as sgai:
res = await sgai.monitor.create(MonitorCreateRequest(
url="https://time.is/",
name="Time Monitor",
interval="*/10 * * * *",
formats=[JsonFormatConfig(
prompt="Extract the current time",
schema={
"type": "object",
"properties": {
"time": {"type": "string"},
},
"required": ["time"],
},
)],
))
if res.status != "success" or not res.data:
print("Failed to create monitor:", res.error)
return
monitor_id = res.data.cron_id
print(f"Monitor created: {monitor_id}")
print(f"Interval: {res.data.interval}")
print("\nPolling for activity (Ctrl+C to stop)...\n")
seen_ids = set()
try:
while True:
activity = await sgai.monitor.activity(monitor_id)
if activity.status == "success" and activity.data:
for tick in activity.data.ticks:
if tick.id in seen_ids:
continue
seen_ids.add(tick.id)
changes = "CHANGED" if tick.changed else "no change"
print(f"[{tick.created_at}] {tick.status} - {changes} ({tick.elapsed_ms}ms)")
diffs = tick.diffs.model_dump(exclude_none=True)
if diffs:
print(f" Diffs: {json.dumps(diffs, indent=2)}")
elif tick.changed:
print(" (no diffs data)")
await asyncio.sleep(30)
except asyncio.CancelledError:
pass
print("\nStopping monitor...")
await sgai.monitor.delete(monitor_id)
print("Monitor deleted")
asyncio.run(main())