-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjournal_sync.py
More file actions
330 lines (272 loc) · 12 KB
/
journal_sync.py
File metadata and controls
330 lines (272 loc) · 12 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#!/usr/bin/env python3
"""
Journal Sync - Simple tool to sync README.md posts to GitHub Gists
Only updates changed posts, creates new gists for new posts
"""
import os
import sys
import json
import hashlib
import subprocess
from datetime import datetime
from pathlib import Path
from typing import Dict, List, Optional, Tuple
try:
import requests
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from rich.progress import Progress, SpinnerColumn, TextColumn
except ImportError:
print("Missing dependencies. Install with: uv add rich requests")
sys.exit(1)
class JournalSync:
def __init__(self):
self.console = Console()
self.readme_path = Path("README.md")
self.config_file = Path.home() / ".config" / "journal_sync.json"
self.posts_db_file = Path.home() / ".config" / "journal_posts.json"
# Load configuration
self.config = self.load_config()
self.posts_db = self.load_posts_db()
def load_config(self) -> Dict:
"""Load configuration from file"""
default_config = {
"github_token": "",
"gist_id": "",
"gist_filename": "coding-journal.md",
"default_gist_filename": "post.md"
}
if self.config_file.exists():
try:
with open(self.config_file, 'r') as f:
config = json.load(f)
return {**default_config, **config}
except (json.JSONDecodeError, IOError):
self.console.print("[red]Error loading config, using defaults[/red]")
return default_config
def load_posts_db(self) -> Dict:
"""Load posts database from file"""
if self.posts_db_file.exists():
try:
with open(self.posts_db_file, 'r') as f:
return json.load(f)
except (json.JSONDecodeError, IOError):
self.console.print("[red]Error loading posts DB, starting fresh[/red]")
return {"posts": {}}
def save_posts_db(self):
"""Save posts database to file"""
self.posts_db_file.parent.mkdir(parents=True, exist_ok=True)
with open(self.posts_db_file, 'w') as f:
json.dump(self.posts_db, f, indent=2)
def parse_readme_posts(self) -> List[Dict]:
"""Parse README.md and extract all posts"""
if not self.readme_path.exists():
self.console.print("[red]README.md not found![/red]")
return []
with open(self.readme_path, 'r', encoding='utf-8') as f:
content = f.read()
posts = []
lines = content.split('\n')
i = 0
while i < len(lines):
line = lines[i].strip()
# Look for post headers (### Tool/Technology Name)
if line.startswith('### ') and not line.startswith('####'):
post_title = line[4:].strip()
# Find the end of this post (next ### or end of file)
start_line = i
end_line = i
# Look for the end of this post
j = i + 1
while j < len(lines):
if lines[j].strip().startswith('### ') and not lines[j].strip().startswith('####'):
end_line = j - 1
break
j += 1
else:
end_line = len(lines) - 1
# Extract post content
post_content = '\n'.join(lines[start_line:end_line + 1])
# Generate post ID from title
post_id = self.generate_post_id(post_title)
# Calculate content hash
content_hash = hashlib.sha256(post_content.encode()).hexdigest()
posts.append({
'id': post_id,
'title': post_title,
'content': post_content,
'content_hash': content_hash,
'start_line': start_line,
'end_line': end_line
})
i = j
else:
i += 1
return posts
def generate_post_id(self, title: str) -> str:
"""Generate a unique post ID from title"""
# Convert title to lowercase, replace spaces with hyphens, remove special chars
import re
post_id = re.sub(r'[^\w\s-]', '', title.lower())
post_id = re.sub(r'[-\s]+', '-', post_id)
return post_id.strip('-')
def get_changed_posts(self, current_posts: List[Dict]) -> Tuple[List[Dict], List[Dict]]:
"""Compare current posts with database to find changes"""
changed_posts = []
new_posts = []
for post in current_posts:
post_id = post['id']
if post_id in self.posts_db['posts']:
# Existing post - check if content changed
stored_hash = self.posts_db['posts'][post_id]['content_hash']
if post['content_hash'] != stored_hash:
changed_posts.append(post)
else:
# New post
new_posts.append(post)
return changed_posts, new_posts
def create_gist(self, post: Dict) -> Optional[str]:
"""Create a new gist for a post"""
headers = {
"Authorization": f"token {self.config['github_token']}",
"Accept": "application/vnd.github.v3+json"
}
# Use post title as filename
filename = f"{post['id']}.md"
data = {
"description": f"Coding Journal: {post['title']}",
"public": True,
"files": {
filename: {
"content": post['content']
}
}
}
try:
response = requests.post("https://api.github.com/gists",
headers=headers, json=data)
if response.status_code == 201:
gist_data = response.json()
gist_id = gist_data['id']
self.console.print(f"[green]Created gist for '{post['title']}': {gist_data['html_url']}[/green]")
return gist_id
else:
self.console.print(f"[red]Failed to create gist: {response.text}[/red]")
return None
except Exception as e:
self.console.print(f"[red]Error creating gist: {e}[/red]")
return None
def update_gist(self, post: Dict, gist_id: str) -> bool:
"""Update an existing gist"""
headers = {
"Authorization": f"token {self.config['github_token']}",
"Accept": "application/vnd.github.v3+json"
}
# Get current gist to find filename
try:
response = requests.get(f"https://api.github.com/gists/{gist_id}", headers=headers)
if response.status_code != 200:
self.console.print(f"[red]Failed to get gist info: {response.text}[/red]")
return False
gist_data = response.json()
files = gist_data['files']
# Find the markdown file (should be only one)
filename = None
for file_key in files.keys():
if file_key.endswith('.md'):
filename = file_key
break
if not filename:
self.console.print("[red]No markdown file found in gist[/red]")
return False
# Update the gist
data = {
"description": f"Coding Journal: {post['title']}",
"files": {
filename: {
"content": post['content']
}
}
}
response = requests.patch(f"https://api.github.com/gists/{gist_id}",
headers=headers, json=data)
if response.status_code == 200:
self.console.print(f"[green]Updated gist for '{post['title']}': {response.json()['html_url']}[/green]")
return True
else:
self.console.print(f"[red]Failed to update gist: {response.text}[/red]")
return False
except Exception as e:
self.console.print(f"[red]Error updating gist: {e}[/red]")
return False
def sync(self):
"""Main sync function"""
self.console.print(Panel.fit("🔄 Journal Sync", style="bold blue"))
# Parse current posts from README.md
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
console=self.console
) as progress:
task = progress.add_task("Parsing README.md...", total=None)
current_posts = self.parse_readme_posts()
progress.update(task, description=f"Found {len(current_posts)} posts")
if not current_posts:
self.console.print("[yellow]No posts found in README.md[/yellow]")
return
# Find changed and new posts
changed_posts, new_posts = self.get_changed_posts(current_posts)
# Display summary
table = Table(title="Sync Summary")
table.add_column("Type", style="cyan")
table.add_column("Count", style="green")
table.add_column("Posts", style="white")
table.add_row("New", str(len(new_posts)),
", ".join([p['title'][:30] + "..." if len(p['title']) > 30
else p['title'] for p in new_posts]))
table.add_row("Changed", str(len(changed_posts)),
", ".join([p['title'][:30] + "..." if len(p['title']) > 30
else p['title'] for p in changed_posts]))
table.add_row("Total", str(len(current_posts)), "")
self.console.print(table)
if not new_posts and not changed_posts:
self.console.print("[green]No changes detected. All posts are up to date![/green]")
return
# Process new posts
for post in new_posts:
self.console.print(f"\n[bold]Creating gist for new post: {post['title']}[/bold]")
gist_id = self.create_gist(post)
if gist_id:
# Update posts database
self.posts_db['posts'][post['id']] = {
'gist_id': gist_id,
'content_hash': post['content_hash'],
'title': post['title'],
'last_modified': datetime.now().isoformat()
}
# Process changed posts
for post in changed_posts:
self.console.print(f"\n[bold]Updating gist for changed post: {post['title']}[/bold]")
stored_post = self.posts_db['posts'][post['id']]
gist_id = stored_post['gist_id']
if self.update_gist(post, gist_id):
# Update posts database
self.posts_db['posts'][post['id']].update({
'content_hash': post['content_hash'],
'last_modified': datetime.now().isoformat()
})
# Save updated database
self.save_posts_db()
self.console.print(f"\n[green]✅ Sync completed![/green]")
self.console.print(f"📊 Processed: {len(new_posts)} new, {len(changed_posts)} changed")
def main():
"""Main entry point"""
if len(sys.argv) > 1 and sys.argv[1] == "sync":
sync = JournalSync()
sync.sync()
else:
print("Usage: python journal_sync.py sync")
print("This will sync your README.md posts to GitHub Gists")
if __name__ == "__main__":
main()