Package saveReddit.main

Source Code of saveReddit.main.MainApplication

package saveReddit.main;
import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map.Entry;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.ParseException;

import saveReddit.gui.MainWindow;
import saveReddit.parser.jsonParser;
import saveReddit.settings.SettingsIO;

public class MainApplication {
 
  /**
   * @author Pascal Romahn
   * @version Version 0.1
   */
 
  private MainWindow mw;
  private fileIO fileIO;
  private SettingsIO settingsIO;
  private Sorter sorter;
 
  private LinkedList<JSONObject> content;
  private LinkedList<String> userList;
 
  private int year;
 
  private String currentUser;
  private String currentLastSave;
  private String currentLastSaveBackup;
  private Boolean lastSaveBoolean;
 
  private boolean overwrite;

 
  public MainApplication(MainWindow pMW) {
   
    mw = pMW;
    fileIO = new fileIO(mw, this);
    settingsIO = new SettingsIO(mw, fileIO);
    sorter = null;
   
    content = null;
    userList = settingsIO.getUsers();
   
    mw.setMainOutput("Application ready!");
   
    year = Calendar.getInstance().get(Calendar.YEAR)
   
    //currentUser = mw.getSelectedUser();
   
    this.setOverwrite(false);
  }
 
  public void runParser() {
    //content = null;
   
    if(lastSaveBoolean == true) {
      Thread parserThread = new Thread("Parser") {
        public void run() {
          startParsing();
        }
      };
      parserThread.start();
    } else {
      mw.startLastSaveBooleanFalseDialog();
    }
  }
 
  public void repeatParsing() {
    Thread parserThread = new Thread("Parser") {
      public void run() {
        mw.mainOutputAddLine("Repeating last save");
        settingsIO.BackupToLastSave();
        setOverwrite(true);
        startParsing();
      }
    };
    parserThread.start();
  }
 
  public void startParsing() {
   
    jsonParser parser = new jsonParser(mw, this);
    try {
      mw.mainOutputAddLine("Parsing images...");
      content = parser.parseContent();
    } catch (ParseException e) {
      mw.mainOutputAddLine("Parsing images failed!");
      e.printStackTrace();
    }
   
    if(content.peek()!= null) {
      sorter = new Sorter(mw, fileIO, parser, content);
 
      try {
        mw.mainOutputAddLine("Saving...");
        this.saveContent();
      } catch (IOException e) {
        mw.mainOutputAddLine("Saving failed!");
        e.printStackTrace();
      }
     
      settingsIO.writeSettings();
      mw.mainOutputAddLine("Finished");
    } else {
      mw.mainOutputAddLine("Everything is up to date!");
    }
    mw.setStartButtonActiv(true);
  }
 
  private void saveContent() throws IOException {
    //Saving images
    int savedCount = 0;
    int failCount = 0;
    settingsIO.setLastSaveBoolean(this.getCurrentUser(), false);
    mw.mainOutputAddLine("Saved " + 0 + " of " + sorter.getTotalImages() + " images");
    HashMap<String, LinkedList<JSONObject>> imagesMap = sorter.getImagesMap();
    Iterator<Entry<String, LinkedList<JSONObject>>> iterator = imagesMap.entrySet().iterator();
    while(iterator.hasNext() == true) {
      //Getting HashMap entry
      Entry<String, LinkedList<JSONObject>> entry = (Entry<String, LinkedList<JSONObject>>) iterator.next();
      String subreddit = (String) entry.getKey();
      LinkedList<JSONObject> list = (LinkedList<JSONObject>) entry.getValue();
      while(list.peekFirst() != null) {
        //Getting post
        JSONObject post = list.poll();
        JSONObject postData = (JSONObject) post.get("data");
        //Get name
        String name = ((String)postData.get("name"));
        //Get URL
        String url = ((String)postData.get("url"));
        //Get extension
        String ext = url.substring(url.lastIndexOf('.'), url.length());
        //Setting up paths
        String dirSubredditPath = currentUser + "/" + subreddit;
        String dirYearPath = dirSubredditPath +  "/" + year;
        String dirImagePath = null;
        if(ext.equals(".gif")) {
          dirImagePath = dirYearPath + "/" + "Gifs";
        } else {
          dirImagePath = dirYearPath + "/" + "Pics";
        }
        //Check for single file or album
        if(post.get("kind").equals("album") == false) {
          String imagePath = dirImagePath + "/" +url.substring(url.lastIndexOf('/')+1, url.length());
          System.out.println(imagePath);
          boolean saved = false;
          do{
            if(fileIO.saveImage(dirSubredditPath, dirYearPath, dirImagePath, imagePath, url) == true) {
              savedCount++;           
              mw.mainOutputReplaceLastLine("Saved " + savedCount + " of " + sorter.getTotalImages() + " images");
              saved = true;
            } else {
              failCount++;
              mw.mainOutputAddLine("Saving failed... " + failCount);
              saved = false;
            }
          } while(saved == false);
        } else {
          //Setting up paths
          String dirAlbumPath = dirYearPath + "/" + "Albums";
          dirImagePath = dirAlbumPath + "/" + postData.get("title");
         
          if(fileIO.checkFile(dirAlbumPath) == false) {
            fileIO.createDir(dirAlbumPath);
          }
         
          if(fileIO.checkFile(dirImagePath) == false) {
              fileIO.createDir(dirImagePath);
          } else {
            int i = 1;
            String newDirImagePath;
            do {
              newDirImagePath = dirImagePath + "(" + i + ")";
              i++;
            } while (fileIO.checkFile(newDirImagePath) == true);
            fileIO.createDir(newDirImagePath);
          }
         
          String urls = (String) postData.get("url");
          jsonParser parser = new jsonParser(mw, this);
          LinkedList<String> urlList = parser.jsonStringToLinkedList(urls);
          int imageCount = 0;
          Iterator URLIterator = urlList.iterator();
          while(URLIterator.hasNext() == true) {
            url = URLIterator.next().toString();
            String imagePath = dirImagePath + "/" + String.format("%02d", imageCount) + url.substring(url.lastIndexOf('.'));
            System.out.println(imagePath);
            boolean saved = false;
            do{
              if(fileIO.saveImage(dirSubredditPath, dirYearPath, dirImagePath, imagePath, url) == true) {
                saved = true;
              } else {
                failCount++;
                mw.mainOutputAddLine("Saving failed... " + failCount);
                saved = false;
              }
            } while(saved == false);
            imageCount++;
          }
          savedCount++;
          mw.mainOutputReplaceLastLine("Saved " + savedCount + " of " + sorter.getTotalImages() + " images");
        }
      }
    }
    if(savedCount == sorter.getTotalImages()) {
      this.setLastSaveBoolean(true);
      mw.mainOutputAddLine("Saving finished successfull");
    } else {
      this.setLastSaveBoolean(false);
      mw.mainOutputAddLine("Saving failed");
    }
  }
 
  public LinkedList<String> getUsers() {
    userList = settingsIO.getUsers();
    return userList;
  }
 
  public void addUser(String name, String url) {
    settingsIO.setUser(name, url);
  }
 
  public synchronized void removeUser(String name) {
    settingsIO.removeUser(name);
    settingsIO.removeLastSave(name);
    settingsIO.removeLastSaveBackup(name);
    settingsIO.removeLastSaveBoolean(name);
  }
 
  public synchronized void setCurrentUser(String name) {
    currentUser = name;
  }
 
  public String getCurrentUser() {
    return currentUser;
  }
 
  public String getCurrentUserURL() {
    return settingsIO.getUserString(currentUser);
  }
 
  public String getCurrentLastSave() {
    currentLastSave = settingsIO.getLastSave(currentUser);
    return currentLastSave;
  }
 
  public synchronized void setCurrentLastSave(String pLastSave) {
    currentLastSave = pLastSave;
    settingsIO.setLastSave(this.getCurrentUser(), pLastSave);
  }
 
  public synchronized void lastSaveToBackup() {
    settingsIO.lastSaveToBackup();
  }
 
  public Boolean getLastSaveBoolean() {
    lastSaveBoolean = settingsIO.getLastSaveBoolean(currentUser);
    return lastSaveBoolean;
  }
 
  public synchronized void setLastSaveBoolean(boolean value) {
    settingsIO.setLastSaveBoolean(this.getCurrentUser(), value);
    lastSaveBoolean = value;
  }
 
  public synchronized void setOverwrite(boolean value) {
    System.out.println("Set overwrite to " + value);
    overwrite = value;
    fileIO.setOverwrite(overwrite);
  }
 
  public boolean getOverwrite() {
    return overwrite;
  }
 
  public void writeSettings() {
    if(settingsIO.writeSettings() == true) {
      System.out.println("Saved settings");
    } else {
      System.out.println("Saving failed");
    }
  }
}
TOP

Related Classes of saveReddit.main.MainApplication

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.