Search code examples
rubytwittertwitter-gem

Pulling Images from User's Timeline with Twitter Gem


I would like user's to be able to "choose" an image that they've previously uploaded via Twitter.

I am using the Twitter Gem by Sferik, but after reading the documentation I haven't gotten much further.

When I make a call to the authenticated user's timeline, I can see some tweets have a media object like so:

Twitter.home_timeline.first
    => #<Twitter::Tweet:0x007fe8eaaf4738 @attrs={
:created_at=>"Fri Jun 14 22:24:19 +0000 2013",
 :id=>345668046926536704,
 :id_str=>"345668046926536704",
 :text=>"Testing some more WishPanda. Check out the logo made by Becky Peng! http://t.co/rmD56WhO3r",
 :source=>"<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
 :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=>216810199,
 :id_str=>"216810199",
 :name=>"Doug",
 :screen_name=>"FloofyDoug",
 :location=>"Brazil",
 :description=>"It's so floofy!",
 :url=>""removed because i couldn't post more than two links",
 :entities=>{
:url=>{
:urls=>[{
:url=>""removed because i couldn't post more than two links"",
 :expanded_url=>"http://floofydoug.com",
 :display_url=>"floofydoug.com",
 :indices=>[0, 20]}]},
 :description=>{
:urls=>[]}},
 :protected=>false,
 :followers_count=>59,
 :friends_count=>77,
 :listed_count=>0,
 :created_at=>"Wed Nov 17 19:49:38 +0000 2010",
 :favourites_count=>107,
 :utc_offset=>-18000,
 :time_zone=>"Eastern Time (US & Canada)",
 :geo_enabled=>true,
 :verified=>false,
 :statuses_count=>243,
 :lang=>"en",
 :contributors_enabled=>false,
 :is_translator=>false,
 :profile_background_color=>"ACDED6",
 :profile_background_image_url=>""removed because i couldn't post more than two links"",
 :profile_background_image_url_https=>""removed because i couldn't post more than two links"",
 :profile_background_tile=>false,
 :profile_image_url=>""removed because i couldn't post more than two links"",
 :profile_image_url_https=>""removed because i couldn't post more than two links"",
 :profile_banner_url=>""removed because i couldn't post more than two links"",
 :profile_link_color=>"038543",
 :profile_sidebar_border_color=>"EEEEEE",
 :profile_sidebar_fill_color=>"F6F6F6",
 :profile_text_color=>"333333",
 :profile_use_background_image=>true,
 :default_profile=>false,
 :default_profile_image=>false,
 :following=>true,
 :follow_request_sent=>nil,
 :notifications=>nil},
 :geo=>nil,
 :coordinates=>nil,
 :place=>nil,
 :contributors=>nil,
 :retweet_count=>0,
 :favorite_count=>0,
 :entities=>{
:hashtags=>[],
 :symbols=>[],
 :urls=>[],
 :user_mentions=>[],
 :media=>[{
:id=>345668046930731009,
 :id_str=>"345668046930731009",
 :indices=>[68, 90],
 :media_url=>"http://pbs.twimg.com/media/BMwPQduCYAEsnaA.jpg",
 :media_url_https=>""removed because i couldn't post more than two links"",
 :url=>"removed because i couldn't post more than two links",
 :display_url=>""removed because i couldn't post more than two links",
 :expanded_url=>""removed because i couldn't post more than two links"",
 :type=>"photo",
 :sizes=>{
:medium=>{
:w=>600,
 :h=>338,
 :resize=>"fit"},
 :small=>{
:w=>340,
 :h=>192,
 :resize=>"fit"},
 :thumb=>{
:w=>150,
 :h=>150,
 :resize=>"crop"},
 :large=>{
:w=>1024,
 :h=>577,
 :resize=>"fit"}}}]},
 :favorited=>false,
 :retweeted=>false,
 :possibly_sensitive=>false,
 :lang=>"en"}>

How can I pull several of these media URLs out via the Twitter gem?


Solution

  • I don't know how you can do that with the gem currently, but you could filter the results with an .each loop:

    @twitter.each do |tweet|
        if tweet.media_url.present? 
            image_tag(tweet.media_url)
        end
    end
    

    Problems with this method

    You're looping through the whole array every time (not very efficient). I am looking at the source to see if anything can be done to filter the results so that a loop isn't necessary


    Update

    Working code from one of our apps:

    In helper:

    def twitter_feed
        Twitter.user_timeline(21931601).take(5) #limits array to 5 items
    end
    

    In view:

    twitter_feed.each do |tweet|
        image_tag(tweet.media[0]["media_url"], :size => tweet.media[0]["sizes"]["thumb"]) if(tweet.media.present?)
    end
    

    More Options

    There are more options on Twitter Gem's Github page