Merge pull request #365 from kevin51jiang/MoreDocumentation

3 Metric Tons of Javadoc documentation for core Ripper stuff.
This commit is contained in:
cyian-1756 2017-12-29 04:21:26 -05:00 committed by GitHub
commit 97975a6257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 223 additions and 16 deletions

View File

@ -63,13 +63,21 @@ public class App {
SwingUtilities.invokeLater(mw); SwingUtilities.invokeLater(mw);
} }
} }
/**
* Creates an abstract ripper and instructs it to rip.
* @param url URL to be ripped
* @throws Exception
*/
private static void rip(URL url) throws Exception { private static void rip(URL url) throws Exception {
AbstractRipper ripper = AbstractRipper.getRipper(url); AbstractRipper ripper = AbstractRipper.getRipper(url);
ripper.setup(); ripper.setup();
ripper.rip(); ripper.rip();
} }
/**
* For dealing with command-line arguments.
* @param args Array of Command-line arguments
*/
private static void handleArguments(String[] args) { private static void handleArguments(String[] args) {
CommandLine cl = getArgs(args); CommandLine cl = getArgs(args);
if (cl.hasOption('h')) { if (cl.hasOption('h')) {
@ -109,7 +117,7 @@ public class App {
} }
if (cl.hasOption('R')) { if (cl.hasOption('R')) {
loadHistory(); loadHistory();
if (HISTORY.toList().size() == 0) { if (HISTORY.toList().isEmpty()) {
logger.error("There are no history entries to re-rip. Rip some albums first"); logger.error("There are no history entries to re-rip. Rip some albums first");
System.exit(-1); System.exit(-1);
} }
@ -173,14 +181,18 @@ public class App {
} }
} }
// this function will attempt to rip the provided url /**
* Attempt to rip targetURL.
* @param targetURL URL to rip
* @param saveConfig Whether or not you want to save the config (?)
*/
private static void ripURL(String targetURL, boolean saveConfig) { private static void ripURL(String targetURL, boolean saveConfig) {
try { try {
URL url = new URL(targetURL); URL url = new URL(targetURL);
rip(url); rip(url);
List<String> history = Utils.getConfigList("download.history"); List<String> history = Utils.getConfigList("download.history");
if (!history.contains(url.toExternalForm())) { if (!history.contains(url.toExternalForm())) {//if you haven't already downloaded the file before
history.add(url.toExternalForm()); history.add(url.toExternalForm());//add it to history so you won't have to redownload
Utils.setConfigList("download.history", Arrays.asList(history.toArray())); Utils.setConfigList("download.history", Arrays.asList(history.toArray()));
if (saveConfig) { if (saveConfig) {
Utils.saveConfig(); Utils.saveConfig();
@ -195,6 +207,10 @@ public class App {
} }
} }
/**
* Creates an Options object, returns it.
* @return Returns all acceptable command-line options.
*/
private static Options getOptions() { private static Options getOptions() {
Options opts = new Options(); Options opts = new Options();
opts.addOption("h", "help", false, "Print the help"); opts.addOption("h", "help", false, "Print the help");
@ -213,6 +229,11 @@ public class App {
return opts; return opts;
} }
/**
* Tries to parse commandline arguments.
* @param args Array of commandline arguments.
* @return CommandLine object containing arguments.
*/
private static CommandLine getArgs(String[] args) { private static CommandLine getArgs(String[] args) {
BasicParser parser = new BasicParser(); BasicParser parser = new BasicParser();
try { try {
@ -224,6 +245,9 @@ public class App {
} }
} }
/**
* Loads history from history file into memory.
*/
private static void loadHistory() { private static void loadHistory() {
File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json"); File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json");
HISTORY.clear(); HISTORY.clear();

View File

@ -140,6 +140,14 @@ public abstract class AbstractHTMLRipper extends AlbumRipper {
} }
waitForThreads(); waitForThreads();
} }
/**
* Gets the file name from the URL
* @param url
* URL that you want to get the filename from
* @return
* Filename of the URL
*/
private String fileNameFromURL(URL url) { private String fileNameFromURL(URL url) {
String saveAs = url.toExternalForm(); String saveAs = url.toExternalForm();
if (saveAs.substring(saveAs.length() - 1) == "/") { saveAs = saveAs.substring(0,saveAs.length() - 1) ;} if (saveAs.substring(saveAs.length() - 1) == "/") { saveAs = saveAs.substring(0,saveAs.length() - 1) ;}
@ -150,6 +158,20 @@ public abstract class AbstractHTMLRipper extends AlbumRipper {
if (saveAs.indexOf(':') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf(':')); } if (saveAs.indexOf(':') >= 0) { saveAs = saveAs.substring(0, saveAs.indexOf(':')); }
return saveAs; return saveAs;
} }
/**
*
* @param url
* Target URL
* @param subdirectory
* Path to subdirectory where you want to save it
* @param text
* Text you want to save
* @param index
* Index in something like an album
* @return
* True if ripped successfully
* False if failed
*/
public boolean saveText(URL url, String subdirectory, String text, int index) { public boolean saveText(URL url, String subdirectory, String text, int index) {
String saveAs = fileNameFromURL(url); String saveAs = fileNameFromURL(url);
return saveText(url,subdirectory,text,index,saveAs); return saveText(url,subdirectory,text,index,saveAs);
@ -189,6 +211,14 @@ public abstract class AbstractHTMLRipper extends AlbumRipper {
} }
return true; return true;
} }
/**
* Gets prefix based on where in the index it is
* @param index
* The index in question
* @return
* Returns prefix for a file. (?)
*/
protected String getPrefix(int index) { protected String getPrefix(int index) {
String prefix = ""; String prefix = "";
if (keepSortOrder() && Utils.getConfigBoolean("download.save_order", true)) { if (keepSortOrder() && Utils.getConfigBoolean("download.save_order", true)) {

View File

@ -20,6 +20,7 @@ public abstract class AbstractJSONRipper extends AlbumRipper {
} }
protected abstract String getDomain(); protected abstract String getDomain();
@Override
public abstract String getHost(); public abstract String getHost();
protected abstract JSONObject getFirstPage() throws IOException; protected abstract JSONObject getFirstPage() throws IOException;

View File

@ -85,6 +85,13 @@ public abstract class AbstractRipper
} }
} }
/**
* Checks to see if Ripme has already downloaded a URL
* @param url URL to check if downloaded
* @return
* Returns true if previously downloaded.
* Returns false if not yet downloaded.
*/
private boolean hasDownloadedURL(String url) { private boolean hasDownloadedURL(String url) {
File file = new File(URLHistoryFile); File file = new File(URLHistoryFile);
try { try {
@ -118,6 +125,15 @@ public abstract class AbstractRipper
this.url = sanitizeURL(url); this.url = sanitizeURL(url);
} }
/**
* Sets ripper's:
* Working directory
* Logger (for debugging)
* FileAppender
* Threadpool
* @throws IOException
* Always be prepared.
*/
public void setup() throws IOException { public void setup() throws IOException {
setWorkingDir(this.url); setWorkingDir(this.url);
Logger rootLogger = Logger.getRootLogger(); Logger rootLogger = Logger.getRootLogger();
@ -155,9 +171,27 @@ public abstract class AbstractRipper
* @param cookies * @param cookies
* The cookies to send to the server while downloading this file. * The cookies to send to the server while downloading this file.
* @return * @return
* True if downloaded successfully
* False if failed to download
*/ */
protected abstract boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String, String> cookies); protected abstract boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String, String> cookies);
/**
* Queues image to be downloaded and saved.
* @param url
* URL of the file
* @param prefix
* Prefix for the downloaded file
* @param subdirectory
* Path to get to desired directory from working directory
* @param referrer
* The HTTP referrer to use while downloading this file.
* @param cookies
* The cookies to send to the server while downloading this file.
* @return
* True if downloaded successfully
* False if failed to download
*/
protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map<String, String> cookies) { protected boolean addURLToDownload(URL url, String prefix, String subdirectory, String referrer, Map<String, String> cookies) {
if (Utils.getConfigBoolean("remember.url_history", true) && !isThisATest()) { if (Utils.getConfigBoolean("remember.url_history", true) && !isThisATest()) {
if (hasDownloadedURL(url.toExternalForm())) { if (hasDownloadedURL(url.toExternalForm())) {
@ -322,6 +356,11 @@ public abstract class AbstractRipper
} }
} }
/**
* Gets URL
* @return
* Returns URL that wants to be downloaded.
*/
public URL getURL() { public URL getURL() {
return url; return url;
} }
@ -335,8 +374,20 @@ public abstract class AbstractRipper
return workingDir; return workingDir;
} }
@Override
public abstract void setWorkingDir(URL url) throws IOException; public abstract void setWorkingDir(URL url) throws IOException;
/**
*
* @param url
* The URL you want to get the title of.
* @return
* host_URLid
* e.g. (for a reddit post)
* reddit_post_7mg2ur
* @throws MalformedURLException
* If any of those damned URLs gets malformed.
*/
public String getAlbumTitle(URL url) throws MalformedURLException { public String getAlbumTitle(URL url) throws MalformedURLException {
return getHost() + "_" + getGID(url); return getHost() + "_" + getGID(url);
} }
@ -401,8 +452,16 @@ public abstract class AbstractRipper
observer.update(this, new RipStatusMessage(status, message)); observer.update(this, new RipStatusMessage(status, message));
} }
/**
* Get the completion percentage.
* @return
* Percentage complete
*/
public abstract int getCompletionPercentage(); public abstract int getCompletionPercentage();
/**
* @return
* Text for status
*/
public abstract String getStatusText(); public abstract String getStatusText();
/** /**
@ -423,7 +482,9 @@ public abstract class AbstractRipper
cleanup(); cleanup();
} }
} }
/**
* Tries to delete any empty directories
*/
private void cleanup() { private void cleanup() {
if (this.workingDir.list().length == 0) { if (this.workingDir.list().length == 0) {
// No files, delete the dir // No files, delete the dir
@ -435,6 +496,14 @@ public abstract class AbstractRipper
} }
} }
/**
* Pauses thread for a set amount of time.
* @param milliseconds
* Amount of time (in milliseconds) that the thread gets paused for
* @return
* True if paused successfully
* False if failed to pause/got interrupted.
*/
protected boolean sleep(int milliseconds) { protected boolean sleep(int milliseconds) {
try { try {
logger.debug("Sleeping " + milliseconds + "ms"); logger.debug("Sleeping " + milliseconds + "ms");

View File

@ -13,6 +13,10 @@ import com.rarchives.ripme.ui.RipStatusMessage;
import com.rarchives.ripme.ui.RipStatusMessage.STATUS; import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
import com.rarchives.ripme.utils.Utils; import com.rarchives.ripme.utils.Utils;
/**'
* For ripping delicious albums off the interwebz.
*/
public abstract class AlbumRipper extends AbstractRipper { public abstract class AlbumRipper extends AbstractRipper {
private Map<URL, File> itemsPending = Collections.synchronizedMap(new HashMap<URL, File>()); private Map<URL, File> itemsPending = Collections.synchronizedMap(new HashMap<URL, File>());
@ -34,10 +38,17 @@ public abstract class AlbumRipper extends AbstractRipper {
} }
@Override @Override
/**
* Returns total amount of files attempted.
*/
public int getCount() { public int getCount() {
return itemsCompleted.size() + itemsErrored.size(); return itemsCompleted.size() + itemsErrored.size();
} }
@Override
/**
* Queues multiple URLs of single images to download from a single Album URL
*/
public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String,String> cookies) { public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String,String> cookies) {
// Only download one file if this is a test. // Only download one file if this is a test.
if (super.isThisATest() && if (super.isThisATest() &&
@ -101,6 +112,9 @@ public abstract class AlbumRipper extends AbstractRipper {
} }
@Override @Override
/**
* Cleans up & tells user about successful download
*/
public void downloadCompleted(URL url, File saveAs) { public void downloadCompleted(URL url, File saveAs) {
if (observer == null) { if (observer == null) {
return; return;
@ -119,6 +133,9 @@ public abstract class AlbumRipper extends AbstractRipper {
} }
@Override @Override
/**
* Cleans up & tells user about failed download.
*/
public void downloadErrored(URL url, String reason) { public void downloadErrored(URL url, String reason) {
if (observer == null) { if (observer == null) {
return; return;
@ -131,6 +148,10 @@ public abstract class AlbumRipper extends AbstractRipper {
} }
@Override @Override
/**
* Tells user that a single file in the album they wish to download has
* already been downloaded in the past.
*/
public void downloadExists(URL url, File file) { public void downloadExists(URL url, File file) {
if (observer == null) { if (observer == null) {
return; return;

View File

@ -24,16 +24,27 @@ public class DownloadThreadPool {
initialize(threadPoolName); initialize(threadPoolName);
} }
/**
* Initializes the threadpool.
* @param threadPoolName Name of the threadpool.
*/
private void initialize(String threadPoolName) { private void initialize(String threadPoolName) {
int threads = Utils.getConfigInteger("threads.size", 10); int threads = Utils.getConfigInteger("threads.size", 10);
logger.debug("Initializing " + threadPoolName + " thread pool with " + threads + " threads"); logger.debug("Initializing " + threadPoolName + " thread pool with " + threads + " threads");
threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads); threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(threads);
} }
/**
* For adding threads to execution pool.
* @param t
* Thread to be added.
*/
public void addThread(Thread t) { public void addThread(Thread t) {
threadPool.execute(t); threadPool.execute(t);
} }
/**
* Tries to shutdown threadpool.
*/
public void waitForThreads() { public void waitForThreads() {
threadPool.shutdown(); threadPool.shutdown();
try { try {

View File

@ -136,6 +136,12 @@ class DownloadVideoThread extends Thread {
logger.info("[+] Saved " + url + " as " + this.prettySaveAs); logger.info("[+] Saved " + url + " as " + this.prettySaveAs);
} }
/**
* @param url
* Target URL
* @return
* Returns connection length
*/
private int getTotalBytes(URL url) throws IOException { private int getTotalBytes(URL url) throws IOException {
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("HEAD"); conn.setRequestMethod("HEAD");

View File

@ -7,6 +7,8 @@ import java.net.URL;
/** /**
* I have no idea why I made this interface. Everything is captured within the AbstractRipper. * I have no idea why I made this interface. Everything is captured within the AbstractRipper.
* Oh well, here's to encapsulation and abstraction! (raises glass) * Oh well, here's to encapsulation and abstraction! (raises glass)
*
* (cheers!)
*/ */
interface RipperInterface { interface RipperInterface {
void rip() throws IOException; void rip() throws IOException;

View File

@ -74,6 +74,12 @@ public abstract class VideoRipper extends AbstractRipper {
return addURLToDownload(url, saveAs); return addURLToDownload(url, saveAs);
} }
/**
* Creates & sets working directory based on URL.
* @param url
* Target URL
*/
@Override @Override
public void setWorkingDir(URL url) throws IOException { public void setWorkingDir(URL url) throws IOException {
String path = Utils.getWorkingDirectory().getCanonicalPath(); String path = Utils.getWorkingDirectory().getCanonicalPath();
@ -89,11 +95,22 @@ public abstract class VideoRipper extends AbstractRipper {
logger.debug("Set working directory to: " + this.workingDir); logger.debug("Set working directory to: " + this.workingDir);
} }
/**
* @return
* Returns % of video done downloading.
*/
@Override @Override
public int getCompletionPercentage() { public int getCompletionPercentage() {
return (int) (100 * (bytesCompleted / (float) bytesTotal)); return (int) (100 * (bytesCompleted / (float) bytesTotal));
} }
/**
* Runs if download successfully completed.
* @param url
* Target URL
* @param saveAs
* Path to file, including filename.
*/
@Override @Override
public void downloadCompleted(URL url, File saveAs) { public void downloadCompleted(URL url, File saveAs) {
if (observer == null) { if (observer == null) {
@ -109,6 +126,14 @@ public abstract class VideoRipper extends AbstractRipper {
logger.error("Exception while updating observer: ", e); logger.error("Exception while updating observer: ", e);
} }
} }
/**
* Runs if the download errored somewhere.
* @param url
* Target URL
* @param reason
* Reason why the download failed.
*/
@Override @Override
public void downloadErrored(URL url, String reason) { public void downloadErrored(URL url, String reason) {
if (observer == null) { if (observer == null) {
@ -117,6 +142,15 @@ public abstract class VideoRipper extends AbstractRipper {
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_ERRORED, url + " : " + reason)); observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_ERRORED, url + " : " + reason));
checkIfComplete(); checkIfComplete();
} }
/**
* Runs if user tries to redownload an already existing File.
* @param url
* Target URL
* @param file
* Existing file
*/
@Override @Override
public void downloadExists(URL url, File file) { public void downloadExists(URL url, File file) {
if (observer == null) { if (observer == null) {
@ -126,6 +160,11 @@ public abstract class VideoRipper extends AbstractRipper {
checkIfComplete(); checkIfComplete();
} }
/**
* Gets the status and changes it to a human-readable form.
* @return
* Status of current download.
*/
@Override @Override
public String getStatusText() { public String getStatusText() {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
@ -139,6 +178,10 @@ public abstract class VideoRipper extends AbstractRipper {
} }
@Override @Override
/**
* Sanitizes URL.
* Usually just returns itself.
*/
public URL sanitizeURL(URL url) throws MalformedURLException { public URL sanitizeURL(URL url) throws MalformedURLException {
return url; return url;
} }