List of usage examples for twitter4j Query setLang
public void setLang(String lang)
From source file:edu.ucsc.twitter.PeriodicTweetsSearch.java
License:Apache License
private Query buildOrQuery(Set<String> keywords, String language) { final StringBuilder queryString = new StringBuilder(); for (Iterator<String> itr = keywords.iterator(); itr.hasNext();) { queryString.append(String.format("\"%s\"", itr.next())); if (itr.hasNext()) { queryString.append(" OR "); }/*from w ww . j a v a 2s. c o m*/ } final Query query = new Query(queryString.toString() + " OR " + buildAndQuery(keywords)); query.setLang(language); return query; }
From source file:edu.uml.TwitterDataMining.TwitterConsumer.java
/** * Search twitter with the given search term Results will be saved in a file * with the same name as the query// www . j av a 2 s . com * * @param searchTerm The search term to be queried * @return a list of tweets */ public static List<Status> queryTwitter(String searchTerm) { Query query = new Query(searchTerm); query.setLang("en"); // we only want english tweets // get and format date to get tweets no more than a year old SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String date = sdf.format(new Date(System.currentTimeMillis() - (long) (365 * 24 * 60 * 60 * 1000))); query.setSince(date); QueryResult result; List<Status> tweets = null; try { //ONLY GETTING FIRST SET OF RESULTS result = twitter.search(query); tweets = result.getTweets(); // for (Status tweet : tweets) { // System.out.println("@" + tweet.getUser().getScreenName() + "\t" + tweet.getText()); // } // Wait loop for reset } catch (TwitterException te) { try { // try block for sleep thread if (!te.isCausedByNetworkIssue()) { // Not really checking for anything else but it is likely that we are out of requests int resetTime = te.getRateLimitStatus().getSecondsUntilReset(); while (resetTime > 0) { Thread.sleep(1000); // 1 second stop System.out.println("seconds till reset: " + resetTime); --resetTime; } } else { te.printStackTrace(); } } catch (InterruptedException ie) { ie.printStackTrace(); System.exit(-1); } } return tweets; }
From source file:gui.project.v3.FXMLDocumentController.java
@FXML public void btn(ActionEvent event) { twitter = tf.getInstance();/*www . j a v a 2 s . co m*/ String[] fields = { ch1.getText(), ch2.getText(), ch3.getText(), ch4.getText(), ch5.getText(), ch6.getText(), ch7.getText(), ch8.getText(), ch9.getText(), ch10.getText() }; int[] amount = new int[10]; for (int i = 0; i < 10; i++) { amount[i] = 0; } Query query = new Query(parent.getText()); query.setLang("en"); QueryResult result; int number = 0; try { do { result = twitter.search(query); List<Status> tweets = result.getTweets(); number += tweets.size(); for (Status tweet : tweets) { String tweetText = tweet.getText(); System.out.println(tweetText); for (int i = 0; i < 10; i++) { if ((tweetText.startsWith(fields[i] + " ") || (tweetText.endsWith(" " + fields[i]) || tweetText.contains(" " + fields[i] + " "))) && fields[i].length() > 0) { amount[i]++; } } } } while ((query = result.nextQuery()) != null); } catch (TwitterException ex) { } ObservableList<PieChart.Data> list = FXCollections.observableArrayList(); for (int i = 0; i < 10; i++) { if (fields[i].length() > 0) { list.add(new PieChart.Data(fields[i], amount[i])); } switch (i) { case 0: lab1.setText("" + amount[i]); break; case 1: lab2.setText("" + amount[i]); break; case 2: lab3.setText("" + amount[i]); break; case 3: lab4.setText("" + amount[i]); break; case 4: lab5.setText("" + amount[i]); break; case 5: lab6.setText("" + amount[i]); break; case 6: lab7.setText("" + amount[i]); break; case 7: lab8.setText("" + amount[i]); break; case 8: lab9.setText("" + amount[i]); break; case 9: lab10.setText("" + amount[i]); break; default: System.out.print(" "); } } chart.setData(list); }
From source file:mashup.MashTweetManager.java
public static ArrayList<String> getTweets(String topic) throws IOException { ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthAccessToken(Common.AccessToken) .setOAuthAccessTokenSecret(Common.AccessTokenSecret).setOAuthConsumerKey(Common.ConsumerKey) .setOAuthConsumerSecret(Common.ConsumerSecret); TwitterFactory tf = new TwitterFactory(cb.build()); Twitter twitter = tf.getInstance();/*from w w w. j a v a 2s.co m*/ //Twitter twitter = new TwitterFactory().getInstance(); // twitter.setOAuthAccessToken(null); ArrayList<String> tweetList = new ArrayList<>(); try { Query query = new Query(topic.toLowerCase().trim()); query.setCount(100); query.setLocale("en"); query.setLang("en"); QueryResult result = null; do { result = twitter.search(query); List<Status> tweets = result.getTweets(); for (Status tweet : tweets) { String data = tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", ""); HinghlishStopWords.removeStopWords(data.trim()); // Remove Special... Characters // Remove stop words tweetList.add(tweet.getText().replaceAll("[^\\p{L}\\p{Z}]", "")); } } while ((query = result.nextQuery()) != null); } catch (TwitterException te) { te.printStackTrace(); System.out.println("Failed to search tweets: " + te.getMessage()); } return tweetList; }
From source file:net.lacolaco.smileessence.twitter.task.SearchTask.java
License:Open Source License
public static Query getBaseQuery(MainActivity activity, String queryString) { Configuration config = activity.getResources().getConfiguration(); Query query = new Query(); query.setQuery(queryString);//w w w.j a v a 2s .c o m query.setLang(config.locale.getLanguage()); query.setCount(TwitterUtils.getPagingCount(activity)); query.setResultType(Query.RECENT); return query; }
From source file:ontoSentiment.Busca.java
public void buscarPorAssunto(String busca, String lang) throws TwitterException { int totalTweets = 0; long maxID = -1; Query q = new Query(busca + " -filter:retweets -filter:links -filter:replies -filter:images"); q.setCount(Util.TWEETS_PER_QUERY); // How many tweets, max, to retrieve q.resultType(Query.ResultType.recent); // Get all tweets q.setLang(lang); QueryResult r = Util.getTwitter().search(q); do {//w ww . ja v a2 s.com for (Status s : r.getTweets()) { totalTweets++; if (maxID == -1 || s.getId() < maxID) { maxID = s.getId(); } //System.out.printf("O tweet de id %s disse as %s, @%-20s disse: %s\n", new Long(s.getId()).toString(), s.getCreatedAt().toString(), s.getUser().getScreenName(), Util.cleanText(s.getText())); System.out.println(Util.cleanText(s.getText())); } q = r.nextQuery(); if (q != null) { q.setMaxId(maxID); r = Util.getTwitter().search(q); System.out.println("Total tweets: " + totalTweets); System.out.println("Maximo ID: " + maxID); Util.imprimirRateLimit(Util.RATE_LIMIT_OPTION_SEARCH_TWEETS); } } while (q != null); }
From source file:org.anc.lapps.datasource.twitter.TwitterDatasource.java
/** * Entry point for a Lappsgrid service.//from w ww . ja v a 2 s. c o m * <p> * Each service on the Lappsgrid will accept {@code org.lappsgrid.serialization.Data} object * and return a {@code Data} object with a {@code org.lappsgrid.serialization.lif.Container} * payload. * <p> * Errors and exceptions that occur during processing should be wrapped in a {@code Data} * object with the discriminator set to http://vocab.lappsgrid.org/ns/error * <p> * See <a href="https://lapp.github.io/org.lappsgrid.serialization/index.html?org/lappsgrid/serialization/Data.html>org.lappsgrid.serialization.Data</a><br /> * See <a href="https://lapp.github.io/org.lappsgrid.serialization/index.html?org/lappsgrid/serialization/lif/Container.html>org.lappsgrid.serialization.lif.Container</a><br /> * * @param input A JSON string representing a Data object * @return A JSON string containing a Data object with a Container payload. */ @Override public String execute(String input) { Data<String> data = Serializer.parse(input, Data.class); String discriminator = data.getDiscriminator(); // Return ERRORS back if (Discriminators.Uri.ERROR.equals(discriminator)) { return input; } // Generate an error if the used discriminator is wrong if (!Discriminators.Uri.GET.equals(discriminator)) { return generateError( "Invalid discriminator.\nExpected " + Discriminators.Uri.GET + "\nFound " + discriminator); } Configuration config = new ConfigurationBuilder().setApplicationOnlyAuthEnabled(true).setDebugEnabled(false) .build(); // Authentication using saved keys Twitter twitter = new TwitterFactory(config).getInstance(); String key = readProperty(KEY_PROPERTY); if (key == null) { return generateError("The Twitter Consumer Key property has not been set."); } String secret = readProperty(SECRET_PROPERTY); if (secret == null) { return generateError("The Twitter Consumer Secret property has not been set."); } twitter.setOAuthConsumer(key, secret); try { twitter.getOAuth2Token(); } catch (TwitterException te) { String errorData = generateError(te.getMessage()); logger.error(errorData); return errorData; } // Get query String from data payload Query query = new Query(data.getPayload()); // Set the type to Popular or Recent if specified // Results will be Mixed by default. if (data.getParameter("type") == "Popular") query.setResultType(Query.POPULAR); if (data.getParameter("type") == "Recent") query.setResultType(Query.RECENT); // Get lang string String langCode = (String) data.getParameter("lang"); // Verify the validity of the language code and add it to the query if it's valid if (validateLangCode(langCode)) query.setLang(langCode); // Get date strings String sinceString = (String) data.getParameter("since"); String untilString = (String) data.getParameter("until"); // Verify the format of the date strings and set the parameters to query if correctly given if (validateDateFormat(untilString)) query.setUntil(untilString); if (validateDateFormat(sinceString)) query.setSince(sinceString); // Get GeoLocation if (data.getParameter("address") != null) { String address = (String) data.getParameter("address"); double radius = (double) data.getParameter("radius"); if (radius <= 0) radius = 10; Query.Unit unit = Query.MILES; if (data.getParameter("unit") == "km") unit = Query.KILOMETERS; GeoLocation geoLocation; try { double[] coordinates = getGeocode(address); geoLocation = new GeoLocation(coordinates[0], coordinates[1]); } catch (Exception e) { String errorData = generateError(e.getMessage()); logger.error(errorData); return errorData; } query.geoCode(geoLocation, radius, String.valueOf(unit)); } // Get the number of tweets from count parameter, and set it to default = 15 if not specified int numberOfTweets; try { numberOfTweets = (int) data.getParameter("count"); } catch (NullPointerException e) { numberOfTweets = 15; } // Generate an ArrayList of the wanted number of tweets, and handle possible errors. // This is meant to avoid the 100 tweet limit set by twitter4j and extract as many tweets as needed ArrayList<Status> allTweets; Data tweetsData = getTweetsByCount(numberOfTweets, query, twitter); String tweetsDataDisc = tweetsData.getDiscriminator(); if (Discriminators.Uri.ERROR.equals(tweetsDataDisc)) return tweetsData.asPrettyJson(); else { allTweets = (ArrayList<Status>) tweetsData.getPayload(); } // Initialize StringBuilder to hold the final string StringBuilder builder = new StringBuilder(); // Append each Status (each tweet) to the initialized builder for (Status status : allTweets) { String single = status.getCreatedAt() + " : " + status.getUser().getScreenName() + " : " + status.getText() + "\n"; builder.append(single); } // Output results Container container = new Container(); container.setText(builder.toString()); Data<Container> output = new Data<>(Discriminators.Uri.LAPPS, container); return output.asPrettyJson(); }
From source file:org.apache.camel.component.twitter.consumer.search.SearchConsumer.java
License:Apache License
private List<Status> search(Query query) throws TwitterException { Integer numberOfPages = 1;//from ww w . j av a 2 s . c om if (ObjectHelper.isNotEmpty(te.getProperties().getLang())) { query.setLang(te.getProperties().getLang()); } if (ObjectHelper.isNotEmpty(te.getProperties().getCount())) { query.setCount(te.getProperties().getCount()); } if (ObjectHelper.isNotEmpty(te.getProperties().getNumberOfPages())) { numberOfPages = te.getProperties().getNumberOfPages(); } LOG.debug("Searching with " + numberOfPages + " pages."); Twitter twitter = te.getProperties().getTwitter(); QueryResult qr = twitter.search(query); List<Status> tweets = qr.getTweets(); for (int i = 1; i < numberOfPages; i++) { if (!qr.hasNext()) { break; } qr = twitter.search(qr.nextQuery()); tweets.addAll(qr.getTweets()); } if (te.getProperties().isFilterOld()) { for (Status t : tweets) { checkLastId(t.getId()); } } return tweets; }
From source file:org.apache.camel.component.twitter.producer.SearchProducer.java
License:Apache License
@Override public void process(Exchange exchange) throws Exception { long myLastId = lastId; // KEYWORDS/*from w w w . j a v a 2 s. c om*/ // keywords from header take precedence String keywords = exchange.getIn().getHeader(TwitterConstants.TWITTER_KEYWORDS, String.class); if (keywords == null) { keywords = te.getProperties().getKeywords(); } if (keywords == null) { throw new CamelExchangeException("No keywords to use for query", exchange); } Query query = new Query(keywords); // filter of older tweets if (te.getProperties().isFilterOld() && myLastId != 0) { query.setSinceId(myLastId); } // language String lang = exchange.getIn().getHeader(TwitterConstants.TWITTER_SEARCH_LANGUAGE, String.class); if (lang == null) { lang = te.getProperties().getLang(); } if (ObjectHelper.isNotEmpty(lang)) { query.setLang(lang); } // number of elemnt per page Integer count = exchange.getIn().getHeader(TwitterConstants.TWITTER_COUNT, Integer.class); if (count == null) { count = te.getProperties().getCount(); } if (ObjectHelper.isNotEmpty(count)) { query.setCount(count); } // number of pages Integer numberOfPages = exchange.getIn().getHeader(TwitterConstants.TWITTER_NUMBER_OF_PAGES, Integer.class); if (numberOfPages == null) { numberOfPages = te.getProperties().getNumberOfPages(); } Twitter twitter = te.getProperties().getTwitter(); log.debug("Searching twitter with keywords: {}", keywords); QueryResult results = twitter.search(query); List<Status> list = results.getTweets(); for (int i = 1; i < numberOfPages; i++) { if (!results.hasNext()) { break; } log.debug("Fetching page"); results = twitter.search(results.nextQuery()); list.addAll(results.getTweets()); } if (te.getProperties().isFilterOld()) { for (Status t : list) { long newId = t.getId(); if (newId > myLastId) { myLastId = newId; } } } exchange.getIn().setBody(list); // update the lastId after finished the processing if (myLastId > lastId) { lastId = myLastId; } }
From source file:org.mule.twitter.TwitterConnector.java
License:Open Source License
/** * Returns tweets that match a specified query. * <p/>/* ww w . j av a2 s . co m*/ * This method calls http://search.twitter.com/search.json * <p/> * {@sample.xml ../../../doc/twitter-connector.xml.sample twitter:search} * * @param query The search query. * @param lang Restricts tweets to the given language, given by an <a href="http://en.wikipedia.org/wiki/ISO_639-1">ISO 639-1 code</a> * @param locale Specify the language of the query you are sending (only ja is currently effective). This is intended for language-specific clients and the default should work in the majority of cases. * @param maxId If specified, returns tweets with status ids less than the given id * @param since If specified, returns tweets since the given date. Date should be formatted as YYYY-MM-DD * @param sinceId Returns tweets with status ids greater than the given id. * @param geocode A {@link String} containing the latitude and longitude separated by ','. Used to get the tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile * @param radius The radius to be used in the geocode -ONLY VALID IF A GEOCODE IS GIVEN- * @param unit The unit of measurement of the given radius. Can be 'mi' or 'km'. Miles by default. * @param until If specified, returns tweets with generated before the given date. Date should be formatted as YYYY-MM-DD * @param resultType If specified, returns tweets included popular or real time or both in the responce. Both by default. Can be 'mixed', 'popular' or 'recent'. * @return the {@link QueryResult} * @throws TwitterException when Twitter service or network is unavailable */ @Processor public QueryResult search(String query, @Optional String lang, @Optional String locale, @Optional Long maxId, @Optional String since, @Optional Long sinceId, @Optional String geocode, @Optional String radius, @Default(value = Query.MILES) @Optional String unit, @Optional String until, @Optional String resultType) throws TwitterException { final Query q = new Query(query); if (lang != null) { q.setLang(lang); } if (locale != null) { q.setLocale(locale); } if (maxId != null && maxId.longValue() != 0) { q.setMaxId(maxId.longValue()); } if (since != null) { q.setSince(since); } if (sinceId != null && sinceId.longValue() != 0) { q.setSinceId(sinceId.longValue()); } if (geocode != null) { final String[] geocodeSplit = StringUtils.split(geocode, ','); final double latitude = Double.parseDouble(StringUtils.replace(geocodeSplit[0], " ", "")); final double longitude = Double.parseDouble(StringUtils.replace(geocodeSplit[1], " ", "")); q.setGeoCode(new GeoLocation(latitude, longitude), Double.parseDouble(radius), unit); } if (until != null) { q.setUntil(until); } if (resultType != null) { q.setResultType(resultType); } return twitter.search(q); }