In order to end the fight between dogs and cats, I made a page on the web to compare whether dogs or cats are attracting attention.
Comparison http://xiidec.appspot.com/vschart.html?q=dogs, cats
The result looks like this.
Cat dominance. The number of Hatena bookmarks for the past 30 days is aggregated and graphed with the Google Chart API.
You can compare anything other than dogs and cats by entering keywords in the input box separated by commas.
All kinds of religious wars are over!
The mechanism is very simple.
Hatena Bookmark provides new RSS for each keyword.
http://b.hatena.ne.jp/search/tag?q=cat & mode = rss
Just embed a keyword in this URL and access it. Then RSS will drop.
Because it is difficult to measure popularity by the number of articles Count how many bookmarks you have. In XML
<hatena:bookmarkcount>16</hatena:bookmarkcount>
There is an item, so count it. By the way, this time I've only fetched articles within the last 30 days. If you want to see the date of the article
<dc:date>2005-12-22T14:40:18+09:00<<dc:date>
Just look at that.
Introducing the Google Chart API. This is also easy to use.
https://chart.googleapis.com/chart?
After the URL
cht = [Graph type]
When
& chs = [Graph size]
When
& chd = [Graph parameters]
When
& chl = [label name]
With
https://chart.googleapis.com/chart?cht=p3&chs=400x200&chd=t:48,51&chl=%E7%8A%AC|%E7%8C%AB
Just create a URL like this.
Completed in no time. Here is the finished product.
vschart.py
#!/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)
vschart.html
<html>
<head>
</head>
<body style="">
<p> </p>
<p>
<meta charset="UTF-8"></meta>
<title>Comparison-kun</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>Which is more popular?</h1>
<form action="/vschart.html" method="get">
Enter the comparison targets separated by commas. Let's compare the degree of attention on the net over the past month.
<input type="text" name="q" id="q" size="40" value="{{ q }}"><input type="submit" value="Compare">
</form>
<div>
<img src="{{ url }}" alt="Dandelion">
</div>
</div>
</div>
</body>
</html>
This time it was a pie chart, but there are various graphs such as bar graphs and line graphs. Check the Google reference page for more details!
Google Chart Tools:Image Charts (Deprecated) https://developers.google.com/chart/image/?hl=ja
Hmm ... Deprecated?
Important: The Image Charts portion of Google Chart Tools has been officially deprecated as of April 20, 2012. It will continue to work as per our deprecation policy.
Important: The image chart portion of Google's chart tool has been officially discontinued as of April 20, 2012. It will continue to work according to our deprecated policy.
Ehhhhhh The Google Chart API with images is no longer recommended It seems that you can use the new Google Chart API that uses Javascript and SVG.
I made it with much effort ...
Recommended Posts