accounting-dashboard-harves.../src/main/java/org/gcube/dataharvest/harvester/SocialInteractionsHarvester...

97 lines
2.3 KiB
Java

package org.gcube.dataharvest.harvester;
import java.io.IOException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.gcube.dataharvest.datamodel.HarvestedData;
import org.gcube.dataharvest.datamodel.HarvestedDataKey;
import org.gcube.dataharvest.utils.Utils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* @author Eric Perrone (ISTI - CNR)
* @author Luca Frosini (ISTI - CNR)
*/
public class SocialInteractionsHarvester extends SocialNetworkingHarvester {
private static Logger logger = LoggerFactory.getLogger(SocialInteractionsHarvester.class);
private int likes;
private int replies;
private int posts;
public static final String PATH = "/2/posts/get-posts-vre?gcube-token=";
public SocialInteractionsHarvester(Date start, Date end) throws ParseException {
super(start, end);
}
@Override
public List<HarvestedData> getData() throws Exception {
String context = Utils.getCurrentContext();
try {
ArrayList<HarvestedData> data = new ArrayList<HarvestedData>();
getJson();
HarvestedData likesH = new HarvestedData(HarvestedDataKey.SOCIAL_LIKES, context, likes);
logger.debug("{}", likesH);
data.add(likesH);
HarvestedData postsH = new HarvestedData(HarvestedDataKey.SOCIAL_POSTS, context, posts);
logger.debug("{}", postsH);
data.add(postsH);
HarvestedData socialReplies = new HarvestedData(HarvestedDataKey.SOCIAL_REPLIES, context, replies);
logger.debug("{}", socialReplies);
data.add(socialReplies);
return data;
} catch(Exception e) {
logger.error("Error Harvesting Social Interactions for context {}", context, e);
throw e;
}
}
private void getJson() throws Exception {
JSONObject jsonObject = getJSONObject(PATH);
Boolean success = (Boolean) jsonObject.get("success");
if(success == false) {
throw new IOException("Erro while getting posts");
}
JSONArray res = jsonObject.getJSONArray("result");
int len = res.length();
likes = replies = posts = 0;
for(int i = 0; i < len; i++) {
JSONObject item = res.getJSONObject(i);
long time = item.getLong("time");
if(startDate.getTime() <= time && time <= endDate.getTime()) {
posts++;
replies += item.getInt("comments_no");
likes += item.getInt("likes_no");
}
}
}
}