If it won't be simple, it simply won't be. [source code] by Miki Tebeka, CEO, 353Solutions

Friday, June 12, 2009

A Twitter Trends / Google News meshup

This tried to automatically explain why things are trending on Twitter.

The Python Server (trends.py)

#!/usr/bin/env python
'''A twitter trends/google news mesh

loader_thread get news trends every 1min and set _TRENDS_HTML
The web server serves _TRENDS_HTML with is refresed via JavaScript every 30sec

from urllib import urlopen, urlencode
import feedparser
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
from os.path import dirname, join, splitext
import json
from threading import Thread
from time import sleep

def ascii_clean(text):
return text.encode("ascii", "ignore") # Pure ACII

def trend_news(trend):
query = {
"q" : ascii_clean(trend),
"output" : "rss"
url = "http://news.google.com/news?" + urlencode(query)
return feedparser.parse(url).entries

def current_trends():
url = "http://search.twitter.com/trends.json"
return json.load(urlopen(url))["trends"]

def news_html(news):
html = '<li><a href="%(link)s">%(title)s</a></li>'
chunks = map(lambda e: html % e, news)
return "\n".join(["<ul>"] + chunks + ["</ul>"])

def trend_html(trend):
thtml = '<a href="%(url)s">%(name)s' % trend
nhtml = news_html(trend_news(trend["name"]))
return '<tr><td>%s</td><td>%s</td><tr>' % (thtml, nhtml)

def table_html(trends):
return ("<table>" +
"<tr><th>Trend</th><th>Related News</th></tr>" +
"".join(map(trend_html, trends)) +

def loader_thread():

while 1:
_TRENDS_HTML = ascii_clean(table_html(current_trends()))

def run_loader_thread():
t = Thread(target=loader_thread)
t.daemon = 1

def trends_html():
while not _TRENDS_HTML:


def index_html():
return open(join(dirname(__file__), "index.html")).read()

class RequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == "/":
elif self.path.startswith("/trends"):
elif splitext(self.path)[1] in (".js", ".css"):
self.wfile.write(open(".%s" % self.path).read())
self.send_error(404, "Not Found")

if __name__ == "__main__":
server = HTTPServer(("", 8888), RequestHandler)

The Auto-Refreshing Web Client (index.html)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<link rel="stylesheet" href="trends.css" />
body {
font-family: Serif, Helvetica;

table {
width: 100%;
border: 2px solid gray;

td {
vertical-align: top;
border-bottom: 1px solid black;

h1 {
text-align: center;
font-variant: small-caps;

a {
text-decoration: none;
color: black;

a:hover {
background: silver;

ul {
margin: 0px;
<div id="trends">
Loading ...
<script src="jquery.js"></script>
function load() {
$(document).ready(function() {
setInterval(load, 30 * 1000);
Post a Comment

Blog Archive