CDMoyer's
Ramblings


15
May

Tweet :: Command-Line Twitter Broadcast with Readline Support

So, after giving Allan some feedback about the latest less project, Get More Honey, we chatted a bit and it came out that I’m a PHP guy, with a bit of a leaning towards CakePHP. But, I’ll fully admit that I love the ruby language, have played with it quite a bit, but simply never had an opportunity to write a rails app. People pay me to write PHP code. ;)

But, it did inspire me to fire up vim and write something in ruby. (Some day I’ll dust off RubyKnight, the chess engine I wrote in ruby after reading Behind Deep Blue) Here it is:

#!/usr/bin/ruby
# Written by Chris Moyer (chris@inarow.net // http://inarow.net/)
# Do with it whatever you want.  That's public domain, right?

require ‘readline’
require ‘rubygems’
require ‘twitter’

user = ARGV0
pass = ARGV1

if !user or !pass then
puts ‘usage: tweet username password’
exit
end

$0 = “tweet #{user} xxxxxxxx”
twitter = Twitter::Base.new(user, pass)

while line = Readline::readline(’(tweet)> ’) do
Readline::HISTORY.push(line)
if line.length > 140 then
puts “#{line.length – 140} characters too long, try again.”
else
begin
twitter.post(line)
puts “Tweeted!”
rescue
puts “Failed to tweet! (#{$!})”
end
end
end

puts “\nBye!”

It solves my problem of reading twitter via gtalk, and not wanting to open twitter.com to post… while hating when I underestimate the length of my message and being trunctaed. I can just let this run in window of my screen session, and tweet away.

So, I’d love to hear from the rubyists out there:

Posted In: · development    · ruby    · twitter   
Comments

14
May

Tweets Per Hour :: Flot & Twitter API Mash-Up (JSONP?)

Screenshot

TPH :: Tweets per Hour

So, as I mentioned before, Flot is an awesome jQuery plugin for making on-the-fly graphs. The twitter api supports a JSONP mode for its output. 100 lines of javascipt (well, 60 if you condense whitespace and braces, commas… less if the goal was to write small code).

What I don’t understand about what I’ve done here is JSONP, which I first read about here, at remy sharp’s blog… Why doesn’t it get more press. Solves the problem that I would mentally run in to all the time when considering various mash-ups and takes on various internet APIs, that of cross-domain limitations with XMLHTTPRequest.

The heart of the magic is demonstrated in this incredibly simple example:

$.getJSON('http://twitter.com/statuses/user_timeline.json?id=CDMoyer&callback=?',
      twitterCallback);

function twitterCallback(timeline) {
alert(’Twitter Username: ’ + timeline0.user.screen_name);
}

The magic happens in two places. First, jQuery replaces the ? in callback=? piece of the URL with a function name that references to the callback you pass to getJSON(). Second, the twitter API takes the callback parameter and wraps the JSON result structure in a function call with that name. The end result is that the JSON structure is passed to a function in the namespace of your document, circumventing the same-domain origin policy

So far Scoble has reached the highest TPH (8.0) I’ve seen, looking at various popular tweeters.


Posted In: · javascript    · development    · twitter    · jquery   
Comments