简体   繁体   English

如何从Twitter API Java JSON获取所有url照片

[英]How to get all the url photos from Twitter API Java JSON

Anyone know if it's possible to get all the url photos from the twitter api. 任何人都知道是否可以从twitter api获取所有网址照片。 As an example this tweet has 4 photos https://twitter.com/wtfbaile/status/652694439584993280/photo/1 I want to get all the 4 url photos in one request on the twitter api. 作为一个例子,这条推文有4张照片https://twitter.com/wtfbaile/status/652694439584993280/photo/1我想在twitter api上的一个请求中获取所有4个url照片。

Currently in order to do that, I must do 2 requests. 目前为了做到这一点,我必须做2个请求。 The 4 url photos is only available in the second request. 4个网址照片仅在第二个请求中提供。

The first one : 第一个 :

Link1 链接1

Result : 结果:

{
  "statuses": [
    {
      "metadata": {
        "result_type": "recent",
        "iso_language_code": "en"
      },
      "in_reply_to_status_id_str": null,
      "in_reply_to_status_id": null,
      "created_at": "Sat Oct 10 03:57:35 +0000 2015",
      "in_reply_to_user_id_str": null,
      "source": "<a href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\">Twitter for iPhone<\/a>",
      "retweet_count": 1,
      "retweeted": false,
      "geo": null,
      "in_reply_to_screen_name": null,
      "is_quote_status": false,
      "id_str": "652694439584993280",
      "in_reply_to_user_id": null,
      "favorite_count": 6,
      "id": 6.5269443958499e+17,
      "text": "some cool pictures from time square, brooklyn bridge and china town http:\/\/t.co\/kv0OVmoMoI",
      "place": null,
      "lang": "en",
      "favorited": false,
      "possibly_sensitive": false,
      "coordinates": null,
      "truncated": false,
      "entities": {
        "urls": [

        ],
        "hashtags": [

        ],
        "media": [
          {
            "display_url": "pic.twitter.com\/kv0OVmoMoI",
            "indices": [
              68,
              90
            ],
            "sizes": {
              "small": {
                "w": 340,
                "h": 191,
                "resize": "fit"
              },
              "large": {
                "w": 1024,
                "h": 576,
                "resize": "fit"
              },
              "thumb": {
                "w": 150,
                "h": 150,
                "resize": "crop"
              },
              "medium": {
                "w": 600,
                "h": 337,
                "resize": "fit"
              }
            },
            "id_str": "652694417070006272",
            "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
            "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
            "id": 6.5269441707001e+17,
            "type": "photo",
            "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
            "url": "http:\/\/t.co\/kv0OVmoMoI"
          }
        ],
        "user_mentions": [

        ],
        "symbols": [

        ]
      },
      "contributors": null,
      "user": {
        "utc_offset": -10800,
        "friends_count": 112,
        "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/652216850751688704\/kC7rdEm__normal.png",
        "listed_count": 3,
        "profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/378800000046929850\/8b7537e055b6c558c461f49cb1a3ad39.png",
        "default_profile_image": false,
        "favourites_count": 21009,
        "description": "?? | 121614 ?? | phs '18 | ig: wtfbaile",
        "created_at": "Sat Jun 30 02:41:11 +0000 2012",
        "is_translator": false,
        "profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/378800000046929850\/8b7537e055b6c558c461f49cb1a3ad39.png",
        "protected": false,
        "screen_name": "wtfbaile",
        "id_str": "622435883",
        "profile_link_color": "9266CC",
        "is_translation_enabled": false,
        "id": 622435883,
        "geo_enabled": true,
        "profile_background_color": "C0DEED",
        "lang": "en",
        "has_extended_profile": true,
        "profile_sidebar_border_color": "FFFFFF",
        "profile_text_color": "333333",
        "verified": false,
        "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/652216850751688704\/kC7rdEm__normal.png",
        "time_zone": "Atlantic Time (Canada)",
        "url": null,
        "contributors_enabled": false,
        "profile_background_tile": true,
        "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/622435883\/1444187766",
        "entities": {
          "description": {
            "urls": [

            ]
          }
        },
        "statuses_count": 67181,
        "follow_request_sent": false,
        "followers_count": 600,
        "profile_use_background_image": true,
        "default_profile": false,
        "following": false,
        "name": "baile",
        "location": "410",
        "profile_sidebar_fill_color": "DDEEF6",
        "notifications": false
      }
    }
  ],
  "search_metadata": {
    "max_id_str": "652694439584993280",
    "since_id_str": "0",
    "query": "some+cool+pictures+from+time+square%2C+brooklyn+bridge+and+china+town+filter%3Aimages+exclude%3Aretweets",
    "count": 100,
    "max_id": 6.5269443958499e+17,
    "since_id": 0,
    "completed_in": 0.027,
    "refresh_url": "?since_id=652694439584993280&q=some%20cool%20pictures%20from%20time%20square%2C%20brooklyn%20bridge%20and%20china%20town%20filter%3Aimages%20exclude%3Aretweets&include_entities=1"
  }
}

The second one : 第二个 :

Link2 LINK2

Result : 结果:

    {
  "possibly_sensitive_appealable": false,
  "extended_entities": {
    "media": [
      {
        "display_url": "pic.twitter.com\/kv0OVmoMoI",
        "indices": [
          68,
          90
        ],
        "sizes": {
          "small": {
            "w": 340,
            "h": 191,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 576,
            "resize": "fit"
          },
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "medium": {
            "w": 600,
            "h": 337,
            "resize": "fit"
          }
        },
        "id_str": "652694417070006272",
        "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
        "id": 6.5269441707001e+17,
        "type": "photo",
        "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
        "url": "http:\/\/t.co\/kv0OVmoMoI"
      },
      {
        "display_url": "pic.twitter.com\/kv0OVmoMoI",
        "indices": [
          68,
          90
        ],
        "sizes": {
          "small": {
            "w": 340,
            "h": 255,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 768,
            "resize": "fit"
          },
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "medium": {
            "w": 600,
            "h": 450,
            "resize": "fit"
          }
        },
        "id_str": "652694417128747008",
        "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHT_W8AAgwUB.jpg",
        "id": 6.5269441712875e+17,
        "type": "photo",
        "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHT_W8AAgwUB.jpg",
        "url": "http:\/\/t.co\/kv0OVmoMoI"
      },
      {
        "display_url": "pic.twitter.com\/kv0OVmoMoI",
        "indices": [
          68,
          90
        ],
        "sizes": {
          "small": {
            "w": 340,
            "h": 255,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 768,
            "resize": "fit"
          },
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "medium": {
            "w": 600,
            "h": 450,
            "resize": "fit"
          }
        },
        "id_str": "652694417195827200",
        "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHUPWgAA14SE.jpg",
        "id": 6.5269441719583e+17,
        "type": "photo",
        "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHUPWgAA14SE.jpg",
        "url": "http:\/\/t.co\/kv0OVmoMoI"
      },
      {
        "display_url": "pic.twitter.com\/kv0OVmoMoI",
        "indices": [
          68,
          90
        ],
        "sizes": {
          "small": {
            "w": 340,
            "h": 605,
            "resize": "fit"
          },
          "large": {
            "w": 575,
            "h": 1024,
            "resize": "fit"
          },
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "medium": {
            "w": 575,
            "h": 1024,
            "resize": "fit"
          }
        },
        "id_str": "652694417208385536",
        "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHUSWIAAP9Ue.jpg",
        "id": 6.5269441720839e+17,
        "type": "photo",
        "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHUSWIAAP9Ue.jpg",
        "url": "http:\/\/t.co\/kv0OVmoMoI"
      }
    ]
  },
  "in_reply_to_status_id_str": null,
  "in_reply_to_status_id": null,
  "created_at": "Sat Oct 10 03:57:35 +0000 2015",
  "in_reply_to_user_id_str": null,
  "source": "<a href=\"http:\/\/twitter.com\/download\/iphone\" rel=\"nofollow\">Twitter for iPhone<\/a>",
  "retweet_count": 1,
  "retweeted": false,
  "geo": null,
  "in_reply_to_screen_name": null,
  "is_quote_status": false,
  "id_str": "652694439584993280",
  "in_reply_to_user_id": null,
  "favorite_count": 6,
  "id": 6.5269443958499e+17,
  "text": "some cool pictures from time square, brooklyn bridge and china town http:\/\/t.co\/kv0OVmoMoI",
  "place": null,
  "lang": "en",
  "favorited": false,
  "possibly_sensitive": false,
  "coordinates": null,
  "truncated": false,
  "entities": {
    "urls": [

    ],
    "hashtags": [

    ],
    "media": [
      {
        "display_url": "pic.twitter.com\/kv0OVmoMoI",
        "indices": [
          68,
          90
        ],
        "sizes": {
          "small": {
            "w": 340,
            "h": 191,
            "resize": "fit"
          },
          "large": {
            "w": 1024,
            "h": 576,
            "resize": "fit"
          },
          "thumb": {
            "w": 150,
            "h": 150,
            "resize": "crop"
          },
          "medium": {
            "w": 600,
            "h": 337,
            "resize": "fit"
          }
        },
        "id_str": "652694417070006272",
        "expanded_url": "http:\/\/twitter.com\/wtfbaile\/status\/652694439584993280\/photo\/1",
        "media_url_https": "https:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
        "id": 6.5269441707001e+17,
        "type": "photo",
        "media_url": "http:\/\/pbs.twimg.com\/media\/CQ7WHTxWoAA8JXV.jpg",
        "url": "http:\/\/t.co\/kv0OVmoMoI"
      }
    ],
    "user_mentions": [

    ],
    "symbols": [

    ]
  },
  "contributors": null,
  "user": {
    "utc_offset": -10800,
    "friends_count": 112,
    "profile_image_url_https": "https:\/\/pbs.twimg.com\/profile_images\/652216850751688704\/kC7rdEm__normal.png",
    "listed_count": 3,
    "profile_background_image_url": "http:\/\/pbs.twimg.com\/profile_background_images\/378800000046929850\/8b7537e055b6c558c461f49cb1a3ad39.png",
    "default_profile_image": false,
    "favourites_count": 21009,
    "description": "?? | 121614 ?? | phs '18 | ig: wtfbaile",
    "created_at": "Sat Jun 30 02:41:11 +0000 2012",
    "is_translator": false,
    "profile_background_image_url_https": "https:\/\/pbs.twimg.com\/profile_background_images\/378800000046929850\/8b7537e055b6c558c461f49cb1a3ad39.png",
    "protected": false,
    "screen_name": "wtfbaile",
    "id_str": "622435883",
    "profile_link_color": "9266CC",
    "is_translation_enabled": false,
    "id": 622435883,
    "geo_enabled": true,
    "profile_background_color": "C0DEED",
    "lang": "en",
    "has_extended_profile": true,
    "profile_sidebar_border_color": "FFFFFF",
    "profile_text_color": "333333",
    "verified": false,
    "profile_image_url": "http:\/\/pbs.twimg.com\/profile_images\/652216850751688704\/kC7rdEm__normal.png",
    "time_zone": "Atlantic Time (Canada)",
    "url": null,
    "contributors_enabled": false,
    "profile_background_tile": true,
    "profile_banner_url": "https:\/\/pbs.twimg.com\/profile_banners\/622435883\/1444187766",
    "entities": {
      "description": {
        "urls": [

        ]
      }
    },
    "statuses_count": 67181,
    "follow_request_sent": false,
    "followers_count": 600,
    "profile_use_background_image": true,
    "default_profile": false,
    "following": false,
    "name": "baile",
    "location": "410",
    "profile_sidebar_fill_color": "DDEEF6",
    "notifications": false
  }
}

I tried too with twitter4j, but doesn't work too .. I only get the first url photo ! 我也试过twitter4j,但也不行......我只得到第一个网址照片! Maybe i'm doing something bad ! 也许我做的事情很糟糕! If anyone can help me ! 如果有人可以帮助我!

public static void main(String[] args) throws Exception {
        ConfigurationBuilder cb = new ConfigurationBuilder();
        cb.setDebugEnabled(true)
                .setOAuthConsumerKey(TwitterConstantes.APIKEY)
                .setOAuthConsumerSecret(TwitterConstantes.APIKEYSECRET)
                .setOAuthAccessToken(TwitterConstantes.TOKEN)
                .setOAuthAccessTokenSecret(TwitterConstantes.TOKENSECRET);
        TwitterFactory tf = new TwitterFactory(cb.build());
        Twitter twitter = tf.getInstance();
        Query query = new Query("some cool pictures from time square, brooklyn bridge and china town");
        QueryResult result = twitter.search(query);
        System.out.println(result);
        for (Status status : result.getTweets()) {
            MediaEntity[] media = status.getMediaEntities(); //get the media entities from the status
            for(MediaEntity m : media){ //search trough the entities
                System.out.println(m.getMediaURL()); //get your url!
            }
        }
}

I found a solution to my question and I post it here to you. 我找到了问题的解决方案,并将其发布给您。 Hope this will be helpful for others in my case. 希望这对我的其他人有所帮助。

        ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
            .setOAuthConsumerKey(TwitterConstantes.APIKEY)
            .setOAuthConsumerSecret(TwitterConstantes.APIKEYSECRET)
            .setOAuthAccessToken(TwitterConstantes.TOKEN)
            .setOAuthAccessTokenSecret(TwitterConstantes.TOKENSECRET);

    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();
    double res = 5;

    //Query query = new Query(this.text);
    Query query = new Query().geoCode(new GeoLocation(latitude,longitude), res, Query.KILOMETERS.toString()); 
    query.setResultType(Query.RECENT); // get the recent tweets
    query.count(100);
    QueryResult result = twitter.search(query);
    do {

        for (Status tweet : result.getTweets()) {
            //if (tweet.isRetweet()){continue;}         
            try {
                Status tweetById = twitter.showStatus(tweet.getId());
                String url= "https://twitter.com/" + tweetById.getUser().getScreenName() 
                        + "/status/" + tweetById.getId();

                List<String> hashtags =  new ArrayList<String>();
                HashtagEntity[] hashtagsEntities = tweetById.getHashtagEntities();
                for (HashtagEntity hashtag : hashtagsEntities){
                    System.out.println(hashtag.getText());
                }

                ExtendedMediaEntity[] medias = tweetById.getExtendedMediaEntities();
                for (ExtendedMediaEntity m : medias){
                    System.out.println(m.getMediaURL());    
                }

            } catch (TwitterException e) {
                System.err.print("Failed to search tweets: " + e.getMessage());
                return;
            }
        }
        query = result.nextQuery();
        if(query!=null){
            result = twitter.search(query);
        }

    }while(query!=null);

This is a simpler answer based on @afaraut effort. 这是一个基于@afaraut努力的简单答案。

Make sure first you are using the latest version of twitter4j in this case 4.0.4 , to get all pictures in a tweet for example tweetID ="652694439584993280" : 首先确保在这种情况下使用最新版本的twitter4j 4.0.4 ,以获取推文中的所有图片,例如tweetID ="652694439584993280"

    ConfigurationBuilder cb = new ConfigurationBuilder();
    cb.setDebugEnabled(true)
        .setOAuthConsumerKey(TwitterConstantes.APIKEY)
        .setOAuthConsumerSecret(TwitterConstantes.APIKEYSECRET)
        .setOAuthAccessToken(TwitterConstantes.TOKEN)
        .setOAuthAccessTokenSecret(TwitterConstantes.TOKENSECRET);
    TwitterFactory tf = new TwitterFactory(cb.build());
    Twitter twitter = tf.getInstance();

    String tweetID ="652694439584993280";
    try {
        twitter4j.Status sts = twitter.showStatus(Long.parseLong(tweetID));
        ExtendedMediaEntity[] medias = sts.getExtendedMediaEntities(); //get the media entities from the status
        for(ExtendedMediaEntity m : medias){ //search trough your entities
            System.out.println("this is a pic","  " +m.getMediaURL());
        }
    } catch (TwitterException e) {
        e.printStackTrace();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM