-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPySticky.py
102 lines (87 loc) · 3.68 KB
/
PySticky.py
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
import argparse
import os
import csv
import json
from datetime import datetime
from sqlalchemy import create_engine, inspect, text
def get_default_db_path():
username = os.getenv('USER') or os.getenv('USERNAME')
return f"C:\\Users\\{username}\\AppData\\Local\\Packages\\Microsoft.MicrosoftStickyNotes_8wekyb3d8bbwe\\LocalState\\plum.sqlite"
def load_database(db_path):
try:
engine = create_engine(f'sqlite:///{db_path}')
inspector = inspect(engine)
tables = inspector.get_table_names()
if 'Note' in tables:
with engine.connect() as connection:
query = text("SELECT Text, CreatedAt, UpdatedAt FROM Note")
result = connection.execute(query)
rows = result.fetchall()
if rows:
return rows
else:
print("No data found in the 'Note' table.")
return []
else:
print("Table 'Note' does not exist in the database.")
return []
except Exception as e:
print(f"An error occurred: {e}")
return []
def display_notes(notes):
if notes:
print("\nSticky Notes:")
for i, note in enumerate(notes, 1):
print(f"\nNote {i}:")
print(f"Content: {note[0]}")
print(f"Created: {datetime.fromtimestamp(note[1]/10000000 - 62135596800)}")
print(f"Updated: {datetime.fromtimestamp(note[2]/10000000 - 62135596800)}")
else:
print("No notes to display.")
def export_notes(notes, export_format, output_file):
if not notes:
print("No notes to export.")
return
if export_format == 'csv':
with open(output_file, 'w', newline='', encoding='utf-8') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(['Content', 'Created', 'Updated'])
for note in notes:
writer.writerow([
note[0],
datetime.fromtimestamp(note[1]/10000000 - 62135596800),
datetime.fromtimestamp(note[2]/10000000 - 62135596800)
])
elif export_format == 'json':
json_notes = []
for note in notes:
json_notes.append({
'content': note[0],
'created': str(datetime.fromtimestamp(note[1]/10000000 - 62135596800)),
'updated': str(datetime.fromtimestamp(note[2]/10000000 - 62135596800))
})
with open(output_file, 'w', encoding='utf-8') as jsonfile:
json.dump(json_notes, jsonfile, ensure_ascii=False, indent=2)
print(f"Notes exported to {output_file}")
def main():
parser = argparse.ArgumentParser(description="Load, query, and export Sticky Notes from an SQLite database.")
parser.add_argument('-f', '--file', help="Path to the SQLite database file. If not provided, uses the default path.")
parser.add_argument('-c', '--current', action="store_true", help="Use the current user's Sticky Notes database.")
parser.add_argument('-e', '--export', choices=['csv', 'json'], help="Export format (csv or json)")
parser.add_argument('-o', '--output', help="Output file name for export")
args = parser.parse_args()
if args.current:
db_path = get_default_db_path()
elif args.file:
db_path = args.file
else:
db_path = get_default_db_path()
print(f"Using database: {db_path}")
notes = load_database(db_path)
display_notes(notes)
if args.export:
if not args.output:
args.output = f"sticky_notes_export.{args.export}"
export_notes(notes, args.export, args.output)
if __name__ == "__main__":
main()