package org.gcube.dataharvest.harvester; import java.io.IOException; import java.net.MalformedURLException; import java.text.ParseException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Properties; import org.gcube.dataharvest.datamodel.Harvest; import org.gcube.dataharvest.utils.Utils; import org.json.JSONArray; import org.json.JSONObject; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class SocialHarvester extends BasicHarvester { private final String CATEGORY_NAME = "Accounting"; private static Logger logger = LoggerFactory.getLogger(VreUsersHarvester.class); private int likes, replies, posts; private long from = 0, to = 0; public SocialHarvester(String start, String end) throws ParseException { super(start, end); this.from = startDate.getTime(); this.to = endDate.getTime(); } public SocialHarvester(Date start, Date end) throws ParseException { super(start, end); this.from = startDate.getTime(); this.to = endDate.getTime(); } @Override public List getData() throws Exception { ArrayList data = new ArrayList(); Properties props = readServiceEndpoint(); String[] vres = getActiveVREs(true); for (String vre : vres) { try { logger.debug("Working on VRE: " + vre); String key = vre; // vre.substring(vre.lastIndexOf("/") + 1); String token = props.getProperty(key); //System.out.println(key + ":" + token); getJson(token); //getFinalResult(); Harvest harvest = new Harvest(Harvest.SOCIAL_LIKES, vre, likes); data.add(harvest); logger.debug(harvest.toString()); harvest = new Harvest(Harvest.SOCIAL_POSTS, vre, posts); data.add(harvest); logger.debug(harvest.toString()); harvest = new Harvest(Harvest.SOCIAL_REPLIES, vre, replies); data.add(harvest); logger.debug(harvest.toString()); } catch (Exception x) { logger.error("SocialHarvester::getJson. " + vre + ". " + x.getLocalizedMessage()); } } return data; } private void getJson(String token) throws MalformedURLException, IOException { likes = replies = posts = 0; // la seguente stringa deve essere letta dinamicamente String url = "https://socialnetworking1.d4science.org/social-networking-library-ws/rest/2/posts/get-posts-vre?gcube-token="; JSONObject jsonObject = new JSONObject(Utils.getJson(url + token)); Boolean success = (Boolean) jsonObject.get("success"); if (success == false) { String message = "getJson() on token: " + token + " success=false"; logger.error(message); throw new IOException(message); } JSONArray res = jsonObject.getJSONArray("result"); int len = res.length(); for (int i = 0; i < len; i++) { JSONObject item = res.getJSONObject(i); long time = item.getLong("time"); //System.out.println(from + " - " + time + " - " + to ); if ((from <= time) && (time <= to)) { posts++; replies += item.getInt("comments_no"); likes += item.getInt("likes_no"); } } } /**/ public static void main(String[] argv) { try { SocialHarvester a = new SocialHarvester("2018-01-01 00:00:00", "2018-01-31 23:59:59"); List list = a.getData(); for (Harvest l : list) { System.out.println(l.toString()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }/**/ }