Skip to content
This repository has been archived by the owner on Mar 16, 2020. It is now read-only.

Commit

Permalink
more stats
Browse files Browse the repository at this point in the history
  • Loading branch information
peterbe committed Aug 30, 2011
1 parent 3306da2 commit 75c5cc2
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 36 deletions.
118 changes: 89 additions & 29 deletions handlers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from pprint import pprint
from pprint import pprint, pformat
import tornado.auth
import tornado.web
from tornado.web import HTTPError
Expand Down Expand Up @@ -31,7 +31,9 @@ def redis(self):
class HomeHandler(BaseHandler):

def get(self):
options = {}
options = {
'page_title': 'Too Cool for Me?',
}
user = self.get_current_user()
if user:
url = '/static/bookmarklet.js'
Expand Down Expand Up @@ -263,36 +265,94 @@ class FollowingHandler(BaseHandler, tornado.auth.TwitterMixin):
@tornado.web.asynchronous
def get(self, username):
options = {'username': username}
user = self.get_current_user()
if not user:
this_username = self.get_current_user()
if not this_username:
self.redirect('auth_twitter')
return
this_username = self.get_current_user()
options['this_username'] = this_username
options['follows'] = None
if this_username:
key = 'follows:%s:%s' % (this_username, username)
value = self.redis.get(key)
if value is None:
access_token = self.redis.get('username:%s' % this_username)
access_token = json_decode(access_token)
self.twitter_request(
"/friendships/show",
source_screen_name=this_username,
target_screen_name=username,
access_token=access_token,
callback=self.async_callback(
lambda x: self._on_show(x, this_username, username, options)
),
)
return
options['follows'] = bool(int(value))
self.render('following.html', **options)
key = 'follows:%s:%s' % (this_username, username)
value = self.redis.get(key)
if value is None:
access_token = self.redis.get('username:%s' % this_username)
access_token = json_decode(access_token)
self.twitter_request(
"/friendships/show",
source_screen_name=this_username,
target_screen_name=username,
access_token=access_token,
callback=self.async_callback(
lambda x: self._on_friendship(x, key, options)
),
)
else:
self._on_friendship(bool(int(value)), None, options)
#options['follows'] = bool(int(value))
#self._fetch_info(options)

def _on_show(self, result, this_username, username, options):
value = None
if result and 'relationship' in result:
value = result['relationship']['target']['following']
key = 'follows:%s:%s' % (this_username, username)
self.redis.setex(key, int(bool(value)), 60)
def _on_friendship(self, result, key, options):
if isinstance(result, bool):
value = result
else:
if result and 'relationship' in result:
value = result['relationship']['target']['following']
if key:
self.redis.setex(key, int(bool(value)), 60)
options['follows'] = value
self._fetch_info(options)

def _fetch_info(self, options, username=None):
if username is None:
username = options['username']
key = 'info:%s' % username
value = self.redis.get(key)
if value is None:
access_token = self.redis.get('username:%s' % options['this_username'])
access_token = json_decode(access_token)
self.twitter_request(
"/users/show",
screen_name=username,
access_token=access_token,
callback=self.async_callback(
lambda x: self._on_info(x, key, options)
),
)
else:
self._on_info(json_decode(value), None, options)
#value = json_decode(value)
#options['info'] = value
#pprint(value)
#self._render(options)

def _on_info(self, result, key, options):
if isinstance(result, basestring):
result = json_decode(result)
if key:
self.redis.setex(key, json_encode(result), 60 * 1)#60)
if 'info' not in options:
options['info'] = {options['username']: result}
self._fetch_info(options, username=options['this_username'])
else:
options['info'][options['this_username']] = result
self._render(options)

def _render(self, options):
if options['follows']:
page_title = '%s follows you'
else:
page_title = '%s is too cool for you'
options['page_title'] = page_title % options['username']
#options['info_print'] = pformat(options['info'])
#_followers = options['info']['followers_count']
#_following = options['info']['friends_count']
#options['ratios'][options['username']] =
self._set_ratio(options, 'username')
self._set_ratio(options, 'this_username')
self.render('following.html', **options)

def _set_ratio(self, options, key):
value = options[key]
followers = options['info'][value]['followers_count']
following = options['info'][value]['friends_count']
ratio = 1.0 * followers / following
options['info'][value]['ratio'] = '%.1f' % ratio
8 changes: 5 additions & 3 deletions templates/base.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!doctype html>
<html>
<head>
<title>{% block title %}{% end %}</title>
<title>{% block title %}{{ page_title }}{% end %}</title>
<meta charset="utf-8">
<link href='http://fonts.googleapis.com/css?family=Permanent+Marker' rel='stylesheet' type='text/css'>
<style>
Expand All @@ -16,17 +16,19 @@
h1 a { color:black; text-decoration:none; }
h1 a:hover { color:black; text-decoration:underline; }
header h1 { font-size:3.2em; }
footer { position: absolute; bottom: 0; }
/*footer { position: absolute; bottom: 0; }*/
footer { clear:left; margin-top:100px; }
footer p { text-align:center; font-family: sans-serif; color:#666; font-size:0.8em; }
.followsyou { color: green; }
.followsyounot { color: #333; }
h1 a .q { color:#999; }
</style>
{% block extra_head %}{% end %}
</head>
<body>
<header>

<h1><a href="/">Too Cool For Me</a></h1>
<h1><a href="/">Too Cool For Me<span class="q">?</span></a></h1>
{% block extra_header %}
{% end %}
</header>
Expand Down
74 changes: 70 additions & 4 deletions templates/following.html
Original file line number Diff line number Diff line change
@@ -1,21 +1,87 @@
{% extends "base.html" %}


{% block extra_head %}
<style>
table { border-collapse:collapse; }
table td { padding:8px 20px 8px 20px; }
.label { font-weight:bold; }
td.label { border-right:1px solid #999; }
th.label { border-bottom:1px solid #999; }
</style>
{% end %}

{% block content %}

<p style="text-align:center; font-size:1.2em"><strong>{{ username }}</strong>
<p style="font-size:2em">
<a href="https://twitter.com/#!{{ username }}"
><img src="http://api.twitter.com/1/users/profile_image/{{ username }}.png?size=bigger"></a>
<strong>{{ username }}</strong>
{% if follows %}<span class="followsyou">follows me!</span>
{% else %}
{% if follows is not None %}
<span class="followsyounot">too cool for me</span>
<span class="followsyounot">is too cool for me</span>
{% else %}
<em>couldn't even be looked up on Twitter</em>
{% end %}
{% end %}
</p>

<p style="font-size:0.8em;margin-top:75px">
<div id="chart_div"></div>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Year');
data.addColumn('number', 'followers');
data.addColumn('number', 'following');
data.addRows(2);
data.setValue(0, 0, '{{ username }}');
data.setValue(0, 1, {{ info[username]['followers_count'] }});
data.setValue(0, 2, {{ info[username]['friends_count'] }});
data.setValue(1, 0, 'you');
data.setValue(1, 1, {{ info[this_username]['followers_count'] }});
data.setValue(1, 2, {{ info[this_username]['friends_count'] }});

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, {width: 600, height: 400,
title: 'Coolness in terms of following and followers',
vAxis: {title: 'number of people'},
hAxis: {title: 'cool people have bigger \'followers\' than \'following\''}
});
}
</script>

<table>
<tr>
<td>&nbsp;</td>
<th class="label">{{ username }}</th>
<th class="label">{{ this_username }}</th>
</tr>
<tr>
<td class="label">followers:</td>
<td>{{ info[username]['followers_count'] }}</td>
<td>{{ info[this_username]['followers_count'] }}</td>
</tr>
<tr>
<td class="label">following:</td>
<td>{{ info[username]['friends_count'] }}</td>
<td>{{ info[this_username]['friends_count'] }}</td>
</tr>
<tr>
<td class="label">ratio:</td>
<td>{{ info[username]['ratio'] }}</td>
<td>{{ info[this_username]['ratio'] }}</td>
</tr>
</table>

<!--<p style="font-size:0.8em;margin-top:75px">
There'll be more stuff shown here about this user and you and perhaps an explanation
why this person is too cool for you or whether being followed by this user is cool at all.<br>
Bear with me.
</p>
</p>-->

{% end %}

0 comments on commit 75c5cc2

Please sign in to comment.