~jaapz-b/t00mblr/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python

#	t00mblr - a GTK tumblr client
#	By: Jaap Broekhuizen <jaapz.b@gmail.com>
#	Website: blog.jaapz.nl
#	
#	License:
#	This file is part of t00mblr.
#
#   t00mblr is free software: you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation, either version 3 of the License, or
#   (at your option) any later version.
#
#   t00mblr is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with t00mblr.  If not, see <http://www.gnu.org/licenses/>.

'''This renders the tumblr api xml output'''

import urllib
import Image
from lxml import etree
from string import Template

from common import d
import common as c

class ConnectionException():
	pass
	
class NoAuthenticationException():
	pass

class renderer():
	blog = None
	xml = None
	template = "./templates/main.tpl"
	wait = "./templates/wait.tpl"
	error_template = "./templates/error.tpl"
	api = None
	backup_file = "./blog.save"
	
	def __init__(self, blog, api):
		"""
			Renderer main init
		"""
		self.blog = blog
		self.api = api
		
	def get_blog(self):
		"""
			Retrieves the blog xml from the tumblr api
		"""
		if self.api.check_auth():
			try:
				d.debug("Downloading stream xml")
				stream = urllib.urlopen("http://www.tumblr.com/api/dashboard?email="+c.get_username()+"&password="+c.get_password())
				self.xml = stream.read()
				self.tumblr = etree.fromstring(self.xml)
			except Exception, e:
				d.error("Failed retrieving the xml")
				raise ConnectionException
		else:
			raise NoAuthenticationException
			
	def parse_blog(self):
		"""
			Parses the xml into webkit-viewable html
		"""
		self.get_blog()
		
		backup = self.open_backup()
		if backup != self.xml:
			posts = self.tumblr[1]
			caption = ""
			content = ""
			
			for post in posts:
				text = post[0].text
				#poster = self.get_poster(post)
				type = self.get_type(post)
				media = self.get_media(post)
				
				if type == "photo":
					width = c.get_size("w")
					image_width = self.get_width(post)
					image_height = self.get_height(post)
					
					if image_width >= width:
						width, height = self.calculate_image_height(image_width-40.00, image_height-40.00, width-40.00)
					else:
						height = image_height
						width = image_width
					
					media = '<img src="' + media + '" height="'+str(height)+'" width="' +str(width)+ '"/>'
					content += "<div class=\"post\">" + media + text + "</div>"
				elif type == "audio":
					content += "<div class=\"post\">" + media + text + "</div>"
				elif type == "quote":
					content += "<div class=\"post\">" + text +  "<p><strong>" + media + "</strong></p></div>"
				elif type == "regular":
					content += "<div class=\"post\">"+ text +"</div>"
			
			replaced = self.fill_template(content)
			self.save_backup(self.xml)
			
			return replaced
		else:
			d.debug("Stream is the same")
			return False
		
		"""
			TODO:
				loop through text list - DONE
				print captions into divs - DONE
				get names by posts
				get images by captions  - DONE
		"""
		
	def get_media(self, source):
		"""
			Gets the photo, video or audio posted
		"""
		return source[1].text
		
	def get_type(self, source):
		"""
			Gets user name of the poster
		"""
		return source.items()[3][1]
		
	def get_width(self, source):
		"""
			Gets image width from feed
		"""
		return int(source.items()[10][1])		
		
	def get_height(self, source):
		"""
			Gets image height from feed
		"""
		return int(source.items()[11][1])
		
	def fill_template(self, content):
		"""
			Create the template with variables
		"""
		c.get_config()
		bg = self.convert_hex(c.config.get("Theme","background"))
		fg = self.convert_hex(c.config.get("Theme","foreground"))
		fc = self.convert_hex(c.config.get("Theme","font"))
		br = self.convert_hex(c.config.get("Theme","border"))
		bs = c.config.get("Theme","border-size")
		
		file = open(self.template, 'r')
		html = Template(file.read())
		
		return html.substitute(cont=content, background=bg, foreground=fg, font=fc, border=br, bordersize=bs)
		
	def get_error_template(self):
		file = open(self.error_template, 'r')
		return file.read()
		
	def get_noauth_template(self):
		file = open(self.wait, 'r')
		return file.read()
						
	def convert_hex(self, colors):
		"""
			Converts 48-bit hex to 24-bit hex, in the poor mans way
		"""
		if len(colors) > 4:
			return colors[0:3] + colors[5:7] + colors[9:11]
		else:
			return colors
			
	def calculate_image_height(self, width, height, newwidth):
		"""
			Calculates new image height
		"""
		i = newwidth/width
		return newwidth, i*height
		
	def save_backup(self, blog):
		"""
			Saves the blog to a file
		"""
		file = open(self.backup_file, 'w')
		file.writelines(blog)
		file.close()
		
	def open_backup(self):
		"""
			Open the backup file
		"""
		file = open(self.backup_file, 'r')
		return file.read()