homepage/scripts/compileposts.py

50 lines
1.5 KiB
Python

#!/usr/bin/env python
import os;
import json;
import time;
# each folder in the post folder is a type of post
base = '../public/assets/post'
posts = os.listdir(base)
outputfile = '../public/assets/posts.json'
# build ourselves a json object
jsonresult = []
# each folder in the current one is a post! the name of the folder is the _id_
for post in posts:
print('doing post '+post)
postdir = base+"/"+post
infofile = postdir+"/info.json"
print(infofile)
with open(infofile) as j:
tt = j.read()
d = json.loads(tt)
temp = {}
temp['id'] = post
temp['info'] = d
# have to write the 'modified date' in the info.json of the post if it's not there yet
if 'created_timestamp' not in d:
d['created_timestamp'] = time.time()
print('created new timestamp: '+str(time.time()))
# persist it in the json file itself
with open(infofile, 'w') as infooutfile:
json.dump(d, infooutfile, sort_keys=True, indent=4, separators=(',', ': '))
# # figure out the last change of any file in the folder
# lastchange = 0;
# contents = os.listdir(postdir);
# for contentfile in contents:
# lastchange = max(lastchange,os.path.getmtime(postdir+"/"+contentfile))
# temp['lastmodified'] = lastchange;
jsonresult.append(temp)
# completely append the info.json file
# print the result of the json parse
print(json.dumps(jsonresult))
with open(outputfile, 'w') as outfile:
json.dump(jsonresult, outfile)