-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathauto_export.py
More file actions
275 lines (224 loc) · 7.72 KB
/
auto_export.py
File metadata and controls
275 lines (224 loc) · 7.72 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python3
"""
Auto-export WeChat favorites.
Strategy: always double-click the FIRST visible item, export it,
then scroll down by one item. Verify scroll worked by comparing
list screenshots before/after.
"""
import json
import os
import re
import subprocess
import sys
import time
from datetime import datetime
import pyautogui
from PIL import Image
# === Config ===
OUTPUT_DIR = "output/全部收藏"
ITEM_X = 420 # logical x for clicking item cards
ITEM_Y = 125 # logical y of the first visible item
LIST_AREA_X = 380 # x to click list area for focus
LIST_AREA_Y = 300 # y to click list area for focus
SCROLL_AMOUNT = 10 # per scroll() call
SCROLL_CALLS = 5 # calls per item (total = 50 units)
# === Helpers ===
def activate():
subprocess.run(
['osascript', '-e', 'tell application "WeChat" to activate'],
capture_output=True, text=True,
)
def win_count():
r = subprocess.run(
['osascript', '-e',
'tell application "System Events" to tell process "WeChat" to count of windows'],
capture_output=True, text=True, timeout=10,
)
return int(r.stdout.strip())
def get_bounds():
time.sleep(0.5)
script = (
'tell application "System Events" to tell process "WeChat"\n'
'set p to position of front window\nset s to size of front window\n'
'return "" & (item 1 of p) & "," & (item 2 of p) & "," '
'& (item 1 of s) & "," & (item 2 of s)\nend tell'
)
r = subprocess.run(['osascript', '-e', script], capture_output=True, text=True, timeout=10)
if r.returncode != 0 or not r.stdout.strip():
return None
parts = [int(x.strip()) for x in r.stdout.strip().split(',')]
return tuple(parts[:4])
def close_win():
subprocess.run(
['osascript', '-e',
'tell application "System Events" to tell process "WeChat" '
'to keystroke "w" using command down'],
capture_output=True, text=True,
)
def focus_list():
"""Click on the list area to give it scroll focus."""
activate()
time.sleep(0.2)
# Single click on list area (not double-click, to avoid opening item)
pyautogui.click(LIST_AREA_X, LIST_AREA_Y)
time.sleep(0.2)
def scroll_down_one():
"""Scroll the list down by one item."""
# First ensure list area has focus
focus_list()
# Now scroll
for _ in range(SCROLL_CALLS):
pyautogui.scroll(-SCROLL_AMOUNT)
time.sleep(0.08)
time.sleep(0.3)
def list_snapshot():
"""Snapshot the first item area for comparison."""
# Capture the area where the first item card is (Retina 2x)
return pyautogui.screenshot(region=(260, 70, 220, 150))
def snapshots_differ(a, b):
"""Check if two snapshots are meaningfully different."""
a_small = a.resize((32, 32)).convert("L")
b_small = b.resize((32, 32)).convert("L")
pa, pb = list(a_small.getdata()), list(b_small.getdata())
diff = sum(abs(x - y) for x, y in zip(pa, pb))
avg_diff = diff / len(pa)
return avg_diff > 3 # threshold: avg pixel diff > 3
def long_screenshot(bounds):
"""Scroll detail window and stitch screenshots."""
x, y, w, h = bounds
pyautogui.moveTo(x + w // 2, y + h // 2)
time.sleep(0.2)
def _hash(img):
s = img.resize((8, 8)).convert("L")
return list(s.getdata())
def _similar(a, b):
ha, hb = _hash(a), _hash(b)
return sum(abs(x - y) for x, y in zip(ha, hb)) < 300
shots = []
prev = None
same = 0
for page in range(20):
shot = pyautogui.screenshot(region=bounds)
if prev and _similar(shot, prev):
same += 1
if same >= 2:
break
else:
same = 0
shots.append(shot)
prev = shot
if page < 19:
pyautogui.scroll(-5)
time.sleep(0.4)
if not shots:
return pyautogui.screenshot(region=bounds), 1
if len(shots) == 1:
return shots[0], 1
overlap = int(shots[0].height * 0.1)
total_h = shots[0].height + sum(s.height - overlap for s in shots[1:])
result = Image.new("RGB", (shots[0].width, total_h))
yp = 0
for i, s in enumerate(shots):
if i == 0:
result.paste(s, (0, 0))
yp = s.height
else:
c = s.crop((0, overlap, s.width, s.height))
result.paste(c, (0, yp))
yp += c.height
return result, len(shots)
def export_item(idx):
"""Export the first visible item in the list."""
item_dir = os.path.join(OUTPUT_DIR, f'{idx:03d}')
meta_path = os.path.join(item_dir, 'meta.json')
os.makedirs(item_dir, exist_ok=True)
activate()
time.sleep(0.3)
before = win_count()
pyautogui.doubleClick(ITEM_X, ITEM_Y)
time.sleep(1.5)
after = win_count()
if after <= before:
pyautogui.screenshot().save(os.path.join(item_dir, 'screenshot.png'))
meta = {
'index': idx,
'timestamp': datetime.now().isoformat(timespec='seconds'),
'open_failed': True,
}
with open(meta_path, 'w') as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
print(f' [{idx:03d}] no window — list screenshot saved')
return
bounds = get_bounds()
if not bounds:
close_win()
return
# Long screenshot
img, pages = long_screenshot(bounds)
img.save(os.path.join(item_dir, 'screenshot.png'))
# Copy text (scroll back to top first)
pyautogui.moveTo(bounds[0] + bounds[2] // 2, bounds[1] + bounds[3] // 2)
for _ in range(30):
pyautogui.scroll(10)
time.sleep(0.2)
pyautogui.hotkey('command', 'a')
time.sleep(0.2)
pyautogui.hotkey('command', 'c')
time.sleep(0.2)
r = subprocess.run(['pbpaste'], capture_output=True, text=True, timeout=5)
text = (r.stdout or '').strip()
if text:
with open(os.path.join(item_dir, 'content.txt'), 'w', encoding='utf-8') as f:
f.write(text)
urls = re.findall(r'https?://[^\s<>"\')》,。]+', text) if text else []
close_win()
time.sleep(0.5)
meta = {
'index': idx,
'timestamp': datetime.now().isoformat(timespec='seconds'),
'has_text': len(text) > 0,
'text_length': len(text),
'urls': urls,
'screenshot_pages': pages,
}
with open(meta_path, 'w', encoding='utf-8') as f:
json.dump(meta, f, ensure_ascii=False, indent=2)
print(f' [{idx:03d}] ✓ ({pages}p, {len(text)}c)')
def main():
max_items = int(sys.argv[1]) if len(sys.argv) > 1 else 50
os.makedirs(OUTPUT_DIR, exist_ok=True)
activate()
time.sleep(0.5)
pyautogui.click(137, 150) # 全部收藏
time.sleep(0.5)
# Scroll to top first
pyautogui.moveTo(LIST_AREA_X, LIST_AREA_Y)
for _ in range(30):
pyautogui.scroll(10)
time.sleep(0.05)
time.sleep(0.5)
print(f"Exporting up to {max_items} items from 全部收藏")
print(f"Output: {os.path.abspath(OUTPUT_DIR)}\n")
no_scroll_count = 0
for idx in range(max_items):
snap_before = list_snapshot()
print(f'Item {idx}:')
export_item(idx)
# Scroll down one item
scroll_down_one()
# Verify scroll worked
snap_after = list_snapshot()
if snapshots_differ(snap_before, snap_after):
no_scroll_count = 0
print(f' → scrolled OK')
else:
no_scroll_count += 1
print(f' → scroll may have failed ({no_scroll_count}/5)')
if no_scroll_count >= 5:
print(f'\nList not scrolling — reached end after {idx + 1} items.')
break
# Try extra scroll
scroll_down_one()
print(f'\n=== Done: {idx + 1} items exported ===')
if __name__ == "__main__":
main()