public static void deleteAllImagesFormDb() { List<MImages> imageList = new ArrayList<MImages>(); imageList = MImages.all(); for (int i = 0; i < imageList.size(); i++) { MImages.delete(imageList.get(i).id); } // for } // deleteAllImagesFormDb;
public static void initHotImages() { Logger.info("----Init hot images"); List<MImages> images = new ArrayList<MImages>(); images = MImages.all(); // initialize global list that holds 4 top hot images hotImages = new ArrayList<MImages>(); // find 4 images with the most likes Collections.sort( images, new Comparator<MImages>() { public int compare(MImages im1, MImages im2) { Long nLikes1 = im1.numberOfLikesIns + im1.numberOfLikesLocall; Long nLikes2 = im2.numberOfLikesIns + im2.numberOfLikesLocall; return nLikes1.compareTo(nLikes2); } }); for (int i = images.size() - 1; i > images.size() - 5; i--) { hotImages.add(images.get(i)); Logger.info( " image id: " + images.get(i).id + " idIns: " + images.get(i).idInstagram + " author: " + images.get(i).authorName + " nlikes: " + (images.get(i).numberOfLikesIns + images.get(i).numberOfLikesLocall)); } // for Logger.info("-----------------------------"); } // initHotImages
public static void localWatchDog() { Logger.info(" --local watchdog"); Long nImages = checkNumberOfNewLocalImages(); if (nImages > 0) { // there are new images to take care of Logger.info(" number of new images: " + nImages); List<String> images = null; // get the list of local images try { images = readLocalFolder(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // TODO check if the image is already in db, double images problem!!! // for each new image for (int i = images.size() - 1; i > images.size() - 1 - nImages; i--) { Logger.info(" image name: " + images.get(i)); // add new images to db Date date = new Date(); String dateS = "" + date.getTime() / 1000; MImages newDbImage = new MImages( images.get(i), urlPath + images.get(i), "local", "Usi Display", "http://pdnet.inf.unisi.ch/devel/public/logo-usi-67540.gif", dateS, (long) 0, (long) 0); MImages.addNew(newDbImage); // send the new image to clients sendImageToClients(newDbImage); } // update the tag count LocalInfo.delete((long) 1); Logger.info(" update the local info +"); LocalInfo.addNew(new LocalInfo(urlPath, getNumberOfLocalImages())); } else if (nImages < 0) { // TODO // some images have been deleted // for now keep them in the db, later consider deleting them // update the tag LocalInfo.delete((long) 1); Logger.info(" update the local info -"); LocalInfo.addNew(new LocalInfo(urlPath, getNumberOfLocalImages())); } Logger.info(" --local watchdog---"); } // checkLocalFolder
public static void updateHotImages() { Logger.info("----update hot images"); List<MImages> images = new ArrayList<MImages>(); images = MImages.all(); // find 4 images with the most likes Collections.sort( images, new Comparator<MImages>() { public int compare(MImages im1, MImages im2) { Long nLikes1 = im1.numberOfLikesIns + im1.numberOfLikesLocall; Long nLikes2 = im2.numberOfLikesIns + im2.numberOfLikesLocall; return nLikes1.compareTo(nLikes2); } }); int k = 0; boolean update = false; for (int i = images.size() - 1; i > images.size() - 5; i--) { if (!hotImages .get(k) .idInstagram .equals(images.get(i).idInstagram)) { // they are not the same update = true; hotImages.get(k).id = images.get(i).id; hotImages.get(k).idInstagram = images.get(i).idInstagram; hotImages.get(k).authorName = images.get(i).authorName; hotImages.get(k).authorPic = images.get(i).authorPic; hotImages.get(k).numberOfLikesIns = images.get(i).numberOfLikesIns; hotImages.get(k).numberOfLikesLocall = images.get(i).numberOfLikesLocall; hotImages.get(k).source = images.get(i).source; hotImages.get(k).timeCreated = images.get(i).timeCreated; hotImages.get(k).url = images.get(i).url; Logger.info( " image id: " + images.get(i).id + " idIns: " + images.get(i).idInstagram + " author: " + images.get(i).authorName + " nlikes: " + (images.get(i).numberOfLikesIns + images.get(i).numberOfLikesLocall)); } else { // update only number of likes hotImages.get(k).numberOfLikesIns = images.get(i).numberOfLikesIns; hotImages.get(k).numberOfLikesLocall = images.get(i).numberOfLikesLocall; } // else k++; } // for if (update) { sendHotImagesToClients(); } Logger.info("-----------------------------"); } // updateHotImages
public static void updateLikesInDbLocal(String imageId, String displayId) { Logger.info(" --update local likes in db for image: " + imageId); List<MImages> images = new ArrayList<MImages>(); images = MImages.all(); // find the right image based on imageId for (int i = 0; i < images.size(); i++) { MImages image = images.get(i); if (image.idInstagram.equals(imageId)) { Logger.info( " i= " + i + " imageId: " + image.idInstagram + " author: " + image.authorName); // update likes in db (local likes +1) // TODO find a way to update the filed, not to delete and add the same image image.updateNumberOfLikesLocal(); MImages.delete(image.id); MImages.addNew( new MImages( image.idInstagram, image.url, "instagram", image.authorName, image.authorPic, image.timeCreated, image.numberOfLikesIns, image.numberOfLikesLocall)); } // if } // for // send the like update to all clients except the source sendLikeUpdateToClients(imageId, displayId); // update the hot images updateHotImages(); Logger.info("-----------------------------"); } // updateLikesInDbLocal
public static void sendImagesToClient(WebSocket.Out<JsonNode> out, String clientId) { Logger.info("----Send imageS to client: " + clientId); List<MImages> imageList = new ArrayList<MImages>(); imageList = MImages.all(); // send maxNumberOfImagesInst to the client int limit = 0; if (maxNumberOfImagesInst < imageList.size()) { limit = imageList.size() - maxNumberOfImagesInst; } for (int i = imageList.size() - 1; i >= limit; i--) { sendImage(imageList.get(i), out, "initImage"); } // for Logger.info("-----------------------------"); } // sendImagesToClient
public static void addLocalImagesToDb() { Logger.info(" --Add local images to db-------------"); List<String> images = null; // get the list of local images try { images = readLocalFolder(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (images != null) { for (int i = 0; i < images.size(); i++) { Logger.info(" image name: " + images.get(i)); // add local image to db // MImages.addNew(new MImages(image.getId(), // image.getImages().getLowResolution().getImageUrl(), "instagram", // caption.getFrom().getFullName(), caption.getFrom().getProfilePicture(), // image.getCreatedTime(), (long)image.getLikes().getCount(), (long)0)); Date date = new Date(); String dateS = "" + date.getTime() / 1000; MImages.addNew( new MImages( images.get(i), urlPath + images.get(i), "local", "Usi Display", "http://pdnet.inf.unisi.ch/devel/public/logo-usi-67540.gif", dateS, (long) 0, (long) 0)); } // for } // if Logger.info(" ------------------------------------"); } // addLocalImagesToDb
// ---------------------------------------------------------------- // ---- Image Database--------------------------------------------- public static void addImagesToDB(Instagram instagram) { Logger.info(" --Add images to db-------------"); TagMediaFeed mediaFeed = getMediaFeed(instagram); List<MediaFeedData> images = mediaFeed.getData(); List<MediaFeedData> imagesAll = new ArrayList<MediaFeedData>(); // add first media feeds to imagesAll for (int i = 0; i < images.size(); i++) { if (imagesAll.size() < maxNumberOfImagesInst) { imagesAll.add(0, images.get(i)); } } // add additional media feed to imagesAll[maxNumberOfImagesInst] String nextMaxId = getPagination(mediaFeed); while ((imagesAll.size() < maxNumberOfImagesInst) && (nextMaxId != null)) { // get new set of images TagMediaFeed nextMediaFeed = getNextMediaFeed(instagram, nextMaxId); List<MediaFeedData> imagesNext = nextMediaFeed.getData(); // add imagesNext to imagesAll for (int i = 0; i < imagesNext.size(); i++) { if (imagesAll.size() < maxNumberOfImagesInst) { imagesAll.add(0, imagesNext.get(i)); } } nextMaxId = getPagination(nextMediaFeed); // Logger.info("imagesNext: "+imagesNext.size()); Logger.info("imagesAll: " + imagesAll.size()); } // while // print all images and add them to the db Logger.info("imagesAll: " + imagesAll.size()); if (imagesAll != null) { int counter = 1; for (MediaFeedData image : imagesAll) { if (image != null) { Date d = new Date(Long.parseLong(image.getCreatedTime()) * 1000); Caption caption = image.getCaption(); Logger.info( " " + (counter++) + "." + " id:" + image.getId() + " time: " + d.toString() + " "); if (caption != null) { Logger.info(" from, full name: " + caption.getFrom().getFullName()); // add image to db MImages.addNew( new MImages( image.getId(), image.getImages().getLowResolution().getImageUrl(), "instagram", caption.getFrom().getFullName(), caption.getFrom().getProfilePicture(), image.getCreatedTime(), (long) image.getLikes().getCount(), (long) 0)); } // if } // if } // for } // if } // addImagesToDB
public static void tagWatchDog(Instagram instagram) { Logger.info(" --tag watchdog"); Long nImages = checkNumberOfNewImages(instagram, appDisplayNameTag); if (nImages > 0) { // there are new images to take care of Logger.info(" number of new images: " + nImages); // get the first page of media feeds (images) TagMediaFeed mediaFeed = getMediaFeed(instagram); List<MediaFeedData> images = mediaFeed.getData(); // TODO: support adding new images from other pages // limit number of new images to the first page if (nImages > images.size()) { // number of new images excides the first page nImages = (long) images.size(); Logger.info(" number of limited images: " + nImages); } // TODO check if the image is already in db, double images problem!!! // for each new image from the first page for (int i = (int) (nImages - 1); i >= 0; i--) { // add new images to db MediaFeedData image = images.get(i); Caption caption = image.getCaption(); MImages newDbImage = new MImages( image.getId(), image.getImages().getLowResolution().getImageUrl(), "instagram", caption.getFrom().getFullName(), caption.getFrom().getProfilePicture(), image.getCreatedTime(), (long) image.getLikes().getCount(), (long) 0); MImages.addNew(newDbImage); Logger.info( " add image to db: index: " + i + " id: " + image.getId() + " from: " + caption.getFrom().getFullName()); // send the new image to clients sendImageToClients(newDbImage); } // update the tag count TagInfo.delete((long) 1); Logger.info(" update the tag +"); TagInfo.addNew( new TagInfo(appDisplayNameTag, getNumberOfImagesByTag(instagram, appDisplayNameTag))); displayTagInfo(); } else if (nImages < 0) { // TODO // some images have been deleted // for now keep them in the db, later consider deleting them // update the tag TagInfo.delete((long) 1); Logger.info(" update the tag -"); TagInfo.addNew( new TagInfo(appDisplayNameTag, getNumberOfImagesByTag(instagram, appDisplayNameTag))); displayTagInfo(); } Logger.info(" --tag watchdog---"); } // tagWatchDog()