Example #1
0
 public static void closeConnection() {
   if (twitter != null) {
     Logger.info("Closing down Twitter stream...");
     twitter.cleanUp();
     if (esClient != null) esClient.close();
   } else {
     Logger.info("No twitter stream found");
   }
 }
Example #2
0
 public TweetListener(List<String> terms, Client esClient, String esIndex, String esType) {
   this.terms = terms;
   this.esClient = esClient;
   StringBuilder query = new StringBuilder();
   for (int i = 0; i < terms.size(); i++) {
     if (i > 0) query.append("|");
     // query.append("(").append(Pattern.quote(terms.get(i))).append(")");
     // query.append("(").append(terms.get(i)).append(")");
     query.append(terms.get(i));
   }
   Logger.info("Query match pattern: " + query.toString());
   this.matchPattern = Pattern.compile(query.toString(), Pattern.CASE_INSENSITIVE);
   Logger.info("Query match pattern: " + matchPattern.toString());
 }
Example #3
0
  private static void startStream(List<String> terms) throws TwitterException {
    if (twitter != null) {
      twitter.cleanUp();
    }
    if (esClient != null) {
      esClient.close();
      esClient = null;
    }

    play.Configuration pconf = Play.application().configuration();
    String elasticSearchCluster = pconf.getString("tweet.elasticsearch.cluster.name");
    if (elasticSearchCluster != null) {
      Logger.info("Configuring ElasticSearch...");
      Settings settings =
          ImmutableSettings.settingsBuilder().put("cluster.name", elasticSearchCluster).build();

      esClient =
          new TransportClient(settings)
              .addTransportAddress(
                  new InetSocketTransportAddress(
                      pconf.getString("tweet.elasticsearch.transport.host"),
                      pconf.getInt("tweet.elasticsearch.transport.port")));
    } else {
      esClient = null;
    }

    twitter4j.conf.Configuration tconf = Application.getTwitterConfiguration();
    TwitterStreamFactory tf = new TwitterStreamFactory(tconf);
    twitter = tf.getInstance();
    StatusListener l =
        new TweetListener(
            terms,
            esClient,
            pconf.getString("tweet.elasticsearch.index"),
            pconf.getString("tweet.elasticsearch.type"));
    twitter.addListener(l);

    String[] tracks = new String[terms.size()];
    StringBuffer termsString = new StringBuffer();
    for (int i = 0; i < terms.size(); i++) {
      tracks[i] = terms.get(i);
      if (i != 0) termsString.append(",");
      termsString.append(terms.get(i));
    }
    FilterQuery q = new FilterQuery().track(tracks);
    twitter.filter(q);
    Logger.info("Starting listening for tweets using terms " + termsString.toString() + "...");
  }
Example #4
0
  public static Result start() {
    java.util.Map<String, String[]> map = request().body().asFormUrlEncoded();

    List<String> terms = new ArrayList<>(map.size());
    for (int i = 0; i < map.size(); i++) {
      String key = "terms[" + i + "]";
      if (map.containsKey(key)) {
        String[] values = map.get(key);
        if ((values != null) && (values.length >= 1)) {
          terms.add(values[0]);
        }
      }
    }

    StreamConfig config = getConfig();
    config.putTerms(terms);
    config.update();

    StringBuilder sb = new StringBuilder();
    for (String t : terms) {
      sb.append(t);
      sb.append(", ");
    }
    sb.delete(sb.length() - 2, sb.length());

    try {
      startStream(terms);
      flash("success", "Twitter stream started (" + sb.toString() + ")");
    } catch (TwitterException e) {
      Logger.info("Error starting twitter stream", e);
      flash("error", "Error starting Twitter stream" + e.getMessage());
    }
    return redirect(routes.Streams.listAll());
  }
Example #5
0
 public static void startConnection() {
   StreamConfig config = getConfig();
   try {
     startStream(config.listTerms());
   } catch (TwitterException e) {
     Logger.info("Error starting twitter stream", e);
   }
 }
Example #6
0
 public void onStatus(twitter4j.Status status) {
   Logger.info(status.getUser().getName() + " : " + status.getText());
   Tweet tweet = new Tweet(status);
   tweet.conformsToTerms = checkMatch(status);
   tweet.save();
   if (tweet.conformsToTerms && esClient != null) {
     String json = DataObjectFactory.getRawJSON(status);
     json =
         json.replaceAll(
             "(\"geo\":\\{\"type\":\"Point\",\"coordinates\":)\\[([-0-9.,]*)\\]", "$1\"$2\"");
     // Logger.debug("geo mangled json");
     // Logger.debug(json);
     IndexResponse response =
         esClient.prepareIndex("twitter", "tweet").setSource(json).execute().actionGet();
   }
 }
Example #7
0
  public static Result download() {
    Exporter exporter = new Exporter("/tmp/tweets.xlsx");

    List<Tweet> tweets = Tweet.find.all();

    try {
      for (Tweet t : tweets) {
        SimpleTweet simple = new SimpleTweet();
        simple.id = t.id;
        simple.createdAt = t.date;
        simple.userName = t.fromUser;
        simple.userId = t.fromUserId;
        simple.text = t.text;
        simple.inReplyToName = t.inReplyTo;
        simple.latitude = t.latitude;
        simple.longitude = t.longitude;

        exporter.addTweet(simple);
      }

      exporter.write();
    } catch (FileNotFoundException e) {
      flash("error", "Bestand niet gevonden");
      Logger.info("Bestand niet gevonden", e);
      return ok("Exception opening file");
    }

    //      try {
    response().setContentType("application/x-download");
    response().setHeader("Content-disposition", "attachment; filename=tweets.xlsx");
    return ok(new File("/tmp/tweets.xlsx"));
    /*
      } catch (IOException e) {
      Logger.info("Exception sending zipfile", e);
      return ok("Exception opening file");
    */
    //      }
  }
Example #8
0
 public void onTrackLimitationNotice(int numberOfLimitedStatuses) {
   Logger.info("Track limitation, missed " + numberOfLimitedStatuses);
 }
Example #9
0
 public void onStallWarning(StallWarning warning) {
   Logger.info("Stall warning");
 }
Example #10
0
 public void onScrubGeo(long userId, long upToStatusId) {
   Logger.info("Scrub geo");
 }
Example #11
0
 public void onDeletionNotice(StatusDeletionNotice statusDeletionNotice) {
   Logger.info("Deletion notice");
 }