-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_hashnode.py
More file actions
148 lines (121 loc) · 3.64 KB
/
Copy pathsync_hashnode.py
File metadata and controls
148 lines (121 loc) · 3.64 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
"""
Hashnode to GitHub Sync Script
Fetches articles from Hashnode and saves them as markdown files
"""
import os
import requests
import json
from datetime import datetime
from pathlib import Path
import re
# Configuration
HASHNODE_TOKEN = os.environ.get('HASHNODE_TOKEN')
BLOG_HOST = 'fabiolauria.hashnode.dev'
# GraphQL query to fetch articles
QUERY = """
query Publication($host: String!) {
publication(host: $host) {
posts(first: 50) {
edges {
node {
id
title
slug
brief
content {
markdown
}
coverImage {
url
}
tags {
name
}
publishedAt
updatedAt
url
}
}
}
}
}
"""
def slugify(text):
"""Convert title to filename-safe slug"""
text = text.lower()
text = re.sub(r'[^\w\s-]', '', text)
text = re.sub(r'[-\s]+', '-', text)
return text.strip('-')
def fetch_articles():
"""Fetch articles from Hashnode API"""
headers = {
'Content-Type': 'application/json',
'Authorization': HASHNODE_TOKEN
}
response = requests.post(
'https://gql.hashnode.com/',
json={'query': QUERY, 'variables': {'host': BLOG_HOST}},
headers=headers
)
if response.status_code != 200:
raise Exception(f"API request failed: {response.status_code} - {response.text}")
data = response.json()
if 'errors' in data:
raise Exception(f"GraphQL errors: {data['errors']}")
return data['data']['publication']['posts']['edges']
def save_article(article_node):
"""Save article as markdown file"""
article = article_node['node']
# Parse published date
published_date = datetime.fromisoformat(article['publishedAt'].replace('Z', '+00:00'))
year = published_date.strftime('%Y')
month = published_date.strftime('%m')
# Create directory structure: articles/YYYY/MM/
article_dir = Path(f'articles/{year}/{month}')
article_dir.mkdir(parents=True, exist_ok=True)
# Generate filename
slug = article['slug']
filename = article_dir / f"{slug}.md"
# Prepare frontmatter
tags = [tag['name'] for tag in article['tags']] if article['tags'] else []
cover_image = article['coverImage']['url'] if article['coverImage'] else ''
frontmatter = f"""---
title: "{article['title']}"
slug: "{slug}"
publishedAt: {article['publishedAt']}
updatedAt: {article['updatedAt']}
canonical: {article['url']}
tags: {', '.join(tags)}
coverImage: {cover_image}
brief: "{article['brief']}"
---
"""
# Combine frontmatter and content
content = frontmatter + article['content']['markdown']
# Write to file
with open(filename, 'w', encoding='utf-8') as f:
f.write(content)
print(f"✓ Saved: {filename}")
return filename
def main():
"""Main sync function"""
if not HASHNODE_TOKEN:
raise Exception("HASHNODE_TOKEN environment variable not set")
print(f"🔄 Syncing articles from {BLOG_HOST}...")
try:
articles = fetch_articles()
print(f"📚 Found {len(articles)} articles")
saved_count = 0
for article_edge in articles:
try:
save_article(article_edge)
saved_count += 1
except Exception as e:
print(f"⚠️ Error saving article: {e}")
print(f"\n✅ Sync complete! Saved {saved_count}/{len(articles)} articles")
except Exception as e:
print(f"❌ Sync failed: {e}")
exit(1)
if __name__ == '__main__':
main()