Skip to content

Instantly share code, notes, and snippets.

@Miguel000
Last active February 6, 2016 21:24
Show Gist options
  • Save Miguel000/6363fa2211ff80f68da8 to your computer and use it in GitHub Desktop.
Save Miguel000/6363fa2211ff80f68da8 to your computer and use it in GitHub Desktop.
/*
The MIT License (MIT)
Copyright (c) 2015 sinfonier-project
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
package com.sinfonier.bolts;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.conf.ConfigurationBuilder;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Calendar;
import java.util.Date;
public class TwitterActive extends BaseSinfonierBolt {
private String CONSUMER_KEY = "consumerKey";
private String CONSUMER_SECRET = "consumerSecret";
private String ACCESS_TOKEN = "accessToken";
private String ACCESS_TOKEN_SECRET = "accessTokenSecret";
private String userfield;
private Integer count;
private long miliLimit = 2678400;
public TwitterActive (String path) {
super(path);
// TODO Auto-generated constructor stub
}
@Override
public void userprepare() {
this.CONSUMER_KEY = (String)this.getParam("consumerKey");
this.CONSUMER_SECRET = (String)this.getParam("consumerSecret");
this.ACCESS_TOKEN = (String)this.getParam("accessToken");
this.ACCESS_TOKEN_SECRET = (String)this.getParam("accessTokenSecret");
this.userfield = (String)this.getParam("userfield");
}
@SuppressWarnings("deprecation")
@Override
public void userexecute() {
ConfigurationBuilder config = new ConfigurationBuilder();
config.setOAuthConsumerKey(this.CONSUMER_KEY);
config.setOAuthConsumerSecret(this.CONSUMER_SECRET);
config.setOAuthAccessToken(this.ACCESS_TOKEN);
config.setOAuthAccessTokenSecret(this.ACCESS_TOKEN_SECRET);
config.setJSONStoreEnabled(true);
config.setIncludeEntitiesEnabled(true);
Boolean isString = false;
Long long_userid = null;
String string_userid = null;
try{
long_userid = Long.parseLong((String)this.getField(this.userfield));
}catch (Exception euid){
string_userid = (String)this.getField(this.userfield);
isString=true;
}
try {
TwitterFactory tf = new TwitterFactory(config.build());
Twitter twitter = tf.getInstance();
List<Status> statuses;
User user = twitter.verifyCredentials();
if (isString){
statuses = twitter.getUserTimeline(string_userid);
}else{
statuses = twitter.getUserTimeline(long_userid);
}
// Tiempo actual
Date dateActual = new Date();
Calendar calendarActual = Calendar.getInstance();
calendarActual.setTime(dateActual);
long miliActual = calendarActual.getTimeInMillis()/1000;
// Si no escribe un tweet desde hace un mes, no esta activa la cuenta.
if((miliActual - (statuses.get(0).getCreatedAt().getTime()/1000)) > (long) miliLimit){
this.addField("userActivity", "NO_ACTIVO");
}else{
long difference = 0;
int cont=0;
for (int i=0; i< statuses.size()-1; i++){
long fecha1 = statuses.get(i).getCreatedAt().getTime()/1000;
long fecha2 = statuses.get(i+1).getCreatedAt().getTime()/1000;
difference = difference + ( fecha1 - fecha2);
cont++;
}
if ((difference/cont) > 259200) {
this.addField("userActivity", "ACTIVO_EXPORADICO");
}else{
this.addField("userActivity", "ACTIVO_HABITUAL");
}
}
}catch (Exception te) {
te.printStackTrace();
this.addField("userfollowerdetail-error", "");
}
this.emit();
}
@Override
public void usercleanup() {
// TODO Auto-generated method stub
}
public static Calendar DateToCalendar(Date date){
Calendar cal = Calendar.getInstance();
cal.setTime(date);
return cal;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment