-
Notifications
You must be signed in to change notification settings - Fork 0
/
dropblog.py
146 lines (124 loc) · 3.57 KB
/
dropblog.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
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
# all the imports
import sqlite3
import urllib
from flask import Flask, request, session, g, redirect, url_for, \
abort, render_template, flash
from contextlib import closing
import os
import markdown2
# configuration
DATABASE = '/tmp/flaskr.db'
DEBUG = True
SECRET_KEY = 'development key'
USERNAME = 'admin'
PASSWORD = 'default'
# create our little application :)
app = Flask(__name__)
app.config.from_object(__name__)
def connect_db():
return sqlite3.connect(app.config['DATABASE'])
def init_db():
with closing(connect_db()) as db:
with app.open_resource('schema.sql', mode='r') as f:
db.cursor().executescript(f.read())
db.commit()
@app.before_request
def before_request():
g.db = connect_db()
@app.teardown_request
def teardown_request(exception):
db = getattr(g, 'db', None)
if db is not None:
db.close()
@app.route('/')
def show_entries():
blogPosts = urllib.urlopen('https://dl.dropboxusercontent.com/u/9366248/blog/blogPosts')
baseurl = 'https://dl.dropboxusercontent.com/u/9366248/blog/'
titles = []
fileNames = []
postText = []
postURL = []
dates = []
for line in blogPosts:
if not line.startswith('#'):
postURL.append(baseurl + line.strip())
fileNames.append('/post/' + line.strip())
postURL = postURL[::-1]
fileNames = fileNames[::-1]
blogPosts.close()
for i in postURL:
post = urllib.urlopen(i)
result = ""
title = i[49:]
title = os.path.splitext(title)[0]
date = ""
for line in post:
if line.startswith("<title>"):
title = line[7:]
elif line.startswith("<date>"):
date = line[6:]
else:
result += line + "\n"
titles.append(title)
dates.append(date)
result = markdown2.markdown(result)
postText.append(result)
post.close()
entries = [dict(title=titles[i], text=postText[i], url=fileNames[i], date=dates[i]) for i in range(len(titles))]
return render_template('show_entries.html', entries=entries)
@app.route('/post_list')
def show_list():
blogPosts = urllib.urlopen('https://dl.dropboxusercontent.com/u/9366248/blog/blogPosts')
baseurl = 'https://dl.dropboxusercontent.com/u/9366248/blog/'
titles = []
postText = []
postURL = []
fileNames = []
for line in blogPosts:
if not line.startswith('#'):
postURL.append(baseurl + line.strip())
fileNames.append('/post/' + line.strip())
postURL = postURL[::-1]
fileNames = fileNames[::-1]
blogPosts.close()
for i in postURL:
post = urllib.urlopen(i)
result = ""
title = i[49:]
title = os.path.splitext(title)[0]
for line in post:
if line.startswith("<title>"):
title = line[7:]
titles.append(title)
post.close()
entries = [dict(title=titles[i], fileName=fileNames[i]) for i in range(len(titles))]
return render_template('post_list.html', entries=entries)
@app.route('/post/<postTitle>')
def single_post(postTitle):
baseurl = 'https://dl.dropboxusercontent.com/u/9366248/blog/'
post = urllib.urlopen(baseurl + postTitle)
title = postTitle
title = os.path.splitext(title)[0]
body = ""
date = ""
for line in post:
if line.startswith("<title>"):
title = line[7:]
elif line.startswith("<date>"):
date = line[6:]
else:
body += line + "\n"
body = markdown2.markdown(body)
return render_template('single_post.html', title=title, body=body, date=date)
@app.route('/about')
def about():
url = 'https://dl.dropboxusercontent.com/u/9366248/blog/about.md'
content = urllib.urlopen(url)
title = "About"
body = ""
for line in content:
body += line + "\n"
body = markdown2.markdown(body)
return render_template('single_post.html', title=title, body=body)
if __name__ == '__main__':
app.run()