-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminer.py
More file actions
executable file
·148 lines (117 loc) · 4.65 KB
/
Copy pathminer.py
File metadata and controls
executable file
·148 lines (117 loc) · 4.65 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
"""
Stage 1: Knowledge Miner — fetches from Gmail and Google Calendar via gws.
Reads recent emails and calendar events, saves raw JSON to data/knowledge-extraction/raw/.
Usage:
python3 miner.py [--days 7]
"""
import os
import sys
import json
import shutil
import subprocess
import argparse
from datetime import datetime, timedelta
SCRIPT_DIR = os.path.dirname(os.path.abspath(__file__))
_HH_FALLBACK = os.path.dirname(os.path.dirname(os.path.dirname(SCRIPT_DIR)))
HARVEY_HOME = os.environ.get("HARVEY_HOME", _HH_FALLBACK)
HARVEY_HOME = os.path.realpath(HARVEY_HOME)
RAW_DIR = os.path.join(HARVEY_HOME, "data", "knowledge-extraction", "raw")
os.makedirs(RAW_DIR, exist_ok=True)
GWS_PATH = os.environ.get("GWS_PATH") or shutil.which("gws") or "gws"
def log(msg: str):
ts = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
print(f"[{ts}] miner: {msg}")
def run_gws(args: list) -> str:
cmd = [GWS_PATH] + args
result = subprocess.run(cmd, capture_output=True, text=True, timeout=120)
if result.returncode != 0:
log(f"gws error: {'; '.join(result.stderr.strip().splitlines())}")
return ""
return result.stdout
def mine_emails(days: int = 7) -> dict:
"""Fetch emails from the last N days."""
query = f"newer_than:{days}d -from:noreply -from:notifications"
params = json.dumps({"userId": "me", "query": query, "maxResults": 100})
output = run_gws([
"gmail", "users", "messages", "list",
"--params", params,
"--format", "json",
])
if not output:
return {"source": "gmail", "query": query, "messages": [], "fetched_at": datetime.now().isoformat()}
try:
data = json.loads(output)
msg_ids = data.get("messages", []) or []
except json.JSONDecodeError:
log("Failed to parse Gmail list response")
return {"source": "gmail", "query": query, "messages": [], "fetched_at": datetime.now().isoformat()}
messages = []
for m in msg_ids[:50]: # cap at 50 for speed
msg_id = m["id"]
get_params = json.dumps({"userId": "me", "id": msg_id})
detail = run_gws([
"gmail", "users", "messages", "get",
"--params", get_params,
])
if detail:
try:
messages.append(json.loads(detail))
except json.JSONDecodeError:
pass
log(f"Mined {len(messages)} emails from last {days} days")
return {
"source": "gmail",
"query": query,
"days": days,
"messages": messages,
"fetched_at": datetime.now().isoformat(),
}
def mine_calendar(days: int = 7) -> dict:
"""Fetch calendar events for the next N days."""
time_min = datetime.now().strftime("%Y-%m-%dT%H:%M:%SZ")
time_max = (datetime.now() + timedelta(days=days)).strftime("%Y-%m-%dT%H:%M:%SZ")
params = json.dumps({"calendarId": "primary", "timeMin": time_min, "timeMax": time_max, "maxResults": 50})
output = run_gws([
"calendar", "events", "list",
"--params", params,
"--format", "json",
])
if not output:
return {"source": "calendar", "events": [], "fetched_at": datetime.now().isoformat()}
try:
data = json.loads(output)
events = data if isinstance(data, list) else data.get("items", [])
except json.JSONDecodeError:
log("Failed to parse Calendar response")
return {"source": "calendar", "events": [], "fetched_at": datetime.now().isoformat()}
log(f"Mined {len(events)} calendar events")
return {
"source": "calendar",
"events": events,
"fetched_at": datetime.now().isoformat(),
}
def run():
parser = argparse.ArgumentParser()
parser.add_argument("--days", type=int, default=7, help="Days to look back (default: 7)")
args = parser.parse_args()
log(f"Starting knowledge miner for last {args.days} days")
ts = datetime.now().strftime("%Y%m%d_%H%M%S")
email_data = mine_emails(args.days)
cal_data = mine_calendar(args.days)
# Save raw
email_file = os.path.join(RAW_DIR, f"emails_{ts}.json")
cal_file = os.path.join(RAW_DIR, f"calendar_{ts}.json")
with open(email_file, "w") as f:
json.dump(email_data, f, indent=2, default=str)
log(f"Saved emails to {email_file}")
with open(cal_file, "w") as f:
json.dump(cal_data, f, indent=2, default=str)
log(f"Saved calendar to {cal_file}")
# Write latest pointer
latest = {"email_file": email_file, "calendar_file": cal_file, "mined_at": datetime.now().isoformat()}
with open(os.path.join(RAW_DIR, "latest.json"), "w") as f:
json.dump(latest, f, indent=2)
log("Miner complete")
if __name__ == "__main__":
run()