使用Twitter API从推文获取坐标

时间:2020-01-03 01:27:25

标签: java mongodb twitter unirest

我正在尝试使用Twitter API和Java从推文中正确获取信息。我正在尝试从this tweet获取启用了地理标记的确切坐标。

从现在开始,我按照documentation的指示创建了相应的Java类,但是也许我不太了解Geo对象,因为coordinatesbounding_box都没有或place出现。我的猜测是我没有正确声明这些类。

对于Tweet类:

// Imports here

public class Tweet {

    @SerializedName("created_at")
    public String created_at;

    @SerializedName("id")
    public long id;

    // ... All the rest of the parameters

    @SerializedName("coordinates")
    public Coordinates coordinates;

    @SerializedName("place")
    public transient Place place;

对于“坐标”类:

// Imports here

public class Coordinates {

    @SerializedName("coordinates")
    public String coordinates;

    @SerializedName("type")
    public String type;
}

对于地方课程:

// Imports here

public class Place {

    @SerializedName("id")
    public String id;

    @SerializedName("url")
    public String url;

    // ... All the rest of the parameters

    @SerializedName("bounding_box")
    public String bounding_box;

}

当我请求从链接的推文中获取json信息时,我将获得以下数据,并且您可以看到coordinatesplaces字段没有出现。如果有用,我正在使用MongoDB Java API来存储json文件及其Java POJO管理器。您可以在这里找到所有代码:https://github.com/marmatsan/theTrainEngine

{
    "_id" : NumberLong("1212893878430420992"),
    "created_at" : "Fri Jan 03 00:30:09 +0000 2020",
    "favorite_count" : 0,
    "favorited" : false,
    "id_str" : "1212893878430420992",
    "is_quote_status" : false,
    "lang" : "en",
    "possibly_sensitive" : false,
    "quoted_status_id" : NumberLong(0),
    "quoted_status_id_str" : NumberLong(0),
    "reply_count" : 0,
    "retweet_count" : 0,
    "retweeted" : false,
    "source" : "<a href=\"http://twitter.com/download/android\" rel=\"nofollow\">Twitter for Android</a>",
    "text" : "Test 1",
    "truncated" : "false",
    "user" : {
        "_id" : NumberLong(593955589),
        "created_at" : "Tue May 29 18:59:38 +0000 2012",
        "default_profile" : false,
        "default_profile_image" : false,
        "description" : "En búsqueda del verdadero camino del Tao estudiando Ingeniería en Tecnologías de Telecomunicación.",
        "favourites_count" : 7497,
        "followers_count" : 186,
        "friends_count" : 114,
        "id_str" : "593955589",
        "listed_count" : 5,
        "location" : "Salamanca/Toledo/Madrid",
        "name" : "Martín ?",
        "profile_banner_url" : "https://pbs.twimg.com/profile_banners/593955589/1513111580",
        "profile_image_url_https" : "https://pbs.twimg.com/profile_images/1130587021678927874/50unsAd3_normal.png",
        "protected_att" : false,
        "screen_name" : "MMateos97",
        "statuses_count" : 19420,
        "url" : "https://github.com/marmatsan",
        "verified" : false
    }
}  

编辑:如答案中所述,问题出在实现上,因为当我从transient对象中删除Place修饰符时,程序无法运行。如果有人可以解决此问题,请在评论中保留它。

1 个答案:

答案 0 :(得分:1)

当我请求状态时,它具有此状态的正确字段。

$ okurl https://api.twitter.com/1.1/statuses/show/1212893878430420992.json
...
"geo":null,
"coordinates":null,
"place":{"id":"fd110fb449209bc4","url":"https:\/\/api.twitter.com\/1.1\/geo\/id\/fd110fb449209bc4.json","place_type":"city","name":"Burguillos de Toledo"

https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/geo-objects#tweet-exact

阅读“使用Twitter Place进行推文”和“使用精确位置进行推文”之间的区别。

我怀疑您的“瞬态”字段导致Mongo不保存此字段。

相关问题