#!/usr/bin/env ruby # -*- ruby -*- # twitter command line client # That's What I Call Lame require 'json' require 'open-uri' require 'pp' require 'time' begin load "~/.twicl" rescue LoadError abort < auth)) rescue => e if e.message =~ /400 Bad Request/ abort ">> API limit reached. Exiting, try again next hour..." else puts "!> #{e.message}" p e p e.io rescue nil p url return last_id end rescue Timeout::Error # ignore end posts = posts.to_a # On startup, get at most posts of last three days. unless last_id posts.reject! { |post| Time.now - Time.parse(post["created_at"]) > 3*24*60*60 } end posts.reverse_each { |post| post["stamp"] = Time.parse(post["created_at"]).strftime("%m/%d %H:%M") yield post } return posts.map { |post| post["id"] }.max || last_id end def update_friends $last_friends = fetch("http://api.supertweet.net/1.1/statuses/home_timeline.json", $last_friends, $auth) { |post| puts "\r%s %s: %s" % [post["stamp"], post["user"]["screen_name"], post["text"]] } end def update_replies $last_replies = fetch("http://api.supertweet.net/1.1/statuses/mentions_timeline.json", $last_replies, $auth) { |post| puts "\r%s >> %s: %s" % [post["stamp"], post["user"]["screen_name"], post["text"]] } end def update_direct =begin $last_direct = fetch("http://api.supertweet.net/1.1/direct_messages.json", $last_direct, $auth) { |post| puts "\r%s <> %s: %s" % [post["stamp"], post["sender"]["screen_name"], post["text"]] } =end end def post(string, auth) res = Net::HTTP.post_form( URI.parse("http://#{auth * ":"}@api.supertweet.net/1.1/statuses/update.json"), "status" => string, "source" => "twitterandroid") JSON.parse(res.body)["id"] end Thread.new { loop { update_friends; update_replies; update_direct sleep 120 } } Thread.new { loop { print "\r> " line = gets exit if line.nil? line.chomp! unless line.strip.empty? if line.size > 140 puts "!> Line to long, #{line[140..-1].dump} would be clipped." else post line, $auth sleep 1 # ensure it will show up on next fetch end end update_friends; update_replies; update_direct } }.join