Home > user_timeline_to_ical

user_timeline_to_ical

What is this ?

A simple Ruby script that generates iCalendar format from Twitter user_timeline.

Download

user_timeline_to_ical.rb

Requirements

following gems installed:

  • json
  • icalendar

How to use

simply type

ruby user_timeline_to_ical.rb [your_screen_name]

to print out recent 20 events in iCalendar format to stdout.

Redirect its output to a file, then import it with iCal or Google Calendar.

Screenshot

from iCal: Twitter to iCalendar-01

from Google Calendar: Twitter to iCalendar-02

Future …

I’m developing a web service that serves iCalendar feed (by sending direct message to tracking bot). You will subscribe its URL to see your past behaviors at some time.

Code

Try it !

#
# create iCalendar from recent 20 user_timeline.
#
# usage
#   ruby user_timeline_to_ical.rb [username] > some.ics
#
require 'rubygems'
require 'json'
require 'icalendar'
require 'uri'
require 'open-uri'
require 'kconv'
require 'nkf'
require 'logger'
 
KCODE = 'u'
 
# XXX:
#   quick fix to avoid charset crash
module Icalendar
  class Component < Icalendar::Base
    def print_properties
      s = ""
 
      @properties.each do |key,val| 
        # Take out underscore for property names that conflicted
        # with built-in words.
        if key =~ /ip_.*/
          key = key[3..-1]
        end
 
        # Property name
        unless multiline_property?(key)
           prelude = "#{key.gsub(/_/, '-').upcase}" +
 
           # Possible parameters
           print_parameters(val) 
 
           # Property value
           value = ":#{val.to_ical}" 
           escaped = prelude + value.gsub("\\", "\\\\").gsub("\n", "\\n").gsub(",", "\\,").gsub(";", "\\;")
           #escaped = value
           #s << escaped.slice!(0, MAX_LINE_LENGTH) << "\r\n " while escaped.size > MAX_LINE_LENGTH # XXX : quick fix to avoid charset crash
           s << escaped << "\r\n"
           s.gsub!(/ *$/, '')
         else 
           prelude = "#{key.gsub(/_/, '-').upcase}" 
            val.each do |v| 
               params = print_parameters(v)
               value = ":#{v.to_ical}"
               escaped = prelude + params + value.gsub("\\", "\\\\").gsub("\n", "\\n").gsub(",", "\\,").gsub(";", "\\;")
               s << escaped.slice!(0, MAX_LINE_LENGTH) << "\r\n " while escaped.size > MAX_LINE_LENGTH
               s << escaped << "\r\n"
               s.gsub!(/ *$/, '')
            end
         end
      end
      s
    end
  end
end
 
class UserTimeLine
  attr_accessor :id, :since
  URL = 'http://twitter.com/statuses/user_timeline/'
 
  def initialize(id)
    @id = id
    #@log = Logger.new('debug.log')
  end
 
  def get(url)
    JSON.parse(open(url).readlines.join).sort { |a, b| a['id'] <=> b['id'] }
  end
 
  def url(*arg)
    url = URL + @id + '.json'
    return url if arg.empty?
 
    hash = arg[0]
    args = {}
 
    args['page'] = hash[:page] if hash[:page]
    if hash[:since]
      if String == hash[:since].class 
        args['since'] = URI.escape(hash[:since].split.join('+'))
      else # should be Number
        args['since_id'] = hash[:since]
      end
    end
    arg = args.collect { |k, v| [k, v].join('=') }.join('&')
 
    url + '?' + arg
  end
 
  def to_ical
    msgs = get(url).collect {|x| [x['text'], x['created_at']]}
    cal = Icalendar::Calendar.new
 
    (msgs.size-1).times do |i|
      cal.event do
        dtstart  DateTime.parse(msgs[i][1]).to_ical(true)
        dtend    DateTime.parse(msgs[i+1][1]).to_ical(true)
        summary  msgs[i][0]
      end
    end
 
    cal.to_ical
  end
end
 
 
utl = UserTimeLine.new(ARGV.shift)
print NKF.nkf('-w -Lw', utl.to_ical)

Comments:0

Comment Form
Remember personal info

Trackbacks:0

Trackback URL for this entry
http://en.deadbeaf.org/2007/10/17/user_timeline_to_ical/trackback/
Listed below are links to weblogs that reference
user_timeline_to_ical from en.mootoh.log

Home > user_timeline_to_ical

Feeds

Return to page top