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