List of usage examples for twitter4j FilterQuery locations
null locations
To view the source code for twitter4j FilterQuery locations.
Click Source Link
From source file:adapter.TwitterLocationAdapter.java
License:Apache License
public void connectAndRead() throws Exception { ConfigurationBuilder cb = new ConfigurationBuilder(); Properties twitterProperties = new Properties(); File twitter4jPropsFile = new File("../twitter4j.properties"); if (!twitter4jPropsFile.exists()) { logger.error("Cannot find twitter4j.properties file in this location :[{}]", twitter4jPropsFile.getAbsolutePath()); return;/*w w w.j a v a2 s .c om*/ } twitterProperties.load(new FileInputStream(twitter4jPropsFile)); cb = new ConfigurationBuilder(); cb.setOAuthConsumerKey(twitterProperties.getProperty("oauth.consumerKey")); cb.setOAuthConsumerSecret(twitterProperties.getProperty("oauth.consumerSecret")); cb.setOAuthAccessToken(twitterProperties.getProperty("oauth.accessToken")); cb.setOAuthAccessTokenSecret(twitterProperties.getProperty("oauth.accessTokenSecret")); cb.setDebugEnabled(false); cb.setPrettyDebugEnabled(false); cb.setIncludeMyRetweetEnabled(false); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); StatusListener statusListener = new StatusListener() { @Override public void onException(Exception ex) { } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } @Override public void onStatus(Status status) { messageQueue.add(status); } @Override public void onScrubGeo(long userId, long upToStatusId) { } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } @Override public void onStallWarning(StallWarning arg0) { } }; FilterQuery fq = new FilterQuery(); // Posiciones geogrficas, esas dos coordenadas son el vrtice superior izquierda e inferior derecho // de un rectangulo, de tal manera de analizar solo una parte geografica double position[][] = { { 0.0, 0.0 }, { 0.0, 0.0 } }; fq.locations(position); twitterStream.addListener(statusListener); twitterStream.filter(fq); }
From source file:at.illecker.sentistorm.spout.TwitterStreamSpout.java
License:Apache License
@Override public void open(Map config, TopologyContext context, SpoutOutputCollector collector) { m_collector = collector;/* ww w. ja v a2 s. co m*/ m_tweetsQueue = new LinkedBlockingQueue<Status>(1000); // Optional startup sleep to finish bolt preparation // before spout starts emitting if (config.get(CONF_STARTUP_SLEEP_MS) != null) { long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS); TimeUtils.sleepMillis(startupSleepMillis); } TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); // Set Listener twitterStream.addListener(new StatusListener() { @Override public void onStatus(Status status) { m_tweetsQueue.offer(status); // add tweet into queue } @Override public void onException(Exception arg0) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStallWarning(StallWarning arg0) { } @Override public void onTrackLimitationNotice(int arg0) { } }); // Set credentials twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret); AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret); twitterStream.setOAuthAccessToken(token); // Filter twitter stream FilterQuery tweetFilterQuery = new FilterQuery(); if (m_keyWords != null) { tweetFilterQuery.track(m_keyWords); } // Filter location // https://dev.twitter.com/docs/streaming-apis/parameters#locations tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any geotagged tweet // Filter language tweetFilterQuery.language(new String[] { m_filterLanguage }); twitterStream.filter(tweetFilterQuery); }
From source file:at.storm.spout.TwitterStreamSpout.java
License:Apache License
@SuppressWarnings("rawtypes") @Override//w w w .j a v a 2 s .c om public void open(Map config, TopologyContext context, SpoutOutputCollector collector) { m_collector = collector; m_tweetsQueue = new LinkedBlockingQueue<Status>(1000); // Optional startup sleep to finish bolt preparation // before spout starts emitting if (config.get(CONF_STARTUP_SLEEP_MS) != null) { long startupSleepMillis = (Long) config.get(CONF_STARTUP_SLEEP_MS); TimeUtils.sleepMillis(startupSleepMillis); } TwitterStream twitterStream = new TwitterStreamFactory( new ConfigurationBuilder().setJSONStoreEnabled(true).build()).getInstance(); // Set Listener twitterStream.addListener(new StatusListener() { @Override public void onStatus(Status status) { m_tweetsQueue.offer(status); // add tweet into queue } @Override public void onException(Exception arg0) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onStallWarning(StallWarning arg0) { } @Override public void onTrackLimitationNotice(int arg0) { } }); // Set credentials twitterStream.setOAuthConsumer(m_consumerKey, m_consumerSecret); AccessToken token = new AccessToken(m_accessToken, m_accessTokenSecret); twitterStream.setOAuthAccessToken(token); // Filter twitter stream FilterQuery tweetFilterQuery = new FilterQuery(); if (m_keyWords != null) { tweetFilterQuery.track(m_keyWords); } // Filter location // https://dev.twitter.com/docs/streaming-apis/parameters#locations tweetFilterQuery.locations(new double[][] { new double[] { -180, -90, }, new double[] { 180, 90 } }); // any // geotagged // tweet // Filter language tweetFilterQuery.language(new String[] { m_filterLanguage }); twitterStream.filter(tweetFilterQuery); }
From source file:bditac.TwitterCaptura.java
public static void main(String[] args) throws TwitterException { int criid, apiid, capid; if (args.length == 3) { criid = Integer.parseInt(args[0]); apiid = Integer.parseInt(args[1]); capid = Integer.parseInt(args[2]); } else {/*from www . j a v a 2 s . c om*/ criid = 1; apiid = 1; capid = 1; } CriseJpaController crijpa = new CriseJpaController(factory); Crise crise = crijpa.findCrise(criid); CriseApiJpaController criapijpa = new CriseApiJpaController(factory); CriseApi criapi = criapijpa.findCriseApi(capid); ApiJpaController apijpa = new ApiJpaController(factory); Api api = apijpa.findApi(apiid); CidadeJpaController cidjpa = new CidadeJpaController(factory); Cidade cidade = cidjpa.findCidade(crise.getCidId()); String coords[] = crise.getCriRegiao().split(","); TwitterGeo geo = new TwitterGeo(coords, cidade.getCidNome(), crise.getCriGeotipo()); Thread threadGeo = new Thread(geo); TwitterMens mens = new TwitterMens(crise.getCrtId()); Thread threadMens = new Thread(mens); TwitterGravar gravar = new TwitterGravar(); Thread threadGravar = new Thread(gravar); threadGeo.setName("ThreadGeo"); threadGeo.start(); threadMens.setName("ThreadMens"); threadMens.start(); threadGravar.setName("ThreadGravar"); threadGravar.start(); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true).setOAuthConsumerKey(criapi.getCapKey()) .setOAuthConsumerSecret(criapi.getCapSecret()).setOAuthAccessToken(criapi.getCapToken()) .setOAuthAccessTokenSecret(criapi.getCapTokenSecret()); TwitterStream twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); String texto = "[\\xF0\\x9F]"; StatusListener listener; listener = new StatusListener() { int cont = 0; @Override public void onStatus(Status status) { if (!(status.getText().contains(texto) || status.getText().isEmpty())) { Ocorrencia ocor = new Ocorrencia(); ocor.setApiId(apiid); ocor.setCriId(criid); ocor.setCapId(capid); ocor.setOcrIdApi(status.getId()); ocor.setOcrCriacao(status.getCreatedAt()); ocor.setOcrTexto(status.getText()); // System.out.println(ocor.getOcrTexto()); ocor.setOcrUsuId(status.getUser().getId()); ocor.setOcrUsuNome(status.getUser().getName()); ocor.setOcrUsuScreenNome(status.getUser().getScreenName()); ocor.setOcrFonte(status.getSource()); ocor.setOcrLingua(status.getLang()); ocor.setOcrFavorite(status.getFavoriteCount()); ocor.setOcrRetweet(status.getRetweetCount()); String coords = ""; if (status.getPlace() == null) { ocor.setOcrPaisCodigo(""); ocor.setOcrPais(""); ocor.setOcrLocal(""); } else { ocor.setOcrPaisCodigo(status.getPlace().getCountryCode()); ocor.setOcrPais(status.getPlace().getCountry()); ocor.setOcrLocal(status.getPlace().getFullName()); GeoLocation locs[][] = status.getPlace().getBoundingBoxCoordinates(); for (int x = 0; x < locs.length; x++) { for (int y = 0; y < locs[x].length; y++) { coords += "[" + locs[x][y].getLongitude() + "," + locs[x][y].getLatitude() + "]"; if (!(x == locs.length - 1 && y == locs[x].length - 1)) { coords += ","; } } } } ocor.setOcrCoordenadas(coords); ocor.setOcrGeo('0'); ocor.setOcrIdentificacao('0'); ocor.setOcrIdenper(0.0f); ocor.setOcrGravado('0'); ocor.setOcrSentimento('0'); ocor.setOcrTempo('0'); boolean add = ocors.add(ocor); cont++; if (ocors.size() > 1000) { Limpar(); } // System.out.println(cont+" - "+status.getId() + " - " + status.getCreatedAt() + status.getPlace().getFullName()); } } @Override public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } @Override public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } @Override public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } @Override public void onStallWarning(StallWarning warning) { System.out.println("Got stall warning:" + warning); } @Override public void onException(Exception ex) { ex.printStackTrace(); } private void Limpar() { while (!threadGeo.isInterrupted()) { threadGeo.interrupt(); } while (!threadMens.isInterrupted()) { threadMens.interrupt(); } while (!threadGravar.isInterrupted()) { threadGravar.interrupt(); } boolean achou = true; int x = 0; System.out.println("Removendo: " + ocors.size()); while (x < ocors.size()) { if (ocors.get(x).getOcrGravado() != '0') { ocors.remove(x); } else { x++; } } System.out.println("Final: " + ocors.size()); if (!threadGeo.isAlive()) { threadGeo.start(); } if (!threadMens.isAlive()) { threadMens.start(); } if (!threadGravar.isAlive()) { threadGravar.start(); } } }; FilterQuery filter = new FilterQuery(); double[][] location = new double[2][2]; location[0][0] = Double.parseDouble(coords[0]); location[0][1] = Double.parseDouble(coords[1]); location[1][0] = Double.parseDouble(coords[4]); location[1][1] = Double.parseDouble(coords[5]); filter.locations(location); twitterStream.addListener(listener); twitterStream.filter(filter); }
From source file:com.esri.geoevent.transport.twitter.TwitterInboundTransport.java
License:Apache License
private void receiveData() { try {//from w w w. j a v a 2s . c o m applyProperties(); setRunningState(RunningState.STARTED); ConfigurationBuilder cb = new ConfigurationBuilder(); cb.setDebugEnabled(true); cb.setOAuthConsumerKey(consumerKey); cb.setOAuthConsumerSecret(consumerSecret); cb.setOAuthAccessToken(accessToken); cb.setOAuthAccessTokenSecret(accessTokenSecret); twitterStream = new TwitterStreamFactory(cb.build()).getInstance(); RawStreamListener rl = new RawStreamListener() { @Override public void onException(Exception ex) { LOGGER.error("INBOUND_TRANSPORT_RAW_STREAM_LISTERNER_EXCEPTION", ex.getMessage()); } @Override public void onMessage(String rawString) { receive(rawString); } }; FilterQuery fq = new FilterQuery(); String keywords[] = tracks; if (follows != null && follows.length > 0) fq.follow(follows); else if (keywords != null && keywords.length > 0) fq.track(keywords); else if (locations != null) fq.locations(locations); else throw new Exception("INBOUND_TRANSPORT_NOFILTER_ERROR"); fq.count(count); LOGGER.info("INBOUND_TRANSPORT_FILTER", filterString); twitterStream.addListener(rl); twitterStream.filter(fq); } catch (Throwable ex) { LOGGER.error("UNEXPECTED_ERROR", ex); setRunningState(RunningState.ERROR); } }
From source file:com.github.jiloc.USTweetsAnalyzer.Streamer.java
public void startListening() { // sample() method internally creates a thread which manipulates // TwitterStream // and calls these adequate listener methods continuously. this.twitterStream.addListener(listener); System.out.println("Opening stream. Start listening.."); //this.twitterStream.sample(); // 1 % of the public stream FilterQuery fq = new FilterQuery();// the filter object fq.locations(LOCATIONS); // fq.language(language); // fq.track(query); //the query to track from the stream this.twitterStream.filter(fq); }
From source file:com.telefonica.iot.cygnus.sources.TwitterSource.java
License:Open Source License
@Override public synchronized void start() { LOGGER.info("Starting twitter source {} ...", this); documentCount = 0;//from www . j a va 2 s . c om startTime = System.currentTimeMillis(); exceptionCount = 0; totalTextIndexed = 0; skippedDocs = 0; batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis; final ChannelProcessor channel = getChannelProcessor(); StatusListener listener = new StatusListener() { public void onStatus(Status status) { String jsonTweet = getStringJSONTweet(status); Event event = EventBuilder.withBody(jsonTweet, Charset.forName("UTF8")); eventBatch.add(event); if (eventBatch.size() >= maxBatchSize || System.currentTimeMillis() >= batchEndTime) { batchEndTime = System.currentTimeMillis() + maxBatchDurationMillis; channel.processEventBatch(eventBatch); // send batch of events (one per tweet) to the flume sink eventBatch.clear(); } documentCount++; if ((documentCount % REPORT_INTERVAL) == 0) { LOGGER.info(String.format("Processed %s docs", numFormatter.format(documentCount))); } if ((documentCount % STATS_INTERVAL) == 0) { logStats(); } } // This listener will ignore everything except for new tweets public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { } public void onScrubGeo(long userId, long upToStatusId) { } public void onStallWarning(StallWarning warning) { } public void onException(Exception e) { LOGGER.error("Exception while streaming tweets", e); } }; twitterStream.addListener(listener); if (haveFilters) { FilterQuery filterQuery = new FilterQuery(); if (haveCoordinateFilter) { filterQuery.locations(boundingBox); LOGGER.info("Coordinates added to filter query: {}", boundingBox[0][0] + " " + boundingBox[0][1] + " " + boundingBox[1][0] + " " + boundingBox[1][1]); } if (haveKeywordFilter) { filterQuery.track(splitKeywords); LOGGER.info("Keywords added to filter query: {}", Arrays.toString(splitKeywords)); } twitterStream.filter(filterQuery); LOGGER.info("Filter Query created: {}", filterQuery.toString()); } LOGGER.info("Twitter source {} started.", getName()); super.start(); }
From source file:com.tilab.ca.sda.ctw.connector.TwitterReceiver.java
@Override public void onStart() { try {/*from w w w .j a va2 s . c o m*/ log.info(String.format("[%s] CALLED ONSTART on Twitter Receiver...", Constants.TW_RECEIVER_LOG_TAG)); log.debug(String.format("[%s] Init twitterStreamFactory...", Constants.TW_RECEIVER_LOG_TAG)); TwitterStream twStream = new TwitterStreamFactory(twStreamConf).getInstance(); log.debug(String.format("[%s] twitterStreamFactory initialized!", Constants.TW_RECEIVER_LOG_TAG)); log.debug(String.format("[%s] adding status listener..", Constants.TW_RECEIVER_LOG_TAG)); twStream.addListener(new StatusListener() { @Override public void onException(Exception e) { log.error(String.format("[%s] Exception on twitterStream", Constants.TW_RECEIVER_LOG_TAG), e); restart("Error on receiving tweets!", e); } @Override public void onTrackLimitationNotice(int notDelivered) { log.warn(String.format( "[%s] WARNING:number of statuses matching keywords but not delivered => %d", Constants.TW_RECEIVER_LOG_TAG, notDelivered)); } @Override public void onStatus(Status status) { log.debug(String.format("[%s] new status received! storing on spark..", Constants.TW_RECEIVER_LOG_TAG)); store(status); } @Override public void onStallWarning(StallWarning stallW) { log.warn(String.format("[%s] WARNING: Received Stall Warning message: %s", Constants.TW_RECEIVER_LOG_TAG, stallW.getMessage())); } @Override public void onScrubGeo(long arg0, long arg1) { } @Override public void onDeletionNotice(StatusDeletionNotice arg0) { } }); log.debug(String.format("[%s] Creating filterquery", Constants.TW_RECEIVER_LOG_TAG)); FilterQuery fq = new FilterQuery(); if (trackArray != null) { log.debug(String.format("[%s] added keys to track", Constants.TW_RECEIVER_LOG_TAG)); fq.track(trackArray); } if (locationFilterArray != null) { log.info(String.format("[%s] added geolocations to track", Constants.TW_RECEIVER_LOG_TAG)); fq.locations(locationFilterArray); } if (user2FollowArray != null) { log.debug(String.format("[%s] Added users to track", Constants.TW_RECEIVER_LOG_TAG)); fq.follow(user2FollowArray); } twStream.filter(fq); log.debug(String.format("[%s] Setting twitterStream..", Constants.TW_RECEIVER_LOG_TAG)); setTwitterStream(twStream); log.info(String.format("[%s] Twitter Stream started", Constants.TW_RECEIVER_LOG_TAG)); } catch (Throwable t) { log.error(String.format("[%s] Error starting twitter stream", Constants.TW_RECEIVER_LOG_TAG), t); restart("Error starting twitter stream", t); } }
From source file:com.waves_rsp.ikb4stream.datasource.twitter.TwitterProducerConnector.java
License:Open Source License
/** * Listen tweets from twitter with a bounding box and load them with the data producer object * * @param dataProducer {@link IDataProducer} contains the data queue * @see TwitterProducerConnector#confBuilder * @see TwitterProducerConnector#boundingBox *//*from w ww.ja v a2s .co m*/ @Override public void load(IDataProducer dataProducer) { Objects.requireNonNull(dataProducer); TwitterStream twitterStream = null; try { TwitterStreamListener streamListener = new TwitterStreamListener(dataProducer); twitterStream = new TwitterStreamFactory(confBuilder.build()).getInstance(); FilterQuery filterQuery = new FilterQuery(); filterQuery.locations(boundingBox); twitterStream.addListener(streamListener); twitterStream.filter(filterQuery); Thread.currentThread().join(); } catch (IllegalArgumentException | IllegalStateException err) { LOGGER.error("Error loading : " + err.getMessage()); throw new IllegalStateException(err.getMessage()); } catch (InterruptedException e) { LOGGER.info("Close twitter"); Thread.currentThread().interrupt(); } finally { if (twitterStream != null) { twitterStream.cleanUp(); twitterStream.shutdown(); } Thread.currentThread().interrupt(); } }
From source file:crawling.PrintFilterStreamGeo.java
License:Apache License
public static void main(String[] args) throws TwitterException { Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { /* my shutdown code here */ // Record the user status idOut.println("Final status -------------------:"); for (Long id : discoveredUsers.keySet()) { idOut.println(id + "," + discoveredUsers.get(id)); }//w w w . j a va 2 s .c om idOut.close(); } }); if (args.length < 5) { System.out.println("Usage: java twitter4j.examples.PrintFilterStreamGeo long1 lati1 long2 lati2 CITY"); System.exit(-1); } if (args.length == 6) { // Preload the collected user IDs preloadID(args[5]); } File file = new File(fileName); InputStream is = null; try { if (file.exists()) { is = new FileInputStream(file); prop.load(is); } else { System.out.println(fileName + " doesn't exist!"); System.exit(-1); } } catch (IOException ioe) { ioe.printStackTrace(); System.exit(-1); } StatusListener listener = new StatusListener() { public void onStatus(Status status) { //System.out.println("@" + status.getUser().getScreenName() + " - " + status.getText()); CheckUser(status); } private void CheckUser(Status status) { // TODO Auto-generated method stub Long id = status.getUser().getId(); /*String username = status.getUser().getScreenName(); String realname = status.getUser().getName(); String text = status.getText(); Date date = status.getCreatedAt();*/ if (discoveredUsers.containsKey(id)) { //System.out.println("Already found this user: " + id); long num = discoveredUsers.get(id); discoveredUsers.put(id, num + 1); } else { discoveredUsers.put(id, (long) 1); storeUserID(status); } } public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) { System.out.println("Got a status deletion notice id:" + statusDeletionNotice.getStatusId()); } public void onTrackLimitationNotice(int numberOfLimitedStatuses) { System.out.println("Got track limitation notice:" + numberOfLimitedStatuses); } public void onScrubGeo(long userId, long upToStatusId) { System.out.println("Got scrub_geo event userId:" + userId + " upToStatusId:" + upToStatusId); } public void onException(Exception ex) { ex.printStackTrace(); } @Override public void onStallWarning(StallWarning arg0) { // TODO Auto-generated method stub } }; try { FileWriter outFile = new FileWriter("discoveredUser" + args[4] + ".txt", true); idOut = new PrintWriter(outFile); //out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } //TwitterStream twitterStream = new TwitterStreamFactory().getInstance(); TwitterStream twitterStream = getOAuthTwitterStream(); twitterStream.addListener(listener); /*ArrayList<Long> follow = new ArrayList<Long>(); ArrayList<String> track = new ArrayList<String>(); long[] followArray = new long[follow.size()]; for (int i = 0; i < follow.size(); i++) { followArray[i] = follow.get(i); } String[] trackArray = track.toArray(new String[track.size()]);*/ // Geographic location double[][] location = new double[2][2]; //location[0] = new double[4]; //location[1] = new double[4]; location[0][0] = Double.parseDouble(args[0]); location[0][1] = Double.parseDouble(args[1]); location[1][0] = Double.parseDouble(args[2]); location[1][1] = Double.parseDouble(args[3]); // filter() method internally creates a thread which manipulates TwitterStream and calls these adequate listener methods continuously. //twitterStream.filter(new FilterQuery(0, followArray, trackArray, location)); FilterQuery query = new FilterQuery(); query.locations(location); twitterStream.filter(query); }