Ruby开发中Twitter数据的详细步骤
YPE html>
å¼å人å对è¯è¨çæ¥è§¦åºè¯¥æ¯é常å¤çï¼èRubyèæ¬æ¥å¯¹Twitterç¨æ·çæ°æ®ä¹æ£å¨åå°ï¼è¿å ¶ä¸çç¼ç±æ³å¿ 大家并ä¸æ¸ æ¥ï¼ä»å¤©ç±ç«ææ¯é¢éå°ç¼éè¿Rubyå¼åä¸Twitteræ°æ®ç详ç»æ¥éª¤ï¼ä¸ºå¤§å®¶å®ä¾éè¿°ã
Twitter以åä¸äºAPI
尽管æ©æçç½ç»æ¶åçæ¯äºº-æºå¨ç交äºï¼ä½ç°å¨çç½ç»å·²æ¶åæºå¨-æºå¨ä¹é´ç交äºï¼è¿ç§äº¤äºæ¯ä½¿ç¨webæå¡æ¥æ¯æçã大é¨åå欢è¿çç½ç«é½æè¿æ ·çæå¡åå¨ââä»åç§åæ ·çGoogleæå¡å°LinkedInãFacebookåTwitterçãéè¿webæå¡å建çAPIï¼å¤é¨çåºç¨å¯ä»¥æ¥è¯¢ææ¯æ纵ç½ç«ä¸çå 容ã
webæå¡å¯ä»¥ä½¿ç¨å¤ç§æ¹å¼æ¥å®ç°ãç®åææµè¡çåæ³ä¹ä¸æ¯è¡¨è¿°æ§ç¶æ转移(Representational State Transfeï¼ REST)ãRESTçä¸ç§å®ç°æ¯éè¿ä¸ºäººçç¥çHTTPåè®®ï¼å 许HTTPä½ä¸ºRESTfulæ¶æçåªä»åå¨(使ç¨è¯¸å¦GETãPUTãPOSTãDELETEä¸ç±»çæ åHTTPæä½)ãTwitterçAPIæ¯ä½ä¸ºè¿ä¸åªä»ä¹ä¸çä¸ä¸ªæ½è±¡æ¥è¿è¡å¼åçãå¨è¿ä¸åæ³ä¸ï¼æ²¡ææ¶åRESTãHTTPææ¯XMLæJSONä¸ç±»çæ°æ®æ ¼å¼çç¥è¯ï¼èæ¯ä»£ä¹ä»¥æ¸ æ°å°æ´åå°äºRubyè¯è¨ä¸çåºäºå¯¹è±¡çæ¥å£ã
RubyåTwitterçä¸ä¸ªå¿«éå±ç¤º
让æ们æ¥æ¢è®¨ä¸ä¸å¦ä½å¨Rubyä¸ä½¿ç¨Twitter APIãé¦å ï¼æ们éè¦è·åæéçèµæºï¼å¦æä½ åæä¸æ ·å¨ç¨Ubuntu Linux®çè¯ï¼å°±ä½¿ç¨aptæ¡æ¶ã
è¥è¦è·åææ°çå®æ´çRubyååçæ¬(大约13MBçä¸è½½é)ï¼ä½¿ç¨è¿ä¸å½ä»¤è¡ï¼
$ sudo apt-get install ruby1.9.1-full
æ¥ç使ç¨gemå®ç¨ç¨åºæ¥æåTwitter gemï¼
$ sudo gem install twitter
ç°å¨ä½ å·²ç»æäºè¿ä¸æ¥éª¤æéçä¸åäºï¼æ们继ç»ï¼æµè¯ä¸ä¸Twitterçå è£ å¨ãè¿ä¸ç¤ºä¾ä½¿ç¨ä¸ä¸ªå为交äºå¼çRubyå¤å£³(Interactive Ruby Shellï¼IRB)çå¤å£³ç¨åºï¼è¯¥å¤å£³ç¨åºå 许å®æ¶å°æ§è¡Rubyå½ä»¤ä»¥å使ç¨è¯è¨è¿è¡å®éªãIRBæçé常å¤çåè½ï¼ä¸è¿æ们åªç¨å®æ¥åä¸äºç®åçå®éªã
æ¸ å1å±ç¤ºäºä¸IRBçä¸ä¸ªä¼è¯ï¼è¯¥ä¼è¯è¢«åæäºä¸æ®µä»¥ä¾¿äºé 读ã第ä¸æ®µ(001å002è¡)éè¿å¯¼å ¥å¿ éçè¿è¡æ¶å ç´ æ¥å好ç¯å¢æ¹é¢çåå¤(requireæ¹æ³å 载并æ§è¡æå®çåº)ãæ¥ä¸æ¥çä¸æ®µ(003è¡)说æçæ¯ä½¿ç¨Twitter gemæ¥æ¾ç¤ºä»IBM® developerWorks®ååºçææ°tweetæ¶æ¯ãå¦æå±ç¤ºçé£æ ·ï¼ä½¿ç¨Client::Timeline模åçuser_timelineæ¹æ³æ¥æ¾ç¤ºä¸æ¡æ¶æ¯ï¼è¿ç¬¬ä¸ä¸ªä¾å说æäºRubyçâé¾æ¹æ³âçåè½ãuser_timelineæ¹æ³è¿åä¸ä¸ªæç20æ¡æ¶æ¯çæ°ç»ï¼æ¥çé¾å ¥å°æ¹æ³firstä¸ï¼è¿æ ·åæ¯ä¸ºäºä»æ°ç»ä¸æååºç¬¬ä¸æ¡æ¶æ¯(firstæ¯Arrayç±»çä¸ä¸ªæ¹æ³)ï¼æ¥çä»è¿ä¸æ¡æ¶æ¯ä¸æååºææ¬å段ï¼éè¿putsæ¹æ³æå®æ¾å°è¾åºä¸ã
æ¥ä¸æ¥çä¸æ®µ(004è¡)使ç¨äºç¨æ·å®ä¹çä½ç½®å段ï¼è¿æ¯ä¸ä¸ªä¸éå½¢å¼çå段ï¼ç¨æ·å¯ä»¥å¨å ¶ä¸æä¾æç¨çææ¯æ ç¨çä½ç½®ä¿¡æ¯ãå¨è¿ä¸ä¾åä¸ï¼User模åæåäºä½ç½®å段æéå®çç¨æ·ä¿¡æ¯ã
æåä¸æ®µ(ä»005è¡å¼å§)ç 究äºTwitter::Search模åï¼è¿ä¸æ索模åæä¾äºæå ¶ä¸°å¯çç¨æ¥æç´¢Twitterçæ¥å£ãå¨è¿ä¸ä¾åä¸ï¼é¦å æ¯å建ä¸ä¸ªæç´¢å®ä¾(005è¡)ï¼æ¥çå¨006è¡æå®ä¸ä¸ªæç´¢ï¼æ LulzSecç¨æ·å¨æè¿ååºçå å«äºwhyè¿ä¸è¯çæ¶æ¯ï¼ç»æå表已ç»è¿å ååç¼è¾ãæ索设置ä¼ä¸ç´åå¨å¨é£éï¼å 为æç´¢å®ä¾ä¿æçæå®ä¹çè¿æ»¤æ¡ä»¶ï¼ä½ å¯ä»¥éè¿æ§è¡search.clearæ¥æ¸ é¤è¿äºè¿æ»¤æ¡ä»¶ã
æ¸ å1. éè¿IRBæ¥å®éªTwitter API
$ irb irb(main):001:0> require "rubygems" => true irb(main):002:0> require "twitter" => true irb(main):003:0> puts Twitter.user_timeline("developerworks").first.text dW Twitter is saving #IBM over $600K per month: will #Google+ add to that?> http://t.co/HiRwir7 #Tech #webdesign #Socialmedia #webapp #app => nil irb(main):004:0> puts Twitter.user("MTimJones").location Colorado, USA => nil irb(main):005:0> search = Twitter::Search.new => #<:search:0xb7437e04 ruby gem>[], :q=>[]}, @cache=nil, @gateway=nil, @consumer_key=nil, @proxy=nil, @format=:json, @adapter=:net_http search.containing("why").to("LulzSec"). result_type("recent").each do |r| puts r.text end @LulzSec why not stop postingand get a full time job! MYSQLi isn't hacking you . ... irb(main):007:0>
æ¥ä¸æ¥ï¼æ们æ¥çä¸ä¸Twitterä¸çç¨æ·ç模å¼ï¼ä½ ä¹å¯ä»¥éè¿IRBæ¥å®ç°è¿ä¸ç¹ï¼ä¸è¿æéæäºç»æçæ ¼å¼ï¼ä»¥ä¾¿ç®å对Twitterç¨æ·çå é¨ç»æç说æãæ¸ å2ç»åºäºç¨æ·ç»æçè¾åºç»æï¼è¿å¨Rubyä¸æ¯ä¸ä¸ªHashie::Mashãè¿ä¸ç»æå¾æç¨ï¼å ä¸ºå ¶å 许对象æç±»æ¹æ³çåå¸é®è®¿é®å¨(å ¬å¼ç对象)ãæ£å¦ä½ ä»æ¸ å2ä¸çå°çé£æ ·ï¼è¿ä¸å¯¹è±¡å å«äºä¸°å¯çä¿¡æ¯(ç¨æ·ç¹å®ç以å渲æçä¿¡æ¯)ï¼å ¶ä¸å æ¬äºå½åçç¨æ·ç¶æ(带æå°çç¼ç ä¿¡æ¯)ãä¸æ¡tweetæ¶æ¯ä¸ä¹å å«äºå¤§éçä¿¡æ¯ï¼ä½ å¯ä»¥ä½¿ç¨user_timelineç±»æ¥è½»æ¾å°å¯è§åçæè¿ä¸ä¿¡æ¯ã
æ¸ å2. Twitterç¨æ·çå é¨è§£æç»æ(Rubyè§è§)
irb(main):007:0> puts Twitter.user("MTimJones") bounding_box= country="United States" country_code="US" full_name="Longmont, CO" id="2736a5db074e8201" name="Longmont" place_type="city" url="http://api.twitter.com/1/geo/id/2736a5db074e8201.json" > retweet_count=0 retweeted=false source="web" text="@AnonymousIRC @anonymouSabu @LulzSec @atopiary @Anonakomis Practical reading for future reference... LULZ \"Prison 101\" http://t.co/sf8jIH9" truncated=false > statuses_count=79 time_zone="Mountain Time (US & Canada)" url="http://www.mtjones.com" utc_offset=-25200 verified=false > => nil irb(main):008:0>
è¿å°±æ¯å¿«éå±ç¤ºé¨åçå 容ãç°å¨ï¼æ们æ¥ç 究ä¸äºç®åçèæ¬ï¼ä½ å¯ä»¥å¨è¿äºèæ¬ä¸ä½¿ç¨RubyåTwitter APIæ¥æ¶éåå¯è§åæ°æ®ãå¨è¿ä¸è¿ç¨ä¸ï¼ä½ ä¼äºè§£å°Twitterçä¸äºæ¦å¿µï¼æ¯å¦è¯´èº«ä»½éªè¯åé¢çéå¶çã
ææTwitteræ°æ®
æ¥ä¸æ¥çå èå 容ä»ç»å 个éè¿Twitter APIæ¥æ¶éååç°å¯ç¨æ°æ®çèæ¬ï¼è¿äºèæ¬éç¹å¨äºå ¶ç®ææ§ï¼ä¸è¿ä½ å¯ä»¥éè¿æ©å±ä»¥åç»åä»ä»¬æ¥å建æ°çåè½ãå¦å¤ï¼æ¬èå 容è¿ä¼æå°Twitter gem APIï¼è¿ä¸APIä¸æçæ´å¤å¯ç¨çåè½ã
éè¦æ³¨æçå¾éè¦çä¸ç¹æ¯ï¼å¨æå®çæ¶é´å ï¼Twitter APIåªå 许客æ·åæé次çè°ç¨ï¼è¿ä¹å°±æ¯Twitterçé¢çéå¶è¯·æ±(ç°å¨æ¯ä¸å°æ¶ä¸è¶ è¿150次)ï¼è¿æå³çç»è¿æ个次æ°ç使ç¨åï¼ä½ ä¼æ¶å°ä¸ä¸ªé误æ¶æ¯ï¼å¹¶è¦æ±ä½ å¨æ交æ°ç请æ±ä¹åå åä¸æ®µæ¶é´ççå¾ ã
ç¨æ·ä¿¡æ¯
åæ³ä¸ä¸æ¸ å2ä¸çæ¯ä¸ªTwitterç¨æ·ç大éå¯ç¨ä¿¡æ¯ï¼åªæå¨ç¨æ·ä¸åä¿æ¤çæ åµä¸è¿äºä¿¡æ¯ææ¯å¯è®¿é®çãæ们æ¥çä¸ä¸å¦ä½ä»¥ä¸ç§æ´ä¾¿æ·çæ¹å¼æ¥æåç¨æ·çä¿¡æ¯å¹¶åç°åºæ¥ã
æ¸ å3ç»åºäºä¸ä¸ª(åºäºç¨æ·ççé¢æ¾ç¤ºå称)æ£ç´¢ç¨æ·ä¿¡æ¯çç®åçRubyèæ¬ï¼ç¶åæ¾ç¤ºä¸äºæ´æç¨çå 容ï¼å¨éè¦æ¶ä½¿ç¨Rubyæ¹æ³to_sæ¥æå¼è½¬æ¢æå符串ãéè¦æ³¨æçæ¯ï¼é¦å ç¨æ·æ¯ä¸åä¿æ¤çï¼å¦åçè¯æ¯ä¸è½è®¿é®å°å¥¹/ä»çæ°æ®çã
æ¸ å3. æåTwitterç¨æ·æ°æ®çç®åèæ¬(user.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" screen_name = String.new ARGV[0] a_user = Twitter.user(screen_name) if a_user.protected != true puts "Username : " + a_user.screen_name.to_s puts "Name : " + a_user.name puts "Id : " + a_user.id_str puts "Location : " + a_user.location puts "User since : " + a_user.created_at.to_s puts "Bio : " + a_user.description.to_s puts "Followers : " + a_user.followers_count.to_s puts "Friends : " + a_user.friends_count.to_s puts "Listed Cnt : " + a_user.listed_count.to_s puts "Tweet Cnt : " + a_user.statuses_count.to_s puts "Geocoded : " + a_user.geo_enabled.to_s puts "Language : " + a_user.lang puts "URL : " + a_user.url.to_s puts "Time Zone : " + a_user.time_zone puts "Verified : " + a_user.verified.to_s puts tweet = Twitter.user_timeline(screen_name).first puts "Tweet time : " + tweet.created_at puts "Tweet ID : " + tweet.id.to_s puts "Tweet text : " + tweet.text end
è¥è¦è°ç¨è¿ä¸èæ¬ï¼éè¦ç¡®ä¿å ¶æ¯å¯æ§è¡ç(chmod +x user.rb)ï¼ä½¿ç¨ä¸ä¸ªç¨æ·çå称æ¥è°ç¨å®ãæ¸ å4æ¾ç¤ºäºä½¿ç¨ç¨æ·developerworksè°ç¨çç»æï¼ç»åºäºç¨æ·çä¿¡æ¯åå½åç¶æ(æåä¸æ¡tweetæ¶æ¯)ãè¿éè¦æ³¨æçæ¯ï¼Twitteræå ³æ³¨ä½ ç人å®ä¹ä¸ºfollowers(ç²ä¸);èæä½ å ³æ³¨ç人称ä½friends(æå)ã
æ¸ å4. user.rbçè¾åºä¾å
$ ./user.rb developerworks Username : developerworks Name : developerworks Id : 16362921 Location : User since : Fri Sep 19 13:10:39 +0000 2008 Bio : IBM's premier Web site for Java, Android, Linux, Open Source, PHP, Social, Cloud Computing, Google, jQuery, and Web developer educational resources Followers : 48439 Friends : 46299 Listed Cnt : 3801 Tweet Cnt : 9831 Geocoded : false Language : en URL : http://bit.ly/EQ7te Time Zone : Pacific Time (US & Canada) Verified : false Tweet time : Sun Jul 17 01:04:46 +0000 2011 Tweet ID : 92399309022167040 Tweet text : dW Twitter is saving #IBM over $600K per month: will #Google+ add to that? > http://t.co/HiRwir7 #Tech #webdesign #Socialmedia #webapp #app
æåçå欢è¿æ åµ
ç 究ä¸ä¸ä½ çæå(ä½ å ³æ³¨ç人)ï¼æ¶éæ°æ®æ¥äºè§£ä¸ä¸ä»ä»¬çå欢è¿ç¨åº¦ãå¨è¿ä¸ªä¾åä¸ï¼æ¶éæåçæ°æ®å¹¶æç §ä»ä»¬çç²ä¸æ°ç®æ¥è¿è¡æåºï¼è¿ä¸ç®åçèæ¬å¦æ¸ å5æ示ã
å¨è¿ä¸èæ¬ä¸ï¼å¨äºè§£äºä½ è¦(åºäºçé¢æ¾ç¤ºå称)åæçç¨æ·åï¼å建ä¸ä¸ªç¨æ·åå¸è¡¨ï¼Rubyåå¸(ææ¯ç¸å ³çæ°ç»)æ¯ä¸ç§å¯ä»¥å è®¸ä½ å®ä¹åå¨é®(èä¸æ¯ç®åçæ°å¼ç´¢å¼)çæ°æ®ç»æãæ¥çï¼éè¿Twitterççé¢å称æ¥ç´¢å¼è¿ä¸åå¸è¡¨ï¼å ³èå¼åæ¯ç¨æ·çç²ä¸æ°ç®ãè¿ä¸è¿ç¨ç®åå°éåä½ çæåç¶åæä»ä»¬çç²ä¸çæ°ç®æ¾å ¥åå¸è¡¨ä¸ï¼æ¥ç(以éåº)æååå¸è¡¨ï¼ç¶åæå®æ¾å°è¾åºä¸ã
æ¸ å5. å ³äºæåçå欢è¿ç¨åº¦çèæ¬(friends.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" name = String.new ARGV[0] user = Hash.new # Iterate friends, hash their followers Twitter.friends(name).users.each do |f| # Only iterate if we can see their followers if (f.protected.to_s != "true") user[f.screen_name.to_s] = f.followers_count end end user.sort_by {|k,v| -v}.each { |user, count| puts "#{user}, #{count}" }
æ¸ å5ä¸çæåèæ¬çä¸ä¸ªä¾åè¾åºå¦æ¸ å6æ示ãæå åäºè¾åºå 容以èç空é´ï¼ä¸è¿ä½ å¯ä»¥çå°ï¼å¨æçç´æ¥ç½ç»ä¸ï¼ReadWriteWeb(RWW)åPlaystationæ¯å¾å欢è¿çTwitterç¨æ·ã
æ¸ å6. æ¸ å5ä¸çæåèæ¬ççé¢è¾åº
$ ./friends.rb MTimJones RWW, 1096862 PlayStation, 1026634 HarvardBiz, 541139 tedtalks, 526886 lifehacker, 146162 wandfc, 121683 AnonymousIRC, 117896 iTunesPodcasts, 82581 adultswim, 76188 forrester, 72945 googleresearch, 66318 Gartner_inc, 57468 developerworks, 48518
æçç²ä¸æ¥èªåªé
åæ³ä¸ä¸æ¸ å2ï¼Twitteræä¾äºä¸°å¯çä½ç½®ä¿¡æ¯ï¼æä¸ä¸ªä¸éå½¢å¼ãç¨æ·å®ä¹çãå¯éç¨å°çç¼ç æ°æ®çä½ç½®å段ãä¸è¿ï¼ç¨æ·è®¾å®çæ¶åºä¹å¯ä»¥ä¸ºç²ä¸çå®é ä½ç½®æä¾çº¿ç´¢ã
å¨è¿ä¸ä¾åä¸ï¼ä½ è¦æ建ä¸ä¸ªæ··æåºç¨ç¨åºæ¥ä»Twitterç²ä¸ä¸æåæ¶åºæ°æ®ï¼ç¶å使ç¨Google Chartsæ¥å¯è§åè¿ä¸æ°æ®ãGoogle Chartsæ¯ä¸ä¸ªé常æææç项ç®ï¼å ¶å è®¸ä½ éè¿ç½ç»æ¥æ建åç§åæ ·ä¸åç±»åçå¾è¡¨;æå¾è¡¨çç±»ååæ°æ®å®ä¹æHTTP请æ±ï¼ç´æ¥å¨æµè§å¨ä¸æ¸²æä½ä¸ºååºçç»æãè¥è¦å®è£ ç¨äºGoogle ChartsçRuby gemï¼ä½¿ç¨ä¸é¢çå½ä»¤è¡ï¼
$ gem install gchartrb
æ¸ å7æä¾çèæ¬æåæ¶åºæ°æ®ï¼æ¥çæ建Google Charts请æ±ãé¦å ï¼ä¸ä¹åçèæ¬ä¸åï¼è¿ä¸èæ¬éè¦ä½ éè¿Twitterç身份éªè¯ãè¥è¦åå°è¿ä¸ç¹ï¼ä½ éè¦æ³¨åä¸ä¸ªTwitteråºç¨ï¼è¿ä¼ç»ä½ æä¾ä¸ç»é®å¼å令çï¼è¿äºä»¤çå¯ä»¥ç¨å¨æ¸ å7çèæ¬ä¸ï¼è¿æ ·æè½å¤æåå°åå°æ°æ®ã请åé åèèµæäºè§£è¿ä¸ç®åè¿ç¨ç详ç»æ åµã
æç §ç¸ç±»ä¼¼ç模å¼ï¼è¯¥èæ¬æ¥åäºä¸ä¸ªçé¢å称ï¼ç¶åéå该ç¨æ·çç²ä¸ï¼ä»å½åç²ä¸ä¸æåæ¶åºå¹¶æå®åæ¾å¨tweetlocationåå¸è¡¨ä¸ãéè¦æ³¨æçæ¯ï¼ä½ å è¦æ£æµè¯¥é®å¼æ¯å¦å·²ç»åå¨äºåå¸è¡¨ä¸ï¼è¥æ¯çè¯ï¼å¢å 该é®å¼ç计æ°ãä½ è¿å¯ä»¥è®°ä½æææ¶åºç个æ°ï¼ä»¥ç¨äºåé¢çç¾åæ¯ç计ç®ã
è¿ä¸èæ¬çæåä¸é¨åå 容æ¯æé Google Pie ChartçURLï¼å建ä¸ä¸ªæ°çPieChartï¼æå®ä¸äºé项(大å°ãæ é¢åæ¯å¦ä¸º3Dç);æ¥çï¼éåæ¶åºåå¸è¡¨ï¼æ¾åºç¨äºå¾è¡¨çæ¶åºä¸²çæ°æ®(å å»&符å·)以å该æ¶åºè®¡æ°ä¸æ»æ°çç¾åæ¯ã
æ¸ å7.éè¿Twitterçæ¶åºæ¥æ建ä¸ä¸ªé¥¼å¾(followers-location.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" require 'google_chart' screen_name = String.new ARGV[0] tweetlocation = Hash.new timezones = 0.0 # Authenticate Twitter.configure do |config| config.consumer_key = '' config.consumer_secret = '' config.oauth_token = ' config.oauth_token_secret = '' end # Iterate followers, hash their location followers = Twitter.followers.users.each do |f| loc = f.time_zone.to_s if (loc.length > 0) if tweetlocation.has_key?(loc) tweetlocation[loc] = tweetlocation[loc] + 1 else tweetlocation[loc] = 1 end timezones = timezones + 1.0 end end # Create a pie chart GoogleChart::PieChart.new('650x350', "Time Zones", false ) do |pc| tweetlocation.each do |loc,count| pc.data loc.to_s.delete("&"), (count/timezones*100).round end puts pc.to_url end
è¥è¦æ§è¡æ¸ å7ä¸çèæ¬ï¼ç»å®æä¾ä¸ä¸ªTwitterçé¢æ¾ç¤ºå称ï¼ç¶åæå¾åºçURLæ·è´å¹¶ç²è´´å°æµè§å¨ä¸ï¼æ¸ å8ç»åºäºè¿ä¸è¿ç¨åæç»çæçURLã
æ¸ å8. è°ç¨followers-locationèæ¬(ç»æåªæ¯ä¸è¡æ¥ç)
$ ./followers-location.rb MTimJones http://chart.apis.google.com/chart?chl=Seoul|Santiago|Paris|Mountain+Time+(US++Canada)| Madrid|Central+Time+(US++Canada)|Warsaw|Kolkata|London|Pacific+Time+(US++Canada)| New+Delhi|Pretoria|Quito|Dublin|Moscow|Istanbul|Taipei|Casablanca|Hawaii|Mumbai| International+Date+Line+West|Tokyo|Ulaan+Bataar|Vienna|Osaka|Alaska|Chennai|Bern| Brasilia|Eastern+Time+(US++Canada)|Rome|Perth|La+Paz &chs=650x350&chtt=Time+Zones&chd=s:KDDyKcKDOcKDKDDDDDKDDKDDDDOKK9DDD&cht=p $
ææ¸ å8ä¸çURLç²è´´å°æµè§å¨ä¸ï¼ä½ å°±å¯ä»¥å¾å°å¾1æ示çç»æã
å¾1. Twitterç²ä¸ä½ç½®åå¸ç饼å¾
Twitterç¨æ·çè¡ä¸º
Twitterå å«äºå¤§éçæ°æ®ï¼ä½ å¯ä»¥éè¿ææè¿äºæ°æ®æ¥äºè§£ç¨æ·è¡ä¸ºçæäºè¦ç´ ã两个ç®åä¾åå°ç¨æ¥åæTwitterç¨æ·ä½æ¶åå¸æ¶æ¯ä»¥åéè¿ä»ä¹åºç¨æ¥åå¸æ¶æ¯ï¼ä½ å¯ä»¥ä½¿ç¨ä¸é¢ä¸¤ä¸ªç®åçèæ¬æ¥æå并å¯è§åè¿äºä¿¡æ¯ã
æ¸ å9ç»åºçèæ¬éåäºæ个ç¹å®ç¨æ·çtweetæ¶æ¯(使ç¨user_timelineæ¹æ³)ï¼ç¶åä»æ¯æ¡tweetæ¶æ¯ä¸æååºè¯¥tweetæ¶æ¯å½¢æçå ·ä½æ¶é´ï¼ä¸ä¸ªç®åçåå¸è¡¨è¢«å次ç¨æ¥ç»è®¡ä¸å¨ä¸æ¯å¤©ç计æ°ï¼ç¶å以ä¸åé¢çæ¶åºä¾åç¸ç±»ä¼¼çæ¹å¼ä½¿ç¨Google Chartsçæä¸ä¸ªæ±ç¶å¾ã
æ¸ å9. æ建tweetåå¸æ¶é´çæ±ç¶å¾(tweet-days.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" require "google_chart" screen_name = String.new ARGV[0] dayhash = Hash.new timeline = Twitter.user_timeline(screen_name, :count => 200 ) timeline.each do |t| tweetday = t.created_at.to_s[0..2] if dayhash.has_key?(tweetday) dayhash[tweetday] = dayhash[tweetday] + 1 else dayhash[tweetday] = 1 end end GoogleChart::BarChart.new('300x200', screen_name, :vertical, false) do |bc| bc.data "Sunday", [dayhash["Sun"]], '00000f' bc.data "Monday", [dayhash["Mon"]], '0000ff' bc.data "Tuesday", [dayhash["Tue"]], '00ff00' bc.data "Wednesday", [dayhash["Wed"]], '00ffff' bc.data "Thursday", [dayhash["Thu"]], 'ff0000' bc.data "Friday", [dayhash["Fri"]], 'ff00ff' bc.data "Saturday", [dayhash["Sat"]], 'ffff00' puts bc.to_url end
å¾2æä¾äºä½¿ç¨developerWorkså¸æ·æ¥æ§è¡æ¸ å9ä¸çtweet-daysèæ¬çç»æï¼æ£å¦æå±ç¤ºçé£æ ·ï¼ææä¸æ¯åå¸tweetæ¶æ¯ææ´»è·çä¸æ¡ï¼æä¸æ´»è·çæ¯ææå åææ天ã
å¾2. æ¯è¾æ¯å¤©çtweetæ´»è·ç¨åº¦çæ±ç¶å¾
æ¥ä¸æ¥çèæ¬ç¡®å®ç¹å®ç¨æ·éè¿åªä¸ä¸ªæ¥æºåå¸tweetæ¶æ¯ï¼ä½ å¯ä»¥éè¿å ç§æ¹å¼æ¥åå¸æ¶æ¯ï¼è¿ä¸èæ¬å¹¶æªå¯¹æ¯ä¸ç§é½åäºç¼ç ãå¦æ¸ å10æ示ï¼ä½¿ç¨ç±»ä¼¼ç模å¼æ¥æåç»å®ç¨æ·çæ¶é´è½´ä¸çå 容ï¼ç¶åå°è¯ç解ç åå¸è¡¨ä¸çtweetæ¶æ¯çååºå¤ãç¨åè¿ä¸åå¸è¡¨ä¼è¢«ç¨æ¥å建ä¸ä¸ªç®åç饼å¾ï¼ä½¿ç¨Google Chartsæ¥å¯è§åæ°æ®ã
æ¸ å10. æ建ç¨æ·çtweetæ¶æ¯æºç饼å¾(tweet-source.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" require 'google_chart' screen_name = String.new ARGV[0] tweetsource = Hash.new timeline = Twitter.user_timeline(screen_name, :count => 200 ) timeline.each do |t| if (t.source.rindex('blackberry')) then src = 'Blackberry' elsif (t.source.rindex('snaptu')) then src = 'Snaptu' elsif (t.source.rindex('tweetmeme')) then src = 'Tweetmeme' elsif (t.source.rindex('android')) then src = 'Android' elsif (t.source.rindex('LinkedIn')) then src = 'LinkedIn' elsif (t.source.rindex('twitterfeed')) then src = 'Twitterfeed' elsif (t.source.rindex('twitter.com')) then src = 'Twitter.com' else src = t.source end if tweetsource.has_key?(src) tweetsource[src] = tweetsource[src] + 1 else tweetsource[src] = 1 end end GoogleChart::PieChart.new('320x200', "Tweet Source", false) do |pc| tweetsource.each do|source,count| pc.data source.to_s, count end puts "\nPie Chart" puts pc.to_url end
å¾3æä¾äºä¸ä¸ªå¯è§åå¾è¡¨ï¼æ¾ç¤ºäºä¸ç»Twitterç¨æ·æå ´è¶£ä½¿ç¨çtweetæ¶æ¯åå¸æºï¼ä¼ ç»çTwitterç½ç«æ常ç¨å°ï¼éåæ¯ç§»å¨çµè¯åºç¨ã
å¾3. Twitterç¨æ·çtweetæ¶æ¯åå¸æºç饼å¾
åæ ç²ä¸æ åµçå¾è¡¨
Twitteræ¯ä¸ä¸ªåºå¤§çç¨æ·ç½ç»ï¼è¿äºç¨æ·å½¢æäºä¸ä¸ªç½ç»å¾ãæ£å¦ä½ éè¿èæ¬æçå°çé£æ ·ï¼éåä½ çè系人å¾å®¹æï¼æ¥çéåä»ä»¬çè系人ä¹å¾å®¹æãå³ä½¿åªæ¯å¨è¿ä¸çº§å«ä¸ï¼è¿æ ·åä¹å·²å»ºç«èµ·ä¸å¼ 大å¾çåºæ¬é¢ã
为äºå¯è§åå¾å½¢ï¼æéæ©ä½¿ç¨å¾å½¢å¯è§å软件GraphVizãå¨Ubuntuä¸ï¼ä½¿ç¨ä¸é¢çå½ä»¤è¡ï¼ä½ å¯ä»¥å¾å®¹æå°±å®è£ 好è¿ä¸å·¥å ·ï¼
$ sudo apt-get install graphviz
æ¸ å11ä¸çèæ¬éåäºç¨æ·çç²ä¸ï¼ç¶ååéåè¿äºç²ä¸ä»ä»¬çç²ä¸ãè¿ä¸æ¨¡å¼ä¸çå¯ä¸çæ£ä¸åä¹å¤å¨äº GraphVizçDOTæ ¼å¼æ件çæé ï¼GraphViz使ç¨ç®åçèæ¬æ ¼å¼æ¥å®ä¹å¾å½¢ï¼è¿äºå¾å½¢ä½ä¸ºä½ å举çTwitterç¨æ·çç»æé¨ååºç°ãå¦æ¸ åæå±ç¤ºçé£æ ·ï¼å¯ä»¥ç®åå°éè¿æå®èç¹çå ³ç³»æ¥å®ä¹å¾å½¢ã
æ¸ å11. å¯è§åTwitterç²ä¸å¾(followers-graph.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" require 'google_chart' screen_name = String.new ARGV[0] tweetlocation = Hash.new # Authenticate Twitter.configure do |config| config.consumer_key = '' config.consumer_secret = '' config.oauth_token = '' config.oauth_token_secret = '' end my_file = File.new("graph.dot", "w") my_file.puts "graph followers {" my_file.puts " node [ fontname=Arial, fontsize=6 ];" # Iterate followers, hash their location followers = Twitter.followers(screen_name, :count=>10 ).users.each do |f| # Only iterate if we can see their followers if (f.protected.to_s != "true") my_file.puts " \"" + screen_name + "\" -- \"" + f.screen_name.to_s + "\"" followers2 = Twitter.followers(f.screen_name, :count =>10 ).users.each do |f2| my_file.puts " \"" + f.screen_name.to_s + "\" -- \"" + f2.screen_name.to_s + "\"" end end end my_file.puts "}"
å¨æ个ç¨æ·ä¸æ§è¡æ¸ å11çèæ¬ï¼å¾åºçç»ææ¾å¨ä¸ä¸ªdotæ件ä¸ï¼æ¥ç使ç¨GraphVizæ¥çæå¾åãé¦å è°ç¨Rubyèæ¬æ¥æ¶éå¾å½¢æ°æ®(åå¨ægraph.dot);æ¥ç使ç¨GraphVizæ¥çæå¾å(è¿éç¨çæ¯circoï¼è¯¥å·¥å ·æå®äºä¸ç§åå½¢çå¸å±)ãçæå¾åçè¿ç¨å®ä¹å¦ä¸ï¼
$ ./followers-graph.rb MTimJones $ circo graph.dot -Tpng -o graph.png
æç»å¾åºçå¾åå¦å¾4æ示ãéè¦æ³¨æçæ¯ï¼ç±äºè¿ä¸Twitterå¾è¡¨å¯è½ä¼å¾å¤§ï¼å 为æ们éè¿å°½éåå°è¦å举çç¨æ·åå ¶ç²ä¸çæ°ç®æ¥éå¶å¾è¡¨çè§æ¨¡(éè¿æ¸ å11ä¸ç:counté项)ã
å¾4. Twitterç²ä¸å¾ä¾å(æ端æ åµçåé)
ä½ç½®ä¿¡æ¯
å¨åè½å¯ç¨çæ åµä¸ï¼Twitteræ¶éä½ åä½ ååºtweetæ¶æ¯çå°çä½ç½®æ°æ®ãè¿äºç±ç»åº¦å纬度信æ¯ç»æçæ°æ®è½å¤ç¨æ¥ç²¾ç¡®å°å®ä½ç¨æ·çä½ç½®æè æ¯tweetæ¶æ¯ä»ä½å¤ååºãæ¤å¤ï¼ææç´¢åè¿äºæ¶æ¯ç»åå¨ä¸èµ·ï¼ä½ å°±å¯ä»¥æ ¹æ®å·²å®ä¹çä½ç½®ææ¯ä½ æå¤çä½ç½®æ¥è¯å«åºæäºå°æ¹ææ¯æäºäººã
并éææçç¨æ·ææ¯tweetæ¶æ¯é½å¯ç¨äºå°çä½ç½®åè½(åºäºéç§åå )ï¼ä½è¿ä¸ä¿¡æ¯æ¯ææTwitterä½éªå½ä¸çä¸ä¸ªé常å¸å¼äººçæ¹é¢ã让æ们æ¥çä¸ä¸ªèæ¬ï¼è¯¥èæ¬å è®¸ä½ å¯è§åå°çå®ä½æ°æ®ï¼è¿æå¦ä¸ä¸ªèæ¬å è®¸ä½ ä½¿ç¨è¿ä¸æ°æ®æ¥è¿è¡æç´¢ã
第ä¸ä¸ªèæ¬(æ¸ å12æ示)æåç¨æ·çç»åº¦å纬度æ°æ®(åæ³ä¸ä¸æ¸ å2ä¸çè¾¹çæ¡(bounding box))ï¼å°½ç®¡è¾¹çæ¡æ¯ä¸ä¸ªå®ä¹ä»£è¡¨äºç¨æ·åºåçå¤è¾¹å½¢ï¼ä½æåäºç®åï¼åªç¨äºè¿ä¸åºåä¸çä¸ä¸ªç¹ãæäºè¿ä¸æ°æ®ï¼æå¨ä¸ä¸ªç®åçHMTLæ件ä¸çæäºä¸ä¸ªç®åçJavaScriptå½æ°ï¼è¿ä¸JavaScript代ç ä¸Google Mapsæ¥å£ï¼ç»åºäºè¿ä¸ä½ç½®çä¸ä¸ªä¿¯è§å°å¾(ç»åºæåèªTwitterç¨æ·çç»åº¦å纬度æ°æ®)ã
æ¸ å12. æé ç¨æ·å°å¾çRubyèæ¬(where-am-i.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" Twitter.configure do |config| config.consumer_key = 'config.consumer_secret = ' config.oauth_token = ' config.oauth_token_secret = ' end screen_name = String.new ARGV[0] a_user = Twitter.user(screen_name) if a_user.geo_enabled == true long = a_user.status.place.bounding_box.coordinates[0][0][0]; lat = a_user.status.place.bounding_box.coordinates[0][0][1]; my_file = File.new("test.html", "w") my_file.puts " my_file.puts " my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "" my_file.puts "
" else puts "no geolocation data available." end
æ¸ å12ä¸çèæ¬æ§è¡èµ·æ¥å¾ç®åï¼
$ ./where-am-i.rb MTimJones
æç»å¾åºçHTMLæ件å¯ä»¥éè¿æµè§å¨æ¥æ¸²æï¼åè¿æ ·ï¼
$ firefox test.html
å¦æ没æå¯ç¨çä½ç½®ä¿¡æ¯çè¯ï¼è¿ä¸èæ¬å°±ä¼æ§è¡å¤±è´¥;ä¸è¿å¦ææ§è¡æåçè¯ï¼å°±ä¼çæä¸ä¸ªHTMLæ件ï¼æµè§å¨å¯ä»¥è¯»å ¥è¯¥æ件æ¥æ¸²æåºå°å¾ãå¾5ç»åºäºæç»çæçå°å¾å¾åï¼å ¶æ¾ç¤ºäºç¾å½ç§ç½æå¤å·åé¨Front Rangeçé¨åå°åºã
å¾5. æ¸ å12ä¸çèæ¬æ¸²æå¾åçä¾å
æäºå°çä½ç½®ï¼ä½ è¿å¯ä»¥éè¿æTwitteræ¥è¯å«åºä¸æ个ç¹å®ä½ç½®ç¸å ³èçTwitterç¨æ·åtweetæ¶æ¯ãTwitteræç´¢APIå 许使ç¨å°çç¼ç ä¿¡æ¯æ¥éå®æç´¢ç»æãä¸é¢æ¸ å13ä¸çä¾åæåç¨æ·çç»åº¦å纬度ï¼ç¶å使ç¨è¿äºæ°æ®è·å该ä½ç½®æ¹å5å ¬éä¹å çæætweetæ¶æ¯ã
æ¸ å13. 使ç¨ç»åº¦å纬度æ°æ®æ¥æç´¢å±é¨å°åºçtweetæ¶æ¯(tweets-local.rb)
#!/usr/bin/env ruby require "rubygems" require "twitter" Twitter.configure do |config| config.consumer_key = '' config.consumer_secret = '' config.oauth_token = '' config.oauth_token_secret = '' end screen_name = String.new ARGV[0] a_user = Twitter.user(screen_name) if a_user.geo_enabled == true long = a_user.status.place.bounding_box.coordinates[0][0][0] lat = a_user.status.place.bounding_box.coordinates[0][0][1] Array tweets = Twitter::Search.new.geocode(lat, long, "5mi").fetch tweets.each do |t| puts t.from_user + " | " + t.text end end
æ¸ å13ä¸çèæ¬çæ§è¡ç»ææ¾ç¤ºå¨æ¸ å14ä¸ï¼è¿æ¯æå®ä½ç½®èå´å çTwitterç¨æ·åå¸çtweetæ¶æ¯çä¸ä¸ªåéã
æ¸ å14. æ¥è¯¢ææå¨ä½ç½®æ¹å5å ¬éä¹å çè¿ä¸å±é¨å°åºçtweetæ¶æ¯ã
$ ./tweets-local.rb MTimJones Breesesummer | @DaltonOls did he answer u LongmontRadMon | 60 CPM, 0.4872 uSv/h, 0.6368 uSv/h, 2 time(s) over natural radiation graelston | on every street there is a memory; a time and place we can never be again. Breesesummer | #I'minafight with @DaltonOls to see who will marry @TheCodySimpson I will marry him!!! :/ _JennieJune_ | ok I'm done, goodnight everyone! Breesesummer | @DaltonOls same _JennieJune_ | @sylquejr sleep well! Breesesummer | @DaltonOls ok let's see what he says LongmontRadMon | 90 CPM, 0.7308 uSv/h, 0.7864 uSv/h, 2 time(s) over natural radiation Breesesummer | @TheCodySimpson would u marry me or @DaltonOls natcapsolutions | RT hlovins: The scientific rebuttal to the silly Forbes release this morning: Misdiagnosis of Surface Temperatu... http://bit.ly/nRpLJl $
继ç»æ·±å ¥
æ¬æç»åºäºä¸äºç®åçèæ¬ï¼è¿äºèæ¬ä½¿ç¨Rubyè¯è¨æ¥ä»Twitterä¸æåæ°æ®ï¼æç« çéç¹æ¾å¨ç¨æ¥è¯´æä¸äºåºæ¬æ¦å¿µçèæ¬çå¼ååæ¼ç¤ºæ¹é¢ï¼ä½æ们è½åçè¿ä¸æ¢äºæ¤ãä¾å¦ï¼ä½ è¿å¯ä»¥ä½¿ç¨APIæ¥æµè§ä½ çæåçç½ç»ï¼å¹¶æ¾åºä½ æå ´è¶£çæå欢è¿çTwitterç¨æ·ãå¦ä¸ä¸ªå¸å¼äººçæ¹é¢æ¯å¯¹tweetæ¶æ¯æ¬èº«è¿è¡ææï¼ä½¿ç¨å°çä½ç½®æ°æ®æ¥äºè§£åºäºä½ç½®çè¡ä¸ºææ¯äºä»¶(æ¯å¦è¯´æµææ´å)ãæ¬æåªæ¯åäºä¸ä¸ªæµ æ¾çä»ç»ï¼è¯·å¨ä¸é¢éæåè¡¨ä½ çè¯è®ºï¼éä¸ä½ èªå·±çåç§æ··æç¨åºãRubyåTwitter gemæ为æ°æ®ææéè¦å¼åæç¨çæ··æåºç¨ç¨åºå仪表çå·¥å ·çå·¥ä½åå¾å¾å®¹æã
以ä¸å°±æ¯ç±ç«ææ¯é¢éå°ç¼ä¸ºå¤§å®¶å¸¦æ¥äºRubyå¼åä¸Twitteræ°æ®ç详ç»æ¥éª¤ï¼ä¸ç¥éä½ æ没æç¨å¾ä¸å¢ï¼å¤§å®¶åªè¦æçå°ç¼çæå¼è¿è¡æä½å°±å¯ä»¥äºã
上一篇:初学Ruby开发的模式详解
下一篇:详解Ruby集合的规则及参数设置