Skip to content

Instantly share code, notes, and snippets.

@segphault
Created May 26, 2009 21:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save segphault/118298 to your computer and use it in GitHub Desktop.
Save segphault/118298 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import SimpleHTTPServer
from gwibber import microblog, config
from mako.template import Template
PORT = 10002
def generate_html(messages):
return Template("""
<html>
<head>
<style>
.imgbox {
width: 48px;
height: 48px;
margin-right: 5px;
background-repeat: no-repeat;
}
.message { border-bottom: 1px solid black; }
.title { font-weight: bold; }
.time { font-size: x-small; }
body { margin: 0px; padding: 0px; }
a:link { color: #00224F; }
</style>
</head>
<body>
% for m in Gwibber:
<div style="background: #${m.hexbg}" class="message" title="${m.sender_nick}">
<table>
<tr><td>
<img width="48" height="48" src="${m.image.replace("https", "http")}" />
</td><td>
<p class="content">
<span class="title">${hasattr(m, "title") and m.title or m.sender}</span>
<span class="time"> (<a href="${m.url}">${m.time_string}</a>)</span><br />
<span class="text">${m.html_string.encode("ascii", "xmlcharrefreplace")}</span>
</p>
</td></tr>
</table>
</div>
%endfor
<body>
</html>
""").render(Gwibber=messages)
class GwibberClient(microblog.Client):
def post_process_message(self, message):
message.hexbg = message.account[message.bgcolor][1:3] + message.account[message.bgcolor][5:7] + message.account[message.bgcolor][9:11]
message.time_string = microblog.support.generate_time_string(message.time)
if not hasattr(message, "html_string"):
message.html_string = '<span class="text">%s</span>' % \
microblog.support.LINK_PARSE.sub('<a href="\\1">\\1</a>', message.text)
return message
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
if self.path == "/Messages/Mobile":
c = GwibberClient(config.Accounts())
messages = list(c.receive())
self.wfile.write(generate_html(messages))
else: self.send_error(404, "Command not recognized")
httpd = SimpleHTTPServer.BaseHTTPServer.HTTPServer(('', PORT), Handler)
httpd.allow_reuse_address = True
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment