Created
December 15, 2013 10:53
-
-
Save kurehajime/7971528 to your computer and use it in GitHub Desktop.
犬派と猫派の勢力図をGoogleChartAPIでグラフ化して決着をつける ref: http://qiita.com/kurehajime/items/cd48febad3e802f9fb1d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
</head> | |
<body style=""> | |
<p> </p> | |
<p> | |
<meta charset="UTF-8"></meta> | |
<title>比較くん</title> | |
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.css" /> | |
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"></script> | |
<script type="text/javascript" src="http://code.jquery.com/mobile/1.1.0/jquery.mobile-1.1.0.min.js"></script> | |
<div data-role="page" id="first"> | |
<div data-role="content"> | |
<h1>どっちが人気?</h1> | |
<form action="/vschart.html" method="get"> | |
比較対象をカンマ区切りで入れてね。過去一ヶ月間のネット上での注目度合いを比較するよ。 | |
<input type="text" name="q" id="q" size="40" value="{{ q }}"><input type="submit" value="比較する"> | |
</form> | |
<div> | |
<img src="{{ url }}" alt="たんぽぽ"> | |
</div> | |
</div> | |
</div> | |
</body> | |
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
import webapp2 | |
import os | |
import urllib | |
from google.appengine.ext.webapp import template | |
from datetime import datetime, timedelta | |
from xml.etree.ElementTree import * | |
class VsChart(webapp2.RequestHandler): | |
def get(self): | |
if self.request.get('q')!="": | |
url=self.get_hatebu_chart_url(self.request.get('q').replace(" ",",").split(",")) | |
else: | |
url="" | |
q=self.request.get('q') | |
template_values={ | |
'url':url, | |
'q':q | |
} | |
path = os.path.join(os.path.dirname(__file__), 'html/vschart.html') | |
self.response.out.write(template.render(path, template_values)) | |
def count_hatebu_tag(self,q): | |
count=0 | |
ago_30=datetime.today()-timedelta(days=30) | |
tree = parse(urllib.urlopen('http://b.hatena.ne.jp/search/tag?q=' + q + '&mode=rss')) | |
for i in tree.findall('./{http://purl.org/rss/1.0/}item'): | |
if ago_30 <= datetime.strptime(i.find('{http://purl.org/dc/elements/1.1/}date').text.split("T")[0], "%Y-%m-%d"): | |
count += int(i.find('{http://www.hatena.ne.jp/info/xmlns#}bookmarkcount').text) | |
return count | |
def get_hatebu_chart_url(self,qList): | |
countList=[] | |
allCount=0 | |
for q in qList: | |
count = self.count_hatebu_tag(q.encode("utf-8")) | |
allCount+=count | |
countList.append(count) | |
if allCount != 0: | |
perList = [str((c*100 / allCount)) for c in countList] | |
else: | |
perList = ["0"]*len(qList) | |
return "https://chart.googleapis.com/chart?cht=p3&chs=400x200&chd=t:"+",".join(perList)+"&chl="+"|".join(qList) | |
app = webapp2.WSGIApplication([ | |
('/vschart.html', VsChart) | |
], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment