#!/usr/bin/python

import MySQLdb
import sys
import markdown

# read from file
if (len(sys.argv) != 2 or sys.argv[1] == '-h'):
   print "usage: blog [filename]"
   sys.exit(1)

text_file = open(sys.argv[1], 'r')

# Parsing
title = text_file.readline().rstrip()

# This new way of reading in text. It continues reading 
body = markdown.markdown(text_file.read())
body = body.replace('"', '\\"')

# This is the old way of getting getting the body of the post
# body = text_file.read().replace('\n\n', "</p><p>")

# connect to database
# enter login information
db = MySQLdb.connect("","","", "")

cursor = db.cursor()

cursor.execute('INSERT INTO posts (title, body, datePosted) VALUES ("{}", "{}", DATE(DATE_SUB(NOW(),INTERVAL 8 HOUR)))'.format(title, body))

# Clean up
cursor.close()
db.commit()
db.close()
