get tweets from keyword - java

Is there any way to get tweets containing a keyword in java? I want to download as many as possible, I have seen a java library twitter4j but it gives only small number of tweets.

Read the documentation of twitter api
https://dev.twitter.com/docs/api/1/get/search
Its rate limited though. I dont think there is a way around it.
The rate limiting varies with open search apis and the ones that require authentication.
http://search.twitter.com/search.json?q=blue%20angels&rpp=5&include_entities=true&result_type=mixed
(Note - this link is copied from twiter api webpage)

You can set the page size and number using Twitter4J to request more tweets.
public static void main(String[] args) throws TwitterException {
Twitter twitter = new TwitterFactory().getInstance();
for (int page = 1; page <= 10; page++) {
System.out.println("\nPage: " + page);
Query query = new Query("#MyWorstFear"); // trending right now
query.setRpp(100);
query.setPage(page);
QueryResult qr = twitter.search(query);
List<Tweet> qrTweets = qr.getTweets();
if(qrTweets.size() == 0) break;
for(Tweet t : qrTweets) {
System.out.println(t.getId() + " - " + t.getCreatedAt() + ": " + t.getText());
}
}
}

Related

How to get a list of retweeters of a retweet?

The title is confusing, I know, but I do not know how else to phrase this question.
Using twitter4j, I am able to get tweets and the list of users who have retweeted that tweet, like this
However, if the tweet is actually a retweet then I am not able to get the list of retweeters. Example
This is the code I am using to get the list of retweeters:
if(tweet.getId() > 0 && tweet.getRetweetCount() > 0) {
try {
List<Status> statuses = twitter.getRetweets(tweet.getId());
for (Status status : statuses) {
System.out.println("\n" + "\t" + "Retweeter ID:" + status.getUser().getId() + "\n" + "\t" + "Retweeter Name:" + status.getUser().getScreenName());
}
} catch (TwitterException e) {
//twitter.getRetweeterIds(tweet.getId(), 2, -1);
e.printStackTrace();
}
}
How do I get the retweeters of a retweet?
Twitter4J is a Java client library that interfaces with the Twitter REST API.
To understand the right call to use it's best to understand the underlying REST API.
Looking at the Twitter Rest API we can see an API that returns a list of users who have retweeted a particular tweet, GET statuses/retweeters/ids.
In your code the Twitter4J API you're using, getRetweets(), does not return the IDs of users who retweeted.
Looking at the Twitter4J Twitter4J API docs we find getRetweeterIds(statusId) that returns the list of user IDs that retweed a particular tweet indicated by statusId.

Twitter4j how can i get any twitter handle tweets from it's 1'st tweets

i am using this code i get an error TwitterException 429
trying to fecth the tweets.
i want to fecth whole tweets of twitter account from starting.
how to solve twitter rate limit issue.
int limitRateCounter=0;
int countOfTweets=0; int numberOfTweets = 3500; long lastID = Long.MAX_VALUE; ArrayList<Status> status = new ArrayList<Status>();while (status.size () < numberOfTweets) { try {.out.print("\nlimit counter = "+limitRateCounter);.out.print("\t tweetsCounter = "+countOfTweets);
List<Status> listOfStatus=
twitter.getUserTimeline(tweeterHandle,pg);
/* making twitter request */
countOfTweets=countOfTweets+listOfStatus.size();
status.addAll(listOfStatus);
limitRateCounter++;
// println("Gathered " + tweets.size() + " tweets");
for (Status t: status)
if(t.getId() < lastID) lastID = t.getId();
}
catch (TwitterException te) {
System.out.println("Couldn't connect: " + te);
//twitter=getTwitterDetails2();
break;
};
pg.setMaxId(lastID-1); /* add pagging max id */
}
after 120 request .getUserTimeline(tweeterHandle,pg); methode not fetching
new tweets after some time get exception.
Twitter4j uses Twitter API to access Twitter data. This API has limits in the number of invocations as is specified here:
API Rate Limits
Rate Limits Charts
You seem to be facing this problem right now. Basically your code needs to wait before making another call until you are again inside the API rate limits. Take into account that the rate limit is per "access token" as the documentation specifies, so you could increase the number of calls your code can make if you provide it with more access tokens, but in the end you will have a (bigger) limit.
create several accounts on twitter (10) then put access tokens in array

Using twitter4j to search through more than 100 queries [duplicate]

This question already has answers here:
How to retrieve more than 100 results using Twitter4j
(4 answers)
Closed 6 years ago.
I am trying to create a program that searches a query from twitter. The problem I am having is that the API returns only a 100 result queries and when I try to retrieve more it keeps giving me the same results again.
User user = twitter.showUser("johnny");
Query query = new Query("football");
query.setCount(100);
query.lang("en");
int i=0;
try {
QueryResult result = twitter.search(query);
for(int z = 0;z<2;z++){
for( Status status : result.getTweets()){
System.out.println("#" + status.getUser().getScreenName() + ":" + status.getText());
i++;
}
}
The program will print me 200 results relating to the query "football", but instead of giving me 200 different results it prints a 100 results twice. My end results should be that I can print as many different results as the rate limit allows. I have seen programs that return more than 100 responses for a specific user, but I haven't seen something that can return more than a 100 responses for a unique query like "football".
To get more than 100 results on a search Query you need to call to the next iteration of the Query.
Query query = new Query("football");
QueryResult result;
int Count=0;
do {
result = twitter.search(query);
List<Status> tweets = result.getTweets();
for (Status tweet : tweets) {
System.out.println("#" + tweet.getUser().getScreenName() + ":" + tweet.getText());
Count++;
}
try {
Thread.sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
while ((query = result.nextQuery()) != null);
System.out.println(Count);
System.exit(0);
I just tested it and got 275 tweets, keep in mind this from the documentation:
The Search API is not complete index of all Tweets, but instead an index of recent Tweets. At the moment that index includes between 6-9 days of Tweets.
And:
Before getting involved, it’s important to know that the Search API is focused on relevance and not completeness. This means that some Tweets and users may be missing from search results. If you want to match for completeness you should consider using a Streaming API instead.

How do I get a RateLimitStatus object in Twitter4j?

I'm trying to make a method public void limit() that checks the rate limit and sleeps however long it is until the reset if it is being rate limited. I cannot, however, figure out how to make a RateLimitStatus. I have tried:
RateLimitStatus status = twitter.getRateLimitStatus();
but it doesn't actually return a RateLimitStatus... Quite frankly, I'm not sure what the point of that is. Anyway, if anyone is aware of how to get a RateLimitStatus, their help would be much appreciated as currently my project is capable of crashing due to rate limits and I'd like to change this.
Thanks in advance!
The new Twitter API has a rate limit status per resource “family”, so twitter.getRateLimitStatus() returns a mapping between families/endpoints and rate limit statuses, e.g.:
RateLimitStatus status = twitter.getRateLimitStatus().get("/users/search");
// Better: specify the family
RateLimitStatus status2 = twitter.getRateLimitStatus("users").get("/users/search");
So, you could write a method public void limit(String endpoint), which would check the proper rate limit status.
public void limit(String endpoint) {
String family = endpoint.split("/", 3)[1];
RateLimitStatus status = twitter.getRateLimitStatus(family).get(endpoint);
// do what you want…
}
You’ll then call it with .limit("/users/search").
Map<String ,RateLimitStatus> rateLimitStatus = twitter.getRateLimitStatus();
for (String endpoint : rateLimitStatus.keySet()) {
RateLimitStatus status = rateLimitStatus.get(endpoint);
System.out.println("Endpoint: " + endpoint);
System.out.println(" Limit: " + status.getLimit());
System.out.println(" Remaining: " + status.getRemaining());
System.out.println(" ResetTimeInSeconds: " + status.getResetTimeInSeconds());
System.out.println(" SecondsUntilReset: " + status.getSecondsUntilReset());
}
Twitter API also allows for:
Log.d("TwitterActivity", "Limit:" + mTwitter.getFavorites().getRateLimitStatus().getLimit());
Where:
mTwitter is your Twitter object
getFavorites() can be replaced by any other function that Twitter4j provides for the Twitter object
getLimit() is but one of the various options you can choose
You can check like so:
if(mTwitter.getFavorites().getRateLimitStatus().getLimit() <= 0){
//do something
}

Is it possible to get a list of workflows the document is attached to in Alfresco

I'm trying to get a list of workflows the document is attached to in an Alfresco webscript, but I am kind of stuck.
My original problem is that I have a list of files, and the current user may have workflows assigned to him with these documents. So, now I want to create a webscript that will look in a folder, take all the documents there, and assemble a list of documents together with task references, if there are any for the current user.
I know about the "workflow" object that gives me the list of workflows for the current user, but this is not a solution for my problem.
So, can I get a list of workflows a specific document is attached to?
Well, for future reference, I've found a way to get all the active workflows on a document from javascript:
var nodeR = search.findNode('workspace://SpacesStore/'+doc.nodeRef);
for each ( wf in nodeR.activeWorkflows )
{
// Do whatever here.
}
I used packageContains association to find workflows for document.
Below i posted code in Alfresco JavaScript for active workflows (as zladuric answered) and also for all workflows:
/*global search, logger, workflow*/
var getWorkflowsForDocument, getActiveWorkflowsForDocument;
getWorkflowsForDocument = function () {
"use strict";
var doc, parentAssocs, packages, packagesLen, i, pack, props, workflowId, instance, isActive;
//
doc = search.findNode("workspace://SpacesStore/8847ea95-108d-4e08-90ab-34114e7b3977");
parentAssocs = doc.getParentAssocs();
packages = parentAssocs["{http://www.alfresco.org/model/bpm/1.0}packageContains"];
//
if (packages) {
packagesLen = packages.length;
//
for (i = 0; i < packagesLen; i += 1) {
pack = packages[i];
props = pack.getProperties();
workflowId = props["{http://www.alfresco.org/model/bpm/1.0}workflowInstanceId"];
instance = workflow.getInstance(workflowId);
/* instance is org.alfresco.repo.workflow.jscript.JscriptWorkflowInstance */
isActive = instance.isActive();
logger.log(" + instance: " + workflowId + " (active: " + isActive + ")");
}
}
};
getActiveWorkflowsForDocument = function () {
"use strict";
var doc, activeWorkflows, activeWorkflowsLen, i, instance;
//
doc = search.findNode("workspace://SpacesStore/8847ea95-108d-4e08-90ab-34114e7b3977");
activeWorkflows = doc.activeWorkflows;
activeWorkflowsLen = activeWorkflows.length;
for (i = 0; i < activeWorkflowsLen; i += 1) {
instance = activeWorkflows[i];
/* instance is org.alfresco.repo.workflow.jscript.JscriptWorkflowInstance */
logger.log(" - instance: " + instance.getId() + " (active: " + instance.isActive() + ")");
}
}
getWorkflowsForDocument();
getActiveWorkflowsForDocument();
Unfortunately the javascript API doesn't expose all the workflow functions. It look like getting the list of workflow instances that are attached to a document only works in Java (or Java backed webscripts).
List<WorkflowInstance> workflows = workflowService.getWorkflowsForContent(node.getNodeRef(), true);
A usage of this can be found in the workflow list in the document details: http://svn.alfresco.com/repos/alfresco-open-mirror/alfresco/HEAD/root/projects/web-client/source/java/org/alfresco/web/ui/repo/component/UINodeWorkflowInfo.java
To get to the users who have tasks assigned you would then need to use getWorkflowPaths and getTasksForWorkflowPath methods of the WorkflowService.

Categories

Resources