forked from LiminalCrab/auger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateHTML.py
47 lines (39 loc) · 1.19 KB
/
createHTML.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
from jinja2 import Environment, FileSystemLoader
import psycopg2
import os
import json
def loadTemplate():
templates_dir = os.path.join(os.getcwd(), 'templates')
env = Environment(loader = FileSystemLoader(templates_dir))
template = env.get_template('template')
return template
def loadData():
# move this into a psycopg2 module?
#open initial connection
conn = psycopg2.connect("")
#open initial cursor
cur = conn.cursor()
ORDER_BY_DATE_TO_JSON = '''
SELECT
json_build_object(
'id', posts.id,
'title', posts.host_title,
'url', posts.post_url,
'date', posts.post_date
) FROM posts ORDER BY post_date DESC;
'''
print("ORDER.PY: SORTING DATES")
cur.execute(ORDER_BY_DATE_TO_JSON)
data = list(map(lambda x: x[0], cur.fetchall()))
cur.close()
conn.close()
print(data)
return data
def makeHTML(template, data):
filename = os.path.join(os.getcwd(), 'index.html')
with open(filename, 'w+') as fw:
fw.write(template.render(data=data))
def main():
makeHTML(loadTemplate(), loadData())
if __name__ == '__main__':
main()