2f99dc61e5404df72c54f5c0493038bf0d2b731a
[dylansserver.git] / publish_notes.py
1 #!/usr/bin/python
2
3 NOTES_DIRECTORY = '/home/dylan/docs/notes'
4
5 import os
6 import time
7 import MySQLdb as db
8 import ConfigParser
9
10 config = ConfigParser.RawConfigParser()
11 config.read('/etc/dylanstestserver.ini')
12 domain = config.get('database', 'domain')
13 user = config.get('database', 'user')
14 password = config.get('database', 'password').replace('"', '')
15 database = config.get('database', 'database')
16 cursor = db.connect(domain, user, password, database).cursor()
17
18 notes = os.listdir(NOTES_DIRECTORY)
19
20 sql = "SELECT title FROM notes"
21 cursor.execute(sql)
22 results = cursor.fetchall()
23 existing_titles = []
24 for row in results:
25 existing_titles.append(row[0])
26
27 for note in notes:
28 if note == 'index.php' or note == 'notes.php': continue
29 url = note[:note.index('.')]
30 f = open(os.path.join(NOTES_DIRECTORY, note))
31 title = str(f.readline()[:-1])
32 date_posted = time.strptime(str(f.readline()[:-1]), "%Y/%m/%d %I:%M%p")
33 text = ''.join(f.readlines()) #converts list to single string
34 if title in existing_titles: continue
35 sql = "INSERT INTO notes (date_posted, url, title, text)\
36 VALUES(\"%s\", \"%s\", \"%s\", \"%s\")"\
37 % (time.strftime("%Y/%m/%d %I:%M:00", date_posted), url, title, db.escape_string(text))
38
39 #print sql
40 cursor.execute(sql)