Merge branch 'syntax-highlighting' into dev
[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/dylansserver.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[:1] == '.': continue
29 if note == 'index.php' or note == 'notes.php': continue
30 url = note[:note.index('.')]
31 f = open(os.path.join(NOTES_DIRECTORY, note))
32 title = str(f.readline()[:-1])
33 date_posted = time.strptime(str(f.readline()[:-1]), "%Y/%m/%d %I:%M%p")
34 text = ''.join(f.readlines()) #converts list to single string
35 if title in existing_titles: continue
36 sql = "INSERT INTO notes (date_posted, url, title, text)\
37 VALUES(\"%s\", \"%s\", \"%s\", \"%s\")"\
38 % (time.strftime("%Y/%m/%d %I:%M:00", date_posted), url, title, db.escape_string(text))
39
40 #print sql
41 cursor.execute(sql)