notes/ now working!
[dylansserver.git] / publish.py
1 #!/usr/bin/python
2
3 import os
4 import time
5 import MySQLdb
6
7 db = MySQLdb.connect('localhost','dylan','password', 'dylanstestserver')
8 cursor = db.cursor()
9
10 notes = os.listdir('notes')
11
12 sql = "SELECT title FROM notes"
13 cursor.execute(sql)
14 results = cursor.fetchall()
15 existing_titles = []
16 for row in results:
17 existing_titles.append(row[0])
18
19 for note in notes:
20 if note == 'index.php' or note == 'notes.php': continue
21 url = note[:note.index('.')]
22 f = open(os.path.join('notes', note))
23 title = str(f.readline()[:-1])
24 text = ''.join(f.readlines()) #converts list to single string
25 if title in existing_titles: continue
26 mtime = time.localtime(os.path.getmtime(os.path.join('notes', note)))
27 date_posted = "%s-%s-%s" % (str(mtime.tm_year)[2:], mtime.tm_mon, mtime.tm_mday)
28 sql = "INSERT INTO notes (date_posted, url, title, text)\
29 VALUES(\"%s\", \"%s\", \"%s\", \"%s\")"\
30 % (date_posted, url, title, MySQLdb.escape_string(text))
31
32 #print sql
33 cursor.execute(sql)