Created
February 12, 2010 17:21
-
-
Save yoko/302768 to your computer and use it in GitHub Desktop.
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 ruby | |
require 'rubygems' | |
require 'sequel' | |
require 'google_chart' | |
require 'open-uri' | |
(size, color) = *ARGV | |
size = size || '640x240' | |
color = color || 'a8bccb' | |
db = Sequel.sqlite('tumblr_stats.db') | |
data = db[:reblogs].order(:received_at) | |
from = data.first[:received_at].strftime('%Y-%m-%d') | |
to = data.last[:received_at].strftime('%Y-%m-%d') | |
title = "Reblogged from #{from} to #{to}" | |
GoogleChart::LineChart.new(size, title) do |c| | |
counts = [] | |
users = [] | |
data = db[:user_counts].order(:count.desc, :user) | |
data.each do |r| | |
counts.push(r[:count]) | |
users.push(r[:user]) | |
end | |
c.show_legend = false | |
c.data '', counts, color | |
c.fill_area color, 0, 0 | |
c.axis :x, :range => [0, data.count], | |
:color => '777777', | |
:font_size => 10 | |
c.axis :x, :labels => ['User'], | |
:positions => [50], | |
:color => '999999', | |
:font_size => 10, | |
:alignment => :center | |
c.axis :y, :range => [0, data.first[:count]], | |
:color => '777777', | |
:font_size => 10 | |
c.axis :y, :labels => ['Reblog'], | |
:positions => [50], | |
:color => '999999', | |
:font_size => 10, | |
:alignment => :center | |
c.grid :x_step => 100 / 5.0, | |
:y_step => 100 / 4.85, | |
:length_segment => 1, | |
:length_blank => 5 | |
url = c.to_escaped_url(:chts => '555555,12') | |
puts url | |
open(url) do |chart| | |
open('reblogged_count.png', 'wb') do |f| | |
f.write(chart.read) | |
end | |
end | |
`open reblogged_count.png` | |
end |
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 ruby | |
require 'rubygems' | |
require 'sequel' | |
Sequel::Model.plugin(:schema) | |
db = Sequel.sqlite('tumblr_stats.db') | |
class Reblog < Sequel::Model(:reblogs) | |
unless table_exists? | |
set_schema do | |
primary_key :id | |
text :user | |
text :url | |
timestamp :received_at | |
end | |
create_table | |
end | |
end | |
class UserCount < Sequel::Model(:user_counts) | |
unless table_exists? | |
set_schema do | |
primary_key :id | |
text :user | |
int :count | |
end | |
create_table | |
end | |
end | |
(subject, content, date) = *ARGV; | |
`/usr/local/bin/growlnotify -t "Tumblr" -m "#{subject}" -a "Tumblr Backup"` | |
user = /reblogged by (\w+)$/.match(subject).to_a[1] | |
url = /^(http:\/\/.+\d+)(\/|$)/.match(content).to_a[1] | |
date = Time.parse(date.gsub(/[^\d: ]/, '')).to_i # japanese date format | |
p user, url, date | |
exit unless user | |
db.transaction do | |
exit if Reblog.filter(:user => user, :url => url, :received_at => date).count > 0 | |
Reblog.insert(:user => user, :url => url, :received_at => date) | |
if u = UserCount.filter(:user => user).first() | |
u.update(:count => u[:count] + 1) | |
else | |
UserCount.insert(:user => user, :count => 1) | |
end | |
end |
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
using terms from application "Mail" | |
on perform mail action with messages theMessages for rule theRule | |
tell application "Mail" | |
repeat with eachMessage in theMessages | |
set theSubject to (subject of eachMessage) | |
set theContent to (content of eachMessage) | |
set {year:y, month:m, day:d, time string:t} to (date sent of eachMessage) | |
set theDate to ((y * 10000 + m * 100 + d) as string) & " " & (t as string) | |
try | |
do shell script "cd ~/apps/tumblr_stats; ruby ./tumblr_stats.rb \"" & theSubject & "\" \"" & theContent & "\" \"" & theDate & "\"" | |
on error errorText | |
display dialog errorText | |
end try | |
end repeat | |
end tell | |
end perform mail action with messages | |
end using terms from |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment