import os
import json
import dicttoxml
import datetime
from xml.dom.minidom import parseString

SITE = "http://yangshun.github.io/luna/#/"

POSTS_DIR_PATH = "./posts/"
POSTS_JSON_FILE_PATH = "./content/posts.json"
POSTS_RSS_FILE_PATH = "./content/posts.rss"

def generate_json(json_output):

	json_output = sorted(json_output, key=lambda post: post['timestamp'], reverse=True)
	print(json.dumps(json_output, None, indent=4))

	json_file = open(POSTS_JSON_FILE_PATH, 'w+')
	json_file.write(json.dumps(json_output))
	json_file.close()

	print('\nJSON output generated at \'./content/posts.json\'.')

def generate_rss(rss_output):
	for post in rss_output:
		del post['filename']
		post['guid'] = post['post_id']
		post['link'] = SITE + post['guid']
		del post['post_id']
		post['description'] = post['content']
		del post['content']
		post['pubDate'] = datetime.date.fromtimestamp(post['timestamp'])
		del post['timestamp']
		del post['modified']

	rss_file = open(POSTS_RSS_FILE_PATH, 'w+')
	rss = {'rss':{'channel': rss_output}}
	xml = dicttoxml.dicttoxml(rss)
	dom = parseString(xml)
	prettyxml = dom.toprettyxml()
	print(prettyxml)
	rss_file.write(prettyxml)
	rss_file.close()

	print('\nRSS file generated at \'./content/posts.rss\'.\n')

def build_posts():
	file_paths = os.listdir(POSTS_DIR_PATH)

	data_output = []

	for file_path in file_paths:
	    current_file_path = POSTS_DIR_PATH + file_path
	    with open(current_file_path) as f:
	        post_title = f.readline()
	        throw_away = f.readline()   # second line of post is markdown heading syntax
	        file_content = f.read()

	    post = {
	            'title': post_title.replace('\n', ''),
	            'timestamp': int(os.path.getctime(current_file_path)),
	            'modified': int(os.path.getmtime(current_file_path)),
	            'content': file_content.strip(),
	            'filename': file_path,
	            'post_id': post_title.replace(' ', '-').lower().strip() # TODO: Make sure there are no duplicate ids
	            }

	    data_output.append(post)

	generate_json(data_output)
	generate_rss(data_output)


build_posts()
@@ -1051,16 +1051,28 @@
 oxml(rss
+, root=False
 )%0A%09dom =

remove root from XML