twitter

[~/local/src/ruby/rubytesting/twitter-test(master)]$ pry
[1] pry(main)> require "twitter"
=> true
[3] pry(main)> require "./configure"
=> true
Veanse en la sección 8.15 los contenidos del fichero configure.rb

[4] pry(main)> Twitter.user_timeline("timoreilly").first
=> #<Twitter::Tweet:0x007fd824025c70
 @attrs=
  {:created_at=>"Wed Sep 18 01:08:06 +0000 2013",
   :id=>380136112389234690,
   :id_str=>"380136112389234690",
   :text=>
    "@isaiah_saxon_ If you want an intro to the folks there, I'd be glad to make it.",
   :source=>
    "<a href=\"http://www.tweetdeck.com\" rel=\"nofollow\">TweetDeck</a>",
   :truncated=>false,
   :in_reply_to_status_id=>380135816275578881,
   :in_reply_to_status_id_str=>"380135816275578881",
   :in_reply_to_user_id=>432803063,
   :in_reply_to_user_id_str=>"432803063",
   :in_reply_to_screen_name=>"isaiah_saxon_",
   :user=>
    {:id=>2384071,
     :id_str=>"2384071",
     :name=>"Tim O'Reilly",
     :screen_name=>"timoreilly",
     :location=>"Sebastopol, CA",
     :description=>
      "Founder and CEO, O'Reilly Media. Watching the alpha geeks, sharing their stories, helping the future u
     :url=>"http://t.co/5086iX7oyT",
     :entities=>
      {:url=>
        {:urls=>
          [{:url=>"http://t.co/5086iX7oyT",
            :expanded_url=>"http://radar.oreilly.com",
            :display_url=>"radar.oreilly.com",
            :indices=>[0, 22]}]},
            :description=>{:urls=>[]}},
            :protected=>false,
            :followers_count=>1723740,
            :friends_count=>1105,
            :listed_count=>25791,
            :created_at=>"Tue Mar 27 01:14:05 +0000 2007",
            :favourites_count=>143,
            :utc_offset=>-25200,
            :time_zone=>"Pacific Time (US & Canada)",
            :geo_enabled=>true,
            :verified=>true,
            :statuses_count=>27026,
            :lang=>"en",
            :contributors_enabled=>false,
            :is_translator=>false,
            :profile_background_color=>"9AE4E8",
            :profile_background_image_url=>
             "http://a0.twimg.com/profile_background_images/3587880/notes.gif",
            :profile_background_image_url_https=>
             "https://si0.twimg.com/profile_background_images/3587880/notes.gif",
            :profile_background_tile=>false,
            :profile_image_url=>
             "http://a0.twimg.com/profile_images/2823681988/f4f6f2bed8ab4d5a48dea4b9ea85d5f1_normal.jpeg",
            :profile_image_url_https=>
             "https://si0.twimg.com/profile_images/2823681988/f4f6f2bed8ab4d5a48dea4b9ea85d5f1_normal.jpeg",
            :profile_link_color=>"0000FF",
            :profile_sidebar_border_color=>"87BC44",
            :profile_sidebar_fill_color=>"E0FF92",
            :profile_text_color=>"000000",
            :profile_use_background_image=>true,
            :default_profile=>false,
            :default_profile_image=>false,
            :following=>nil,
            :follow_request_sent=>false,
            :notifications=>nil},
            :geo=>nil,
            :coordinates=>nil,
            :place=>nil,
            :contributors=>nil,
            :retweet_count=>0,
            :favorite_count=>0,
            :entities=>
             {:hashtags=>[],
              :symbols=>[],
              :urls=>[],
              :user_mentions=>
               [{:screen_name=>"isaiah_saxon_",
                 :name=>"Isaiah Saxon",
                 :id=>432803063,
                 :id_str=>"432803063",
                 :indices=>[0, 14]}]},
            :favorited=>false,
            :retweeted=>false,
             
            ... etc., etc.
            :profile_sidebar_border_color=>"87BC44",
            :profile_sidebar_fill_color=>"E0FF92",
            :profile_text_color=>"000000",
            :profile_use_background_image=>true,
            :default_profile=>false,
            :default_profile_image=>false,
            :following=>nil,
            :follow_request_sent=>false,
            :notifications=>nil},
            :geo=>nil,
            :coordinates=>nil,
            :place=>nil,
            :contributors=>nil,
            :retweet_count=>0,
            :favorite_count=>0,
            :entities=>
             {:hashtags=>[],
              :symbols=>[],
              :urls=>[],
              :user_mentions=>
               [{:screen_name=>"isaiah_saxon_",
                 :name=>"Isaiah Saxon",
                 :id=>432803063,
                 :id_str=>"432803063",
                 :indices=>[0, 14]}]},
            :favorited=>false,
            :retweeted=>false,
            :lang=>"en"}>

[5] pry(main)> Twitter.user("timoreilly").location
=> "Sebastopol, CA"

[10] pry(main)> Twitter.search("#ruby -rt", :lang => "es", :count => 1)
=> #<Twitter::SearchResults:0x007fd8249c2d50
 @attrs=
  {:statuses=>
    [{:metadata=>{:result_type=>"recent", :iso_language_code=>"es"},
      :created_at=>"Wed Sep 18 07:15:41 +0000 2013",
      :id=>380228616136556544,
      :id_str=>"380228616136556544",
      :text=>"Uf mucho mas :) #Ruby.",
      :source=>"web",
      :truncated=>false,
      :in_reply_to_status_id=>nil,
      :in_reply_to_status_id_str=>nil,
      :in_reply_to_user_id=>nil,
      :in_reply_to_user_id_str=>nil,
      :in_reply_to_screen_name=>nil,
      :user=>
       {:id=>1476069577,
        :id_str=>"1476069577",
        :name=>"Teresa Bautista",
        :screen_name=>"teressabautista",
        :location=>"Obregón, Sonora",
        :description=>
         "no tengo idea si voy a volver a verte, pero por lo pronto contenta de conocerte",
        :url=>nil,
        :entities=>{:description=>{:urls=>[]}},
        :protected=>false,
        :followers_count=>21,
        :friends_count=>21,
        ...  etc, etc.

[11] pry(main)> Twitter.search("#ruby -rt", :lang => "es", :count => 1).results.first.text
=> "Uf mucho mas :) #Ruby."
[12] pry(main)> Twitter.search("#ruby -rt", :lang => "es", :count => 1).results.first.to_s
=> "#<Twitter::Tweet:0x007fd825070828>"
Véase la documentación en Twitter::API::Search. Para ver como usar la API de búsqueda de Twitter véanse
  1. Using the Twitter Search API.
  2. GET search/tweets

Hay un número de operadores que podemos usar en nuestra query:

Example Finds tweets...
twitter search containing both "twitter" and "search". This is the default operator
"happy hour" containing the exact phrase "happy hour"
love OR hate containing either "love" or "hate" (or both)
beer -root containing "beer" but not "root"
#haiku containing the hashtag "haiku"
from:twitterapi sent from the user @twitterapi
to:twitterapi sent to the user @twitterapi
place:opentable:2 about the place with OpenTable ID 2
place:247f43d441defc03 about the place with Twitter ID 247f43d441defc03
@twitterapi mentioning @twitterapi
superhero since:2011-05-09 containing "superhero" and sent since date "2011-05-09" (year-month-day).
twitterapi until:2011-05-09 containing "twitterapi" and sent before the date "2011-05-09".
movie -scary :) containing "movie", but not "scary", and with a positive attitude.
flight :( containing "flight" and with a negative attitude.
traffic ? containing "traffic" and asking a question.
hilarious filter:links containing "hilarious" and with a URL.
news source:tweet_button containing "news" and entered via the Tweet Button

Anatomía de un usuario Twitter

[13] pry(main)> Twitter.user('timoreilly')
=> #<Twitter::User:0x007fd8260c1448
 @attrs=
  {:id=>2384071,
   :id_str=>"2384071",
   :name=>"Tim O'Reilly",
   :screen_name=>"timoreilly",
   :location=>"Sebastopol, CA",
   :description=>
    "Founder and CEO, O'Reilly Media. Watching the alpha geeks, sharing their stories, helping the future unf
   :url=>"http://t.co/5086iX7oyT",
   :entities=>
    {:url=>
      {:urls=>
        [{:url=>"http://t.co/5086iX7oyT",
          :expanded_url=>"http://radar.oreilly.com",
          :display_url=>"radar.oreilly.com",
          :indices=>[0, 22]}]},
     :description=>{:urls=>[]}},
   :protected=>false,
   :followers_count=>1723753,
   :friends_count=>1105,
   :listed_count=>25791,
   :created_at=>"Tue Mar 27 01:14:05 +0000 2007",
   :favourites_count=>143,
   :utc_offset=>-25200,
   :time_zone=>"Pacific Time (US & Canada)",
   :geo_enabled=>true,
   :verified=>true,
   :statuses_count=>27026,
   :lang=>"en",
   :status=>
    {:created_at=>"Wed Sep 18 01:08:06 +0000 2013",
     :id=>380136112389234690,
     :id_str=>"380136112389234690",
     :text=>
      "@isaiah_saxon_ If you want an intro to the folks there, I'd be glad to make it.",
     :source=>
      "<a href=\"http://www.tweetdeck.com\" rel=\"nofollow\">TweetDeck</a>",
     :truncated=>false,
     :in_reply_to_status_id=>380135816275578881,
     :in_reply_to_status_id_str=>"380135816275578881",
     :in_reply_to_user_id=>432803063,
     :in_reply_to_user_id_str=>"432803063",
     :in_reply_to_screen_name=>"isaiah_saxon_",
     :geo=>nil,
     :coordinates=>nil,
     :place=>nil,
     :contributors=>nil,
     :retweet_count=>0,
     :favorite_count=>0,
     :entities=>
      {:hashtags=>[],
       :symbols=>[],
       :urls=>[],
       :user_mentions=>
        [{:screen_name=>"isaiah_saxon_",
          :name=>"Isaiah Saxon",
          :id=>432803063,
          :id_str=>"432803063",
          :indices=>[0, 14]}]},
     :favorited=>false,
     :retweeted=>false,
     :lang=>"en"},
   :contributors_enabled=>false,
   :is_translator=>false,
   :profile_background_color=>"9AE4E8",
   :profile_background_image_url=>
    "http://a0.twimg.com/profile_background_images/3587880/notes.gif",
   :profile_background_image_url_https=>
    "https://si0.twimg.com/profile_background_images/3587880/notes.gif",
   :profile_background_tile=>false,
   :profile_image_url=>
    "http://a0.twimg.com/profile_images/2823681988/f4f6f2bed8ab4d5a48dea4b9ea85d5f1_normal.jpeg",
   :profile_image_url_https=>
    "https://si0.twimg.com/profile_images/2823681988/f4f6f2bed8ab4d5a48dea4b9ea85d5f1_normal.jpeg",
   :profile_link_color=>"0000FF",
   :profile_sidebar_border_color=>"87BC44",
   :profile_sidebar_fill_color=>"E0FF92",
   :profile_text_color=>"000000",
   :profile_use_background_image=>true,
   :default_profile=>false,
   :default_profile_image=>false,
   :following=>true,
   :follow_request_sent=>false,
   :notifications=>false}>

Véase También

Véase
  1. https://github.com/crguezl/twitter-test los fuentes de un ejemplo usando twitter
  2. twitter gem documentación de la gema twitter
  3. En este enlace registramos nuestra aplicación en Twitter: Register your application in twitter
  4. Un artículo sobre como usar la gema twitter: Data mining with Ruby and Twitter The interesting side to a Twitter API por Tim Jones en IBM developerWorks
  5. Hay una versión de esta gema que provee un programa llamado t que nos da una interfaz de línea de comandos para acceder a twitter.
  6. Una gema relacionada: TweetStream es una gema que proporciona acceso a la API de Twitter para streaming



Subsecciones
Casiano Rodriguez León 2015-01-07