Skip to content

Instantly share code, notes, and snippets.

@tatsuru
Created August 29, 2011 00:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tatsuru/1177479 to your computer and use it in GitHub Desktop.
Save tatsuru/1177479 to your computer and use it in GitHub Desktop.
count tumblr posts/day
#!/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