Determine tweet location with Twitter API - twitter

How can I find the location of a specific user's tweet? What are the API methods and techniques that I need to go through to determine the location?

Note: The below assumes that you want to grab multiple tweets and find their locations. If you don't, and you just want a single tweet by it's id, use statuses/show.
It's entirely possible - if the user has enabled location for their tweet. It'll be the value of the coordinates key, which will be null if they haven't.
Let's say you're using the following API method: statuses/user_timeline.
This is a GET request
The resource url is: https://api.twitter.com/1.1/statuses/user_timeline.json
You can specify either the user_id or screen_name as part of the GET parameters, for example: ?screen_name=J7mbo.
In the tweet results, once json_decode() is run on them, one of the keys will look like this:
This is actually the first key, according to the documentation, so you should be able to find it and it's value pretty easily.
Warning
Do not get confused with the location sub-key underneath the User key. This is the location of the user, as per their profile, not the location of any specific tweet. Use the coordinates key for that.
Documentation Link

Related

How can I search the channel in a specific country using the YouTube API 3.0?

I use the url to search channels in a specific country like this https://www.googleapis.com/youtube/v3/search?part=snippet&maxResults=8&order=viewCount&q=news&type=channel&regionCode=US&key=API-Key.
But when i changed the regionCode parameter to IT(italy) or any other one,the search result is always identical.So the search result is not changed with the regionCode.
How can I search the channel by country using the YouTube API 3.0,thanks.
I also try to search channel with the optional parameter regionCode, and it seems that this parameter is not to filter the results base on the country code of a Country. I found in this related SO question that the region parameter only applies to movie charts, and is not something that works as a parameter for general searches.
So the only possible parameter that I think you can use to filter the results of YouTube API in a specific country or place is by using the parameter location.
The location parameter, in conjunction with the locationRadius parameter, defines a circular geographic area and also restricts a search to videos that specify, in their metadata, a geographic location that falls within that area. The parameter value is a string that specifies latitude/longitude coordinates e.g. (37.42307,-122.08427).
Hope it helps you.
There is no way to solve this problem. I tried location with locationRadius, but second parameter is limited to 1000km, so, I cant find all videos from Russian Federation or USA using that option and 1 api request.
Also, that api request performs slow for understandable reason.

How to find all 'retweet with comments' for a particular Tweet using API?

I want to get all the 'retweets with comments' of a tweet.
Here are few things I noticed with twitter api
Retweets with comments are treated as tweets. The retweet count does not increase if you add a comment, also the twitter message is "XYZ quoted you instead retweeted you'
You clearly can't use this API endpoint https://dev.twitter.com/rest/reference/post/statuses/retweet/:id
Is there a way to find all the 'tweet/retweet with comment' if you can supply the original Tweet/Id?
So you're referring to Quoted Tweets (retweet with comments). There is no official method for that from the REST API yet, however, there are couple of ways to do it.
Since all quoted tweets contain the short url to the original one,
you can still use in_reply_to_status_id and filter by short url of the original tweet
Search for tweets that contain the field quoted_status_id this can be done either through REST or STREAMING API.
quoted_status_id: This field only surfaces when the Tweet is a quote Tweet. This field contains the integer value Tweet ID of the quoted Tweet.
This isn't easy to accomplish. Here's the brute-force API method to find Quote Tweets.
Take your tweet id, it'll be something like 750176081987014657.
GET search/tweets
Use the Twitter API and search for the tweet.
https://dev.twitter.com/rest/reference/get/search/tweets
The GET request will look something like this:
https://api.twitter.com/1.1/search/tweets.json?q=750176081987014657&count=100
The q "query" argument is the tweet id. I've set the result count argument to the maximum (100).
Authorization
Of course with the Twitter API there's a whole bunch of tricky Authorization header work you have to do as well in order to complete the GET request. That's documented elsewhere and is beyond the scope of this answer.
Results and Conclusion
When you have the JSON results of this GET request, focus on the statuses collection. For each tweet in the collection (otherwise known as a "status"), check if it contains a quoted_status_id field. If it does, and the field value matches your tweet id, the tweet is a quote tweet. If it does not, it is simply a retweet with no added comment. You will also have to deal with iterating through the pagination of results if there are more than 100. That's done by looking for a search_metadata.next_results field and retrieving the next GET query string from it, which will be provided for you.

Rails Koala gem - get user's country

I'm using OmniAuth and the Koala gem to add Facebook Connect to my app and i'm trying to get the user's country.
So far I have:
graph = Koala::Facebook::API.new(user.oauth_token)
profile = graph.get_object("me")
profile["location"] gives me the id and name of the city.
I've tried: graph.get_object(city_id) which returns the city object, but I can't find any mention of the city's country in it.
I can also get the country name from the location string: "Kfar Saba, Israel" but that seems like a hack... Any official way to get what i'm looking for? Thanks!
There's the issue that the location data is not always complete for Place pages. I think you'll need to implement the location string parsing as a fallback if the location.country field of the Place page is empty. Be aware that the location string may also contain the location.state, which means that you'll have to use the last token after the split by comma.
Graph API returns no country for the named city:
https://developers.facebook.com/tools/explorer?method=GET&path=114269838585202&version=v2.0
See https://developers.facebook.com/docs/graph-api/reference/v2.0/page#readfields for the Page object structure.

iOS Twitter API; How to retrieve the most recent tweets within a country?

I am new to the Twitter API and iOS, but reading the documentation I learned to use the "Twitter.framework" in xCode and the "TWRequest" class in specific.
The most obvious way to go would be to make a request to:
GET statuses/public_timeline
However that request does not allow a parameter to specify a latitude and a longitude to get the most recent public tweets within a country. I did find:
GET geo/search
Which allows me to set all kind of cool parameters such as lat, long and granularity, but unfortunately it does not return tweets in the results.
So is it possible? Or a lost cost?
Edit:
So I guess I should be using:
GET search
But the only downfall is that I should give a q (query), a search term, but I am not searching for a specific term. I just like to retrieve the most recent public tweets in a given country. So is there, and should I use, a wildcard?
First make a request to:
GET geo/search
To get the place ID. For The Netherlands the ID is 879d7cfc66c9c290.
Then make a search request to:
GET search
With the query:
q=place:{PLACE_ID}
Example: "q=place:879d7cfc66c9c290"
If you like to get the most recent you can also set a parameter called result_type and set it to recent and that's it. Hope it helps people.

twitter search api no place returned

I am having a bit of trouble sifting through the twitter API. I am trying to search for tweets near a certain location and then see their exact (or approximate) geo coordinates. I understand the geo field is deprecated and now we are supposed to use the "place" field. Unfortunately when I use the following url: http://search.twitter.com/search.json?q=&geocode=30.1829,-97.832,10mi I get a bunch of responses with a location that matches the city of the geo coordinates, but geo: is null and there is no place field. Am I stuck with only having an accuracy up to city coordinates, or am I using the wrong search call?
Thanks in advance!
Remember, tweeting with a geo location is an opt-in process. Twitter will supply a feed from the area for all tweeters in that area, but only those who have opted to tweet their coordinates will show up with "geo" info.
Using q=here with your url it did return 1 tweet for me with "geo" info in this form (though likely it will have moved on down the timeline by the time you try...):
"geo":{"coordinates":[-33.9769,18.5080],"type":"Point"}
Every user has a user defined location set in their profile settings. Use this when users don't use geo-encoding.
You can obtain a list of all countries / states by country and do a simple clean-up operation on the dataset.
This gave me what I needed for location data and greatly enriched my output.

Resources