-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeepsync_clipboard_import.py
More file actions
75 lines (63 loc) · 2.19 KB
/
Copy pathkeepsync_clipboard_import.py
File metadata and controls
75 lines (63 loc) · 2.19 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
"""Import notes from Windows clipboard history."""
import sys
import uuid
from datetime import datetime, timezone
from typing import List, Optional
from keepsync_models import Note, NoteType, SyncStatus
CLIPBOARD_HISTORY_AVAILABLE = False
if sys.platform == "win32":
try:
import winrt.windows.applicationmodel.datatransfer as wrt_dt
CLIPBOARD_HISTORY_AVAILABLE = True
except ImportError:
pass
def fetch_clipboard_text_items(max_items: int = 50) -> List[dict]:
if not CLIPBOARD_HISTORY_AVAILABLE:
return []
import asyncio
import winrt.windows.applicationmodel.datatransfer as wrt_dt
async def _fetch():
items = []
try:
result = wrt_dt.Clipboard.get_history_items_async()
history = await result
if history.status != wrt_dt.ClipboardHistoryItemsResultStatus.SUCCESS:
return items
for item in history.items:
content = item.content
if content.contains(wrt_dt.StandardDataFormats.text):
text_op = content.get_text_async()
text = await text_op
if text and text.strip():
items.append({
"text": text.strip(),
"timestamp": item.timestamp.universal_time if hasattr(item, "timestamp") else None,
})
if len(items) >= max_items:
break
except Exception:
pass
return items
loop = asyncio.new_event_loop()
try:
return loop.run_until_complete(_fetch())
finally:
loop.close()
def clipboard_items_to_notes(items: List[dict]) -> List[Note]:
notes = []
for item in items:
text = item.get("text", "").strip()
if not text:
continue
lines = text.split("\n", 1)
title = lines[0][:80].strip()
content = text
notes.append(Note(
id=str(uuid.uuid4()),
title=title,
content=content,
note_type=NoteType.NOTE,
labels=["clipboard-import"],
sync_status=SyncStatus.LOCAL_ONLY,
))
return notes