Created
August 29, 2011 00:48
-
-
Save tatsuru/1177479 to your computer and use it in GitHub Desktop.
count tumblr posts/day
This file contains hidden or 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 ruby | |
require 'tumblife' | |
require 'yaml' | |
if ARGV.length < 2 | |
puts "Usage: count_tumblr.rb blogname outfile [limit]" | |
exit 0 | |
end | |
$config = YAML.load_file(ENV['HOME'] + '/.notify_tumblr.yaml') | |
blogname = ARGV[0] | |
outfile = ARGV[1] | |
limit = ARGV[2].nil? ? nil : ARGV[2].to_i | |
# Tumblr API | |
consumer = OAuth::Consumer.new( $config["api"]["consumer_key"], $config["api"]["consumer_secret"], {site: 'http://api.tumblr.com'} ) | |
access_token = OAuth::AccessToken.new( consumer, $config["api"]["oauth_token"], $config["api"]["oauth_secret"] ) | |
client = Tumblife.new(access_token) | |
total_posts = client.posts(blogname).total_posts | |
if limit.nil? | |
limit = total_posts | |
elsif limit > total_posts | |
limit = total_posts | |
end | |
current = 0 | |
step = 20 | |
dist = {} | |
while current < limit do | |
puts "getting #{current}/#{limit}..." | |
posts = client.posts(blogname, :limit => step, :offset => current) | |
posts.posts.each do |post| | |
d = Time.at(post.timestamp).strftime("%Y-%m-%d") | |
if dist.keys.include? d | |
dist[d] += 1 | |
else | |
dist[d] = 1 | |
end | |
end | |
current += step | |
end | |
open(outfile, 'w') do |f| | |
dist.each do |k, v| | |
f.write("#{k} #{v}\n") | |
end | |
end | |
puts <<EOS | |
successfully finished. | |
plotting command for gnuplot: | |
set xdata time | |
set timefmt "%Y-%m-%d" | |
plot "#{outfile}" u 1:2 w boxes | |
EOS |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment