Twitter: Twitter4j User's Country - twitter

I'm using twitter4j to get the location of a Twitter user:
Twitter twitter = new TwitterFactory().getInstance();
twitter.setOAuthConsumer(CONSUMER_KEY, CONSUMER_SECRET);
long id = accessToken.getUserId();
User user = twitter.showUser(id);
System.out.print("LOCATION: " +user.getLocation());
user.getLocation() does not include the country. How to get the user's country using twitter4j?

There's no separate field for a user's country. Whatever information is set in the Location field on your Twitter Profile page should be returned by the getLocation() method.
One thing I noticed about your code is that you're not showing how the accessToken variable is initialized. Try the following:
Twitter twitter = new TwitterFactory().getInstance();
long id = new AccessToken(ACCESS_TOKEN, ACCESS_TOKEN_SECRET).getUserId();
User user = twitter.showUser(id);
System.out.print("LOCATION: " + user.getLocation());
Where ACCESS_TOKEN and ACCESS_TOKEN_SECRET are the values you get after granting your application permission to access your Twitter account on your Twitter Developers page. When I run the code above I get the expected: "LOCATION: Charlotte, NC USA"

Related

Get/list all tweets of all users my account follow

I am using python tweepy to connect the twitter end point, and it's very simple to list all of any single user's tweets. It is also possible to read my account's "following" list, so technicly I can get the list of all the tweets by all of my followed users, thing is, it will be lot's and lot's of seperate API calls.
Is there a way to bulk this effectively?
You're not going to be able to get them all in one go, Twitter has rate limits to prevent that, but this script will get as many from each user as possible:
import tweepy
# Put your API keys here
CONSUMER_KEY = ""
CONSUMER_SECRET = ""
ACCESS_TOKEN = ""
ACCESS_TOKEN_SECRET = ""
# Authenticate to Tweepy and wait if you get rate limited
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth, wait_on_rate_limit=True)
# For each user that you follow.....
for user in tweepy.Cursor(api.friends, screen_name="pigeonburger").items():
# Get each user's username and print out 100 of their tweets
username = user._json['screen_name']
print(api.user_timeline(screen_name = username, count = 100))
# Do what you want with those tweets after

Get list of user id's from a users Twitter followers using Tweepy

I have written the following code to get a list of all the Twitter followers a username has but I would like to just get the user_id instead as this allows me to do a lot more within the rate limit for the API. How do I change this code to just print the user_ids?
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(key, secret)
api = tweepy.API(auth)
screen_name = "twitterusername"
friends = api.friends_ids(screen_name)
print(screen_name + " is following :")
for friend in friends:
print(api.get_user(friend).screen_name)
I have all of the correct authorisation keys and tokens.
The User object has an id field
print(screen_name + " is following :")
for friend in friends:
print(api.get_user(friend).id)
If you are interested in the IDs only of the users be aware that api.friends_ids returns already a list of IDs
friends = api.friends_ids('screen_name')
logging.info(friends)
# output: [324343434, 134343234567,...]

graph api get public events with rails/koala works only for some sites

This is my code
Koala.config.api_version = 'v2.3'
#oauth = Koala::Facebook::OAuth.new 'app_id', 'app_secret'
#graph = Koala::Facebook::API.new #oauth.get_app_access_token
#events = #graph.get_object('141991029192409/events')
which works fine. If I try to fetch the events from another site like
#events = #graph.get_object('161335993890217/events')
I get this error
GraphMethodException, code: 100, message: Unsupported get request.
Please read the Graph API documentation at
https://developers.facebook.com/docs/graph-api [HTTP 400]
That page is likely restricted in some way (alcohol related content, age, location) – and that means you need to use a user access token instead of the app access token. (Or a page access token, if you have admin control over that page.)
With a user access token, Facebook uses the information about that user to determine whether or not they are allowed to see the page. An app access token could be used by “anyone”, and therefor can not be used to access such restricted pages.

Factual crosswalk api, can I get a place's foursquare venue id with its corresponding facebook page id in 1 request?

I have a database full of places with the facebook page id's. I'd like to map them to the corresponding foursquare venue id with the factual api.
The problem is when I try to issue a request and set the namespace id to the facebook id all I get back is the entry that factual has with the factual_id and facebook id but not any other third party places with the same factual id.
What I'm doing now is saving the factual id, and then sending another request given the factual_id which returns all the crosswalk entries then I can look for the foursquare one. Is there anyway to do this request in 1 step?
Using php here is the code.
The place variable is a object that is basically mapped to a facebook page
$place = Model_Place::find(42);
$factual = new \Factual(\Config::get('factual_oauth_key'), \Config::get('factual_oauth_secret'));
$query = new \FactualQuery;
$query->limit(1);
$query->field("namespace")->equal("facebook");
$query->field("namespace_id")->equal($place->page_id);
$res = $factual->fetch("crosswalk", $query);
$data = $res->getData();
echo "<pre>";
print_r($data);
echo "</pre>";

How to make batch request using fb_graph?

when a user login using facebook then i need collect all the movies list liked by the user and his/her friends.
user = FbGraph::User.fetch('me', :access_token => "access_token")
userFrnd = user.friends
movies=[]
userFrnd.each do | uf |
frnd = FbGraph::User.fetch(uf.raw_attributes['id'], :access_token => "access_token")
movies << frnd.movies
end
final_movie_list = movies.flatten.uniq_by {|track| track.raw_attributes["id"]}
this is my fb_graph function and it's working fine.but i need to make it as batch request since the i have 360 friend it take 360 request to process the above function correctly.but help me out to optimize this and reduce the time it takes to calculate this function.
I came to know that batch request may help me but,i don't know how to do that in fb_graph.
Please help me
I'm using FbGraph from ( github.com/nov/fb_graph ) Version: 2.7.8 and I'm able to make a batch request for 100 users, at a time and get following information about them by default.
I'm not using any access token. So you might be able to get more information including movies.
id
name
first_name
last_name
link
username
gender
locale
Here's the demonstration code where ids is an array of Facebook User Ids:
r = FbGraph::User.fetch("?ids=#{ids.join(",")}")
r.raw_attributes #information about the users hashed by their id (String)

Resources