Pi (two_pi_r) wrote in lj_nifty,
Pi
two_pi_r
lj_nifty

So, I was bored at work and wrote a ruby script to make a histogram of friend's birthdays. It's pretty fugly and pipes out to gnuplot for the graphing, but hey.
#!/usr/bin/ruby
require 'xmlrpc/client'
require 'net/http'
require 'date'

### lj_histogram.rb by two_pi_r 
###
### FOR INFORMATION & RESEARCH PURPOSES ONLY - ALL USE AT OWN RISK & RESPONSIBILITY
### Requires: Gnuplot and enough crap to get it to generate .png.
###
### Usage:
###
### CHANGE THESE VALUES, or specify them on the command line.
###

$lj_user=ARGV.shift || "two_pi_r" 
$lj_pass=ARGV.shift || "notreal!"

##
## function for manipulating friends lists, shamelessly horked from something
## that horribly violated the TOS. But it's useful code.
##

def get_friends_birthdays
    ret = []
    request={}
    request["username"]=$lj_user
    request["password"]=$lj_pass
    request["includebdays"]=1
    server = XMLRPC::Client.new2("http://www.livejournal.com/interface/xmlrpc")
    result = server.call("LJ.XMLRPC.getfriends",request)

    result["friends"].each do |fr|
        ret.push [ fr["username"],fr["birthday"] ] unless fr["type"]
    end
    return ret
end

print "Retrieving your friends list: "
flist = get_friends_birthdays
puts "#{flist.length} users."

# Create the histogram array. It's 13 items long and should have default value
# of 0, not nil.
histo = Array.new(13) {0}

months = flist.collect do |fr|
    month = Date.strptime(fr[1]).month rescue 0
    histo[month] += 1
end

gnuplot_input = %Q{
set terminal png
set output "birthday_histogram.png"
set xrange [0:12]
set title "Birthday histogram for #$lj_user"
set xlabel "Months"
set xtics rotate ("Unk." 0, "Jan." 1, "Feb." 2, "Mar." 3, "Apr." 4, "May" 5, "June" 6, "July" 7, "Aug." 8, "Sept." 9, "Oct." 10, "Nov" 11, "Dec" 12)
set ylabel "Num. Of Friends"
unset key
plot "-" w boxes
}
IO.popen("gnuplot -", "w") do |gp|
    gp.puts gnuplot_input
    histo.each_with_index do |b,i|
        gp.puts "#{i} #{b}"
    end
    gp.puts "e"
end

Le Output.
Crossposted to my journal.
Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 14 comments