~marnanel/blog-wrangler/trunk

15 by Thomas Thurman
for real now
1
import re
2
import urllib
3
import ConfigParser
4
5
def _thumbnails(cp, package):
6
	r = urllib.urlopen(cp.get(package, 'pictures')).read() # will really be from the referenced url
7
	ini = {}
8
	jpg = {}
9
10
	for href in re.findall('href="([^"]*)', r):
11
		if href.endswith('.ini'):
12
			ini[href[:-4]] = 1
13
		if href.endswith('.jpg'):
14
			jpg[href[:-4]] = 1
15
16
	return filter(jpg.has_key, sorted(ini.keys()))
17
18
def next(cp, package):
19
	candidate = None
20
21
	for thumbnail in _thumbnails(cp, package):
22
		if not cp.has_option(package+'-pictures', thumbnail):
23
			candidate = thumbnail
24
			break
25
26
	if candidate:
27
		url = cp.get(package, 'pictures') + candidate
28
29
		result = {}
30
31
		metadata = ConfigParser.ConfigParser()
32
		metadata.readfp(urllib.urlopen(url+'.ini'))
33
		for (key, value) in metadata.items('picture'):
34
			result[key] = value
35
36
		result['picture'] = url+'.jpg'
37
38
		# and mark it so it doesn't get reused
39
		# (you will have to write the cp out again
40
		# if you want this to stick)
41
		section = package+'-pictures'
42
43
		if not cp.has_section(section):
44
			cp.add_section(section)
45
46
		cp.set(section, candidate, 1)
47
48
		return result
49
	else:
50
		print 'WARNING: Out of pictures. Returning crummy defaults.'
51
		return {
52
			'picture': 'http://www.gnome.org/~tthurman/pics/error.jpg',
53
			'width': 15, 'height': 13,
54
			'title': 'Out of pictures-- operator must resolve',
55
			'target': 'about:blank',
56
			}
57
58
# eof