-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
168 lines (134 loc) · 4.76 KB
/
app.py
File metadata and controls
168 lines (134 loc) · 4.76 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
import re
import requests
import markdown
import datetime
from flask import Flask, render_template, jsonify
from waitress import serve
app = Flask(__name__)
GITHUB_REPO = "IMYdev/blog_posts"
GITHUB_API_URL = f"https://api.github.com/repos/{GITHUB_REPO}/contents"
RAW_BASE_URL = f"https://raw.githubusercontent.com/{GITHUB_REPO}/main"
MONTHS = {
'01': 'January', '02': 'February', '03': 'March', '04': 'April',
'05': 'May', '06': 'June', '07': 'July', '08': 'August',
'09': 'September', '10': 'October', '11': 'November', '12': 'December'
}
def parse_date_info(date_str):
if not date_str:
return datetime.date.min, 'Unknown Date'
try:
parts = date_str.strip().split('/')
if len(parts) != 3:
raise ValueError("Invalid date format")
day, month, year = parts
if len(year) == 2:
year = '20' + year
day_num = int(day)
month_num = int(month)
year_num = int(year)
month_name = MONTHS.get(month.zfill(2), None)
if not month_name:
raise ValueError("Invalid month")
formatted_date = f"{month_name} {day_num}, {year_num}"
sortable_date = datetime.date(year_num, month_num, day_num)
return sortable_date, formatted_date
except Exception as e:
print(f"Error parsing date '{date_str}': {e}")
return datetime.date.min, 'Unknown Date'
def parse_post(filename, content):
all_lines = content.split('\n')
metadata = {}
content_lines = []
in_header = True
for line in all_lines:
if in_header:
if line.strip() == "":
in_header = False
elif ':' in line:
key, value = line.split(':', 1)
metadata[key.strip().lower()] = value.strip()
else:
content_lines.append(line)
title = metadata.get('title', 'Untitled Post')
date_str = metadata.get('date', None)
sortable_date, display_date = parse_date_info(date_str)
slug = filename.replace('.md', '')
content_to_render = '\n'.join(content_lines)
html_content = markdown.markdown(content_to_render)
clean_text = re.sub('<[^<]+?>', '', html_content)
preview = clean_text[:150] + '...' if len(clean_text) > 150 else clean_text
return {
'title': title,
'filename': filename,
'slug': slug,
'date': display_date,
'sort_date': sortable_date,
'preview': preview,
'content': html_content
}
def get_posts():
try:
res = requests.get(GITHUB_API_URL)
res.raise_for_status()
files = res.json()
except Exception as e:
print("Error fetching posts:", e)
return []
posts = []
for file in files:
if file['name'].endswith('.md'):
raw_url = f"{RAW_BASE_URL}/{file['name']}"
try:
content = requests.get(raw_url).text
if content.strip() != "404: Not Found":
post_data = parse_post(file['name'], content)
posts.append(post_data)
except Exception as e:
print(f"Error fetching {file['name']}: {e}")
posts.sort(key=lambda x: x['sort_date'], reverse=True)
return posts
def render_post(filename):
raw_url = f"{RAW_BASE_URL}/{filename}"
try:
res = requests.get(raw_url)
res.raise_for_status()
content = res.text
return parse_post(filename, content)
except requests.exceptions.HTTPError as e:
print(f"Post not found {filename}: {e}")
return None
except Exception as e:
print(f"Error fetching post {filename}: {e}")
return None
@app.route('/')
def index():
posts = get_posts()
return render_template('index.html', posts=posts)
@app.route('/post/<slug>')
def post(slug):
filename = slug + '.md'
post_data = render_post(filename)
if post_data is None:
return "Post not found", 404
return render_template('post.html', post=post_data)
@app.route('/api/posts')
def api_posts():
posts = get_posts()
json_posts = []
for p in posts:
post_copy = p.copy()
if isinstance(post_copy['sort_date'], datetime.date):
post_copy['sort_date'] = post_copy['sort_date'].isoformat()
json_posts.append(post_copy)
return jsonify(json_posts)
@app.route('/api/post/<slug>')
def api_post_detail(slug):
filename = slug + '.md'
post = render_post(filename)
if post is None:
return jsonify({"error": "Post not found"}), 404
if isinstance(post['sort_date'], datetime.date):
post['sort_date'] = post['sort_date'].isoformat()
return jsonify(post)
if __name__ == '__main__':
serve(app)