I am trying to find a way to search for tweets with a specific hashtag AND tweets that only have pictures/media.
Searching for a hashtag is working:
Step #1
Twitter twitter = new TwitterFactory(cb.build()).getInstance();
Query query = new Query("#test");
query.setResultType(Query.RECENT);
query.setRpp(100);
QueryResult result = twitter.search(query);
.....
However, this returns all the most recent tweets up to 100 with the hashtag "test". Then I have to filter the ones with media
Step #2
Loop through the tweets and get the ones with media/pic
I want to skip step #2 and tell the query to retrieve only the ones with media. is that possible...I am particularly interested in the ones with pics.
Any help?
I know it's been a long time but this may help other people who want to learn the answer.
List<twitter4j.Status> tweets=result.getTweets();
for(twitter4j.Status tweet : tweets){
if(tweet.getMediaEntities().length!=0){//If there is media in a tweet
//Do anything you want with image
//tweet.getMediaEntities()[0].getMediaURL() is url of image in a tweet. For example:
publishProgress(tweet.getMediaEntities()[0].getMediaURL());
//Log.d(TAG, tweet.getMediaEntities()[0].getMediaURL()));
}
}
I know this is kinda late, but try including "pic.twitter.com" in your search. That won't have all the pictures, but you can add other urls as well.
Related
I am using the Facebook graph API to get the posts and the comments, and it's all working fine. Now I want to use the API to get the replies of a particular comment. How can I do that? I’m using the following code to get the comments:
cmmntObj = facebookClient.fetchObject(postID + "/comments", JsonObject.class,
Parameter.with("limit", limitOfRecords),
Parameter.with(Since_Until[k], date_SinceLast[k].toString()),
Parameter.with("Date_Format", "U"));
The following code works well and fetches the comments. I would appreciate if somebody can help me in getting the replies also.
I parsed the Comments JSON and built another query around it but it doesn’t work.
This is the query to fetch the tweets:
String getCmmntID = new String();
getCmmntID = cmmntObj.getJsonArray("data").getJsonObject(0).getString("id");// .getString("id");
cmmntReplies = facebookClient.fetchObject(
postID + "/comments?filter=stream&fields=parent.fields(" + getCmmntID + ")",
JsonObject.class, Parameter.with("limit", limitOfRecords),
Parameter.with(Since_Until[k], date_SinceLast[k].toString()),
Parameter.with("Date_Format", "U"));
How do I get the replies to these?
It is possible to get the comments edge for your comments, and then to get the comments edge of those comments, ad-infinitum. This becomes a matter of you deciding how many levels of comments you want to code into your application. I'm not sure if there is a finite number of levels of comments that Facebook would allow, you'll have to experiment with that. However, here is what you would want to add:
Parameter.with("fields", "message,comments{comments,message}")
That will get you three levels of comments (the main comments and two levels of replies).
I wrote this simple code that works just fine :
//access the twitter API using your twitter4j.properties file
Twitter twitter = TwitterFactory.getSingleton();
//create a new search
Query query = new Query("\"your welcome\"");
//get the results from that search
QueryResult result = twitter.search(query);
//get the first tweet from those results
Status tweetResult = result.getTweets().get(0);
//reply to that tweet
StatusUpdate statusUpdate = new StatusUpdate(".#" + tweetResult.getUser().getScreenName() +" I believe you meant \"you're\" here?");
statusUpdate.inReplyToStatusId(tweetResult.getId());
Status status = twitter.updateStatus(statusUpdate);
The problem is, even using my own parametres (Acessstoken, Consumerkey,,) that I generated manually from app.twitter.com, the code still sends the tweet to this account : Twit4j . it seems that too many had tweeted through it too!
twitter4j.proprieties is set up correctly
libs are correctly integrated
anyone knows what could be wrong ?
The Problem were (I guess) in the class name! the package name+ my class name are "Twitter" and the Object name from the Twitter4j is "Twitter" too! think the IDE got confused maybe ?
anyway now it works just fine !
I have created a twitterStream in twitter4j java Api, with which I want to track down user's status. I got a list of users which is actually the followIDs. In listener actually I download user's status images. I save my images with the following name:
String ImageUniqueFileName = status.getUser().getId()+"_id_"+CreateUniqueFileName();
ImageUniqueFileName = ImageUniqueFileName + ".jpg";
I ve noticed that in saved images, I got several user ids which was not in the initial list followIDs. Is it normal that I track down ids from other users? Second question, how is it possible to track down not all user tweets but just the last 200 user tweets in twitterStream?
TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance();
twitterStream.addListener(listener);
FilterQuery fq = new FilterQuery();
// fq.track(myQueries);
fq.follow(followIDs);
twitterStream.filter(fq);
ArrayList<FilterQuery> list = new ArrayList<FilterQuery>();
list.add(fq);
Using the follow parameter will result in the following Tweets being matched:
Tweets created by the user.
Tweets which are retweeted by the user.
Replies to any Tweet created by the user.
Retweets of any Tweet created by the user.
Manual replies, created without pressing a reply button (e.g. “#twitterapi I agree”).
source
I think you assumption is correct, this may account for the unknown ids.
Regarding obtaining the user's Tweets, you won't be able to use the streaming api for that as it's a real time view of Twitter. However, you may be able to use getUserTimeline(userId, paging) to retrieve the Tweets.
For a simple example of getUserTimeline in action, take a look at the GetUserTimeline example.
In Facebook Graph API, can we get list of comments made by given user.
I haven't found any direct way to get lists of comments. So, I tried to find them through my feed, but it's returning all feed posts. Can we filter other posts where I have not commented?
I tried various queries as below, but could not get exactly what I need.
/me/feed?fields=comments?fields=from?name="Nitin Thokare",message
/me/feed?fields=comments.fields(from.name("Nitin Thokare"),message)
I need either (1) list of all comments by me or else (2) lists of posts which I have commented on, filtering out all other posts.
How can we do this? (Finally I'll be using Java api to do the same.)
you can get the list of comments of an user by using spring supported Facebook API. To access any information of an user you have to pass access token of that particular user.
To get list of comments, you have to create FaceBookTemplate object by passing access token and by using that get FeedOperations object. By using FeedOperations object you can get feed,posts,status,links and so on. Get the list of post and iterate each and every post and get all the comments of the post which was made by you on that post.
eg code:-
FaceBook faceBook = new FaceBookTemplate(accessToken);
FeedOperations feeds=faceBook.feedOperations();
List<Post> posts=feeds.getPosts();
List<Comment> comments=new ArrayList<>();
for(Post post:posts){
List<Comment> commentsList = post.getComments();
for(Comment comment:commentsList){
comments.add(comment);
}
}
I'm using the twitter4j library to access the public twitter stream. I'm trying to make a project involving geotagged tweets, and I need to collect a large number of them for testing.
Right now I am getting the unfiltered stream from twitter and only saving tweets with geotags. This is slow though because the VAST majority of tweets don't have geo tags. I want the twitter stream to send me only tweets with geotags.
I have tried using the method mentioned in this question, where you filter with a bounding box of size 360* by 180* but that's not working for me. I'm not getting any errors when using that filter, but I'm still getting 99% of tweets with no geotags. Here is how I'm doing it:
ConfigurationBuilder cb = new ConfigurationBuilder();
cb.setDebugEnabled(true)
.setOAuthConsumerKey("censored")
.setOAuthConsumerSecret("censored")
.setOAuthAccessToken("censored")
.setOAuthAccessTokenSecret("censored");
TwitterStream twitterStream = newTwitterStreamFactory(cb.build()).getInstance();
StatusListener listener = new MyStatusListener();
twitterStream.addListener(listener);
//add location filter for what I hope is the whole planet. Just trying to limit
//results to only things that are geotagged
FilterQuery locationFilter = new FilterQuery();
double[][] locations = {{-180.0d,-90.0d},{180.0d,90.0d}};
locationFilter.locations(locations);
twitterStream.filter(locationFilter);
twitterStream.sample();
Any suggestions about why I'm still getting tweets with no geotags?
Edit: I just reread the twitter4j javadoc on adding filters to a twitter stream, and it says "The default access level allows up to 200 track keywords, 400 follow userids and 10 1-degree location boxes." So bounding boxes may only be 1 degree wide? That's different from the original information I came across. Is that my problem? My filter request is too big so it's being ignored? I'm not getting any errors when trying to use it.
getting from filter stream then overwriting it with sample stream.
remove the last line : twitterStream.sample();