Package saveReddit.main

Source Code of saveReddit.main.Sorter

package saveReddit.main;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import org.json.simple.JSONObject;

import saveReddit.gui.MainWindow;
import saveReddit.parser.htmlParser;
import saveReddit.parser.jsonParser;

public class Sorter {
 
  /**
   * @author Pascal Romahn
   * @Version Version 0.1
   */
 
  private MainWindow mw;
  private fileIO fileIO;
  private htmlParser htmlParser;
  private jsonParser jsonParser;
 
  private LinkedList<JSONObject> content;
  private LinkedList<JSONObject> contentSelf;
  private LinkedList<JSONObject> contentImages;
  private LinkedList<JSONObject> contentImgurAlbums;
  private LinkedList<JSONObject> contentImgur;
  private LinkedList<JSONObject> contentOther;
 
  private HashMap<String, LinkedList<JSONObject>> subredditsImagesMap;
 
  Integer totalContents;
  Integer totalImages;
 
  public Sorter(MainWindow pMW, fileIO pFileIO, jsonParser pJsonParser, LinkedList<JSONObject> pContent) {
    mw = pMW;
    fileIO = pFileIO;
    htmlParser = new htmlParser(mw);
    jsonParser = pJsonParser;
   
    content = pContent;
    contentSelf = new LinkedList<JSONObject>();
    contentImages = new LinkedList<JSONObject>();
    contentImgurAlbums = new LinkedList<JSONObject>();
    contentImgur = new LinkedList<JSONObject>();
    contentOther = new LinkedList<JSONObject>();
   
    subredditsImagesMap = null;
   
    this.sortContent();
    this.parseImgurAlbums();
    this.parseImgurPages();
    this.sortSubreddit();
  }
 
  private void sortContent() {
    mw.mainOutputAddLine("Sorting content...");
    while(content.peekFirst() != null) {
      JSONObject post = content.poll();
      JSONObject postData = (JSONObject) post.get("data");
     
      if(((String)post.get("kind")).equals("t3")) {
        if((boolean)postData.get("is_self") == true) {
          //Check for self posts
          contentSelf.add(post);     
        } else if (fileIO.checkURLForImage((String)postData.get("url")) == true) {
          //Check for images
          contentImages.add(post);
        } else if(((String)postData.get("url")).contains("imgur.com/a/")) {
          //Check for imgur album
          contentImgurAlbums.add(post);
        } else if(((String)postData.get("url")).substring(((String)postData.get("url")).indexOf(':'), ((String)postData.get("url")).lastIndexOf('.')).equals("://imgur")) {
          //Check for imgur sites on ://imgur.com (first part removed to support http and https)
          contentImgur.add(post);
        } else {
          contentOther.add(post);
        }
      }
    }
    //Output content size
    mw.mainOutputAddLine("Content sorted!");
    mw.mainOutputAddLine("Found: " + contentSelf.size() + " self posts");
    mw.mainOutputAddLine("Found: " + contentImages.size() + " images");
    mw.mainOutputAddLine("Found: " + contentImgurAlbums.size() + " Imgur-Albums");
    mw.mainOutputAddLine("Found: " + contentImgur.size() + " Imgur-sites");
    mw.mainOutputAddLine("Found: " + contentOther.size() + " other contents");
    totalContents = contentSelf.size()+contentImages.size()+contentImgurAlbums.size()+contentImgur.size()+contentOther.size();
    mw.mainOutputAddLine("Total of " + totalContents.toString() + " posts");
  }
 
  @SuppressWarnings("unchecked")
  private void parseImgurAlbums() {
    mw.mainOutputAddLine("Parsing Imgur-Albums");
    LinkedList<JSONObject> tempContentImgurAlbums = (LinkedList<JSONObject>) contentImgurAlbums.clone();
    int count = 0;
    while(tempContentImgurAlbums.peekFirst() != null) {
      LinkedList<String> urlsList = new LinkedList<String>();
      JSONObject post = tempContentImgurAlbums.poll();
      JSONObject postData = (JSONObject) post.get("data");
      String url = (String)postData.get("url");
      String title = "album_title";
      try {
        title = htmlParser.parseTitleFromImgurAlbum(url);
        urlsList = htmlParser.parseImagesFromImgurAlbum(url);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      String urlsString = jsonParser.linkedListToJSONArray(urlsList);
      post.put("kind", "album");
      postData.put("title", title);
      postData.put("url", urlsString);
      contentImages.add(post);
      count++;
    }
    mw.mainOutputAddLine(count + " Imgur-Albums have been parsed");
  }
 
  private void parseImgurPages() {
    mw.mainOutputAddLine("Parsing Imgur-Pages...");
    LinkedList<JSONObject> tempContentImgur = (LinkedList<JSONObject>) contentImgur.clone();
    int count = 0;
    while(tempContentImgur.peekFirst() != null) {
      JSONObject post = tempContentImgur.poll();
      JSONObject postData = (JSONObject) post.get("data");
      String url = (String)postData.get("url");
      String newURL = null;
      try {
        newURL = htmlParser.parseImageURLFromImgurSite(url);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      if(newURL.equals("FAILED")) {
        mw.mainOutputAddLine("Parsing Imgur site to source failed!");
      } else {
        newURL = fileIO.correctURLEnding(newURL);
        postData.put("url", newURL);
        contentImages.add(post);
        count++;
      }
    }
    mw.mainOutputAddLine(count + " Imgur-Pages have been parsed!");
  }
 
  private void sortSubreddit() {
    mw.mainOutputAddLine("Sorting subreddits...");
    //Images
    subredditsImagesMap = new HashMap<String, LinkedList<JSONObject>>();
    LinkedList<JSONObject> tempContentImages = (LinkedList<JSONObject>) contentImages.clone();
    while(tempContentImages.peekFirst() != null) {
      JSONObject post = tempContentImages.poll();
      JSONObject postData = (JSONObject) post.get("data");
      String subreddit = (String)postData.get("subreddit");
      if(subredditsImagesMap.containsKey(subreddit) == true) {
        LinkedList<JSONObject> subredditList = subredditsImagesMap.get(subreddit);
        subredditList.add(post);
      } else {
        LinkedList<JSONObject> subredditList = new LinkedList<JSONObject>();
        subredditList.add(post);
        subredditsImagesMap.put(subreddit, subredditList);
      }
    }
    //Output subreddit size
    mw.mainOutputAddLine("Subreddits sorted!");
    Iterator iterator = subredditsImagesMap.entrySet().iterator();
    totalImages = 0;
    while(iterator.hasNext() == true) {
      Entry entry = (Entry) iterator.next();
      LinkedList<JSONObject> list = (LinkedList<JSONObject>) entry.getValue();
      mw.mainOutputAddLine("Found: " + list.size() + " in subreddit  " + entry.getKey());
      totalImages += list.size();
    }
    mw.mainOutputAddLine("Total of " + totalImages.toString() + " images");
  }
 
  public HashMap<String, LinkedList<JSONObject>> getImagesMap() {
    return subredditsImagesMap;
  }
 
  public Integer getTotalContents() {
    return totalContents;
  }
 
  public Integer getTotalImages() {
    return totalImages;
  }
}
TOP

Related Classes of saveReddit.main.Sorter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.