sunny / mariokartwiit
- Source
- Commits
- Network (0)
- Issues (0)
- Downloads (0)
- Wiki (1)
- Graphs
-
Branch:
master
mariokartwiit / mariokartwiit.rb
| cda1a011 » | sunny | 2008-04-21 | 1 | #!/usr/bin/env ruby | |
| 2 | ## MarioKarTwiit | ||||
| 3 | ## Finds Mario Kart Wii codes through your friends' timelines on Twitter. | ||||
| 4 | ## Use it with your twitter username like so: | ||||
| 5 | ## ruby mariokartwiit.rb username | ||||
| 6 | # | ||||
| 7 | # Needs the json gem: | ||||
| 8 | # sudo gem install json | ||||
| 9 | # | ||||
| 10 | # TODO: | ||||
| 11 | # - Go further back than 20 statuses per friend | ||||
| 12 | # - Deal with people with more than 100 friends | ||||
| 13 | # - Turn it into a webapp | ||||
| 14 | # | ||||
| 15 | # By Sunny Ripert - http://sunfox.org | ||||
| 16 | # Under the WTFPL - http://sam.zoy.org/wtfpl/ | ||||
| 17 | # Learn more about the Twitter API over here: http://twitter.com/help/api | ||||
| 18 | |||||
| 19 | require 'open-uri' | ||||
| 20 | require 'rubygems' | ||||
| 21 | require 'json' | ||||
| 22 | |||||
| e56fcd7d » | sunny | 2008-05-01 | 23 | class MarioKarTwiit | |
| 24 | MARIO_KART_CODE_RE = /(\d{4}-\d{4}-\d{4})/ | ||||
| cda1a011 » | sunny | 2008-04-21 | 25 | ||
| e56fcd7d » | sunny | 2008-05-01 | 26 | ||
| 988aeb31 » | sunny | 2008-05-01 | 27 | # Returns an array of twitter friends who have a Wii Mario Kart. The user hash | |
| 28 | # contains the code, a name, a screen_name and the full status hash that matched. | ||||
| e56fcd7d » | sunny | 2008-05-01 | 29 | # | |
| 30 | # mariokartwiits_for("sunfox") | ||||
| 31 | # # => [{"screen_name" => "adylk", "name" => "Audrey", "code" => "9837-... | ||||
| 32 | # ..., "status" => {"text" => "My Mario Kart Wii Code is ..., ...}] | ||||
| 33 | def self.friends_with_codes_for(username) | ||||
| 34 | friends_with_codes = [] | ||||
| 988aeb31 » | sunny | 2008-05-01 | 35 | followers_for(username).each do |friend| | |
| e56fcd7d » | sunny | 2008-05-01 | 36 | friend["statuses"].each do |status| | |
| 37 | if status['text'] =~ MARIO_KART_CODE_RE | ||||
| 38 | friend["code"] = $1 | ||||
| 39 | friend["status"] = status | ||||
| 40 | friends_with_codes << friend | ||||
| 41 | break | ||||
| 42 | end | ||||
| 43 | end | ||||
| 44 | end | ||||
| 45 | friends_with_codes | ||||
| 46 | end | ||||
| 47 | |||||
| 988aeb31 » | sunny | 2008-05-01 | 48 | # Returns an array of user hashes with all their latest statuses | |
| 49 | def self.followers_for(username) | ||||
| e56fcd7d » | sunny | 2008-05-01 | 50 | friends = JSON.parse(open("http://twitter.com/statuses/friends/#{username}.json?lite=true").read) | |
| 51 | threads = [] | ||||
| 52 | statuses = [] | ||||
| 53 | friends.each do |friend| | ||||
| 54 | threads << Thread.new(friend) do |user| | ||||
| 55 | friend["statuses"] = JSON.parse(open("http://twitter.com/statuses/user_timeline/#{user["screen_name"]}.json").read) | ||||
| cda1a011 » | sunny | 2008-04-21 | 56 | end | |
| 57 | end | ||||
| e56fcd7d » | sunny | 2008-05-01 | 58 | threads.each { |thread| thread.join } | |
| 59 | friends | ||||
| cda1a011 » | sunny | 2008-04-21 | 60 | end | |
| 61 | end | ||||
| 62 | |||||
| 63 | # Usage is made of oranges and lemonade and lines starting with "##" | ||||
| 64 | def usage | ||||
| 65 | open(__FILE__).read.grep(/^## ?/).join.gsub(/^## ?/, '') | ||||
| 66 | end | ||||
| 67 | |||||
| 68 | if __FILE__ == $0 | ||||
| 69 | abort usage if ARGV.size < 1 | ||||
| 988aeb31 » | sunny | 2008-05-01 | 70 | MarioKarTwiit.friends_with_codes_for(ARGV.first).each do |friend| | |
| 56b6da50 » | sunny | 2008-05-01 | 71 | puts "#{friend["name"]}: #{friend["code"]}" | |
| cda1a011 » | sunny | 2008-04-21 | 72 | end | |
| 73 | end | ||||
