【问题标题】:Pulling Images from User's Timeline with Twitter Gem使用 Twitter Gem 从用户的时间轴中提取图像
【发布时间】:2013-06-11 16:29:06
【问题描述】:

我希望用户能够“选择”他们之前通过 Twitter 上传的图片。

我正在使用 Sferik 的 Twitter Gem,但在阅读了文档之后,我并没有进一步了解。

当我调用经过身份验证的用户的时间线时,我可以看到一些推文具有如下媒体对象:

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"}>

如何通过 Twitter gem 提取其中几个媒体 URL?

【问题讨论】:

    标签: ruby twitter twitter-gem


    【解决方案1】:

    我不知道您目前如何使用 gem 做到这一点,但您可以使用 .each 循环过滤结果:

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

    这种方法的问题

    您每次都在循环整个数组(效率不高)。我正在查看源代码,看看是否可以做任何事情来过滤结果,从而不需要循环


    更新

    来自我们一款应用的工作代码:

    在助手中:

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

    在视图中:

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

    更多选项

    Twitter Gem's Github page有更多选择

    【讨论】:

      【解决方案2】:

      我最终做了这样的事情。 API 仅返回用户的 3,200 条最新推文。

      require"twitter"
      
      client=Twitter::REST::Client.new{|config|
        config.consumer_key=""
        config.consumer_secret=""
        config.access_token=""
        config.access_token_secret=""
      }
      
      user="username"
      max=nil
      
      while true
        options={count:200}
        options[:max_id]=max if max
        timeline=client.user_timeline(user,options)
        break if timeline.empty?
        timeline.each{|tweet|
          tweet.media.each{|media|
            puts media.media_url.to_s
          }
        }
        max=timeline.last.id-1
      end
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-07-18
        • 2013-03-10
        • 2014-07-05
        • 2023-01-12
        • 2021-03-02
        • 2012-02-23
        • 2020-12-21
        相关资源
        最近更新 更多