constance

Scripts for generating (an earlier obsolete version of) my personal web site
git clone https://code.djc.id.au/git/constance/
commit 3dceddaca7a67502e90b4f5c5d4d04648ceb17cd
parent ae8b2f0bdde853f797bae980475792ae419737a2
Author: Dan Callaghan <djc@djc.id.au>
Date:   Sat,  7 Jun 2008 23:19:43 +1000

first go at reading log

committer: Dan Callaghan <djc@djc.id.au>

--HG--
extra : convert_revision : 153d577bb18afdbd7cdd9e34ef6366ba4d4680ab

Diffstat:
Mapp.py | 3++-
Mblog.py | 31+++++++++++++++++++++++++++----
Aexport_readinglog_wp.py | 28++++++++++++++++++++++++++++
Mexport_wp.py | 3+++
Astatic/images/book_open.png | 0
Astatic/images/star.png | 0
Mtemplates/_entry.xml | 26+++++++++++++++++++++++++-
7 files changed, 85 insertions(+), 6 deletions(-)
diff --git a/app.py b/app.py
@@ -7,6 +7,7 @@ from colubrid.server import StaticExports
 import blog
 
 ENTRIES_DIR = os.path.join(os.path.dirname(__file__), u'entries')
+READINGLOG_FILE = os.path.join(os.path.dirname(__file__), u'reading_log')
 BASE_URL = ''
 
 template_loader = TemplateLoader(
@@ -25,7 +26,7 @@ class BlogApplication(RegexApplication):
 
 	def __init__(self, *args, **kwargs):
 		super(BlogApplication, self).__init__(*args, **kwargs)
-		self.entries = blog.Entries(ENTRIES_DIR)
+		self.entries = blog.Entries(ENTRIES_DIR, READINGLOG_FILE)
 
 	def index(self):
 		rendered = template_loader.load('index.xml').generate(
diff --git a/blog.py b/blog.py
@@ -1,6 +1,8 @@
 import os
 from datetime import datetime
+from itertools import chain
 import markdown
+import yaml
 
 
 def count(iterable):
@@ -30,19 +32,25 @@ class CommentForbiddenError(ValueError): pass
 
 class Entries(object):
 
-	def __init__(self, entries_dir):
+	def __init__(self, entries_dir, readinglog_file):
 		self.entries_dir = entries_dir
+		self.readinglog_file = readinglog_file
 	
 	def __contains__(self, id):
 		return os.path.exists(os.path.join(self.entries_dir, id))
 	
 	def __getitem__(self, id):
+		# XXX reading log entries don't have a key
 		return Entry(self.entries_dir, id)
 	
 	def __iter__(self):
-		return (Entry(self.entries_dir, filename) 
-				for filename in os.listdir(self.entries_dir)
-				if not filename.startswith('.'))
+		return chain(
+				(Entry(self.entries_dir, filename) 
+				 for filename in os.listdir(self.entries_dir)
+				 if not filename.startswith('.')), 
+				(ReadingLogEntry(d)
+				 for d in yaml.load_all(open(self.readinglog_file, 'r')))
+				)
 
 	def by_category(self):
 		"""
@@ -116,6 +124,21 @@ class Entry(object):
 				os.access(self.comments_dir, os.R_OK)
 
 
+class ReadingLogEntry(object):
+
+	def __init__(self, yaml_dict):
+		self.title = yaml_dict['Title']
+		self.author = yaml_dict['Author']
+		self.publication_date = self.modified_date = self.date = yaml_dict['Date']
+		self.url = yaml_dict.get('URL', None)
+		self.rating = yaml_dict.get('Rating', None)
+		self.categories = frozenset([u'Reading'])
+		self.tags = frozenset()
+
+	def has_comments(self):
+		return False
+
+
 class Comments(object):
 
 	def __init__(self, path):
diff --git a/export_readinglog_wp.py b/export_readinglog_wp.py
@@ -0,0 +1,28 @@
+import os, time, re, urllib, uuid
+import MySQLdb
+import yaml
+
+def export(f):
+	log_entries = []
+
+	cn = MySQLdb.connect(host='cruz', user='root', passwd='ELIDED', db='wordpress', use_unicode=True)
+
+	cur = cn.cursor()
+	cur.execute('SELECT id, post_title, post_date FROM wp_posts INNER JOIN wp_term_relationships ON wp_term_relationships.object_id = wp_posts.id WHERE post_status = %s AND term_taxonomy_id = %s ORDER BY post_date ASC', ('publish', 14))
+	for row in cur.fetchall():
+		id, title, date = row
+		entry = {'Title': title, 'Date': date}
+		subcur = cn.cursor()
+		subcur.execute('SELECT meta_key, meta_value FROM wp_postmeta WHERE post_id = %s', (id,))
+		for key, value in subcur.fetchall():
+			if key == '_readinglog_url': entry['URL'] = value
+			elif key == '_readinglog_author': entry['Author'] = value
+			elif key == '_readinglog_rating': entry['Rating'] = float(value)
+		log_entries.append(entry)
+	
+	yaml.add_representer(unicode, lambda dumper, value: dumper.represent_scalar(u'tag:yaml.org,2002:str', value))
+	yaml.dump_all(log_entries, f, default_flow_style=False, allow_unicode=True)
+
+if __name__ == '__main__':
+	import sys
+	export(sys.stdout)
diff --git a/export_wp.py b/export_wp.py
@@ -29,6 +29,9 @@ def export(base_dir):
 		subcur.execute('SELECT wp_terms.name FROM wp_term_relationships INNER JOIN wp_term_taxonomy ON wp_term_relationships.term_taxonomy_id = wp_term_taxonomy.term_taxonomy_id INNER JOIN wp_terms ON wp_term_taxonomy.term_id = wp_terms.term_id WHERE taxonomy = %s AND object_id = %s', ('post_tag', id,))
 		tags = [tag for tag, in subcur.fetchall()]
 
+		# XXX
+		if 'Reading' in categories: continue
+
 		os.mkdir(os.path.join(base_dir, post_name))
 		f = open(os.path.join(base_dir, post_name, 'content.txt'), 'w')
 		f.write('Title: %s\n' % post_title)
diff --git a/static/images/book_open.png b/static/images/book_open.png
Binary files differ.
diff --git a/static/images/star.png b/static/images/star.png
Binary files differ.
diff --git a/templates/_entry.xml b/templates/_entry.xml
@@ -4,7 +4,7 @@
 	 py:strip="True"
 	 py:def="show_entry(entry, show_comments=True)">
 
-<div class="entry">
+<div class="entry" py:if="'Reading' not in entry.categories">
 
 	<h3 class="entrytitle" id="post-${entry.id}"><a href="${BASE_URL}/${entry.id}" rel="bookmark">${entry.title}</a></h3>
 
@@ -32,6 +32,30 @@
 
 </div>
 
+<span py:def="stars(rating)" py:strip="True">
+	<img src="${BASE_URL}/static/images/star.png" alt="[star]" py:for="_ in range(int(rating))" />
+	<img src="${BASE_URL}/static/images/halfstar.png" alt="[half-star]" py:if="rating > int(rating)" />
+</span>
+
+<div class="entry readinglog" py:if="'Reading' in entry.categories">
+	<h3 class="entrytitle">
+		<a py:strip="not entry.url" href="${entry.url}">${entry.title}</a>
+		<py:if test="entry.author">
+			by ${entry.author}
+		</py:if>
+	</h3>
+	<div class="entrymeta">
+		Posted ${entry.publication_date.strftime(str('%-1d %B %Y'))}
+		<py:if test="entry.categories">
+			in <a py:for="category in entry.categories" href="${BASE_URL}/+categories/${category}">${category}</a>
+		</py:if>
+		<py:if test="entry.rating">
+			ยท ${stars(entry.rating)}
+		</py:if>
+	</div>
+	<div class="entrybody"></div>
+</div>
+
 <div id="commentblock"
 	 py:if="show_comments and entry.has_comments()"
 	 py:with="comments = sorted(entry.comments(), key=lambda c: c.date)">