Updating the VideoRipper class:

- ordered imports alphabetically;
- removed signature of the canRip method, which is already in AbstractRipper;
- split variable declaration in different lines;
- corrected javadocs alignment;
- replaced unnecessary use of StringBuilder.
- added a few empty lines to increase the readability of the code
This commit is contained in:
Gaboso 2018-06-02 20:49:35 -04:00
parent a02933f0d2
commit 7f4df8b567

View File

@ -1,5 +1,9 @@
package com.rarchives.ripme.ripper; package com.rarchives.ripme.ripper;
import com.rarchives.ripme.ui.RipStatusMessage;
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
import com.rarchives.ripme.utils.Utils;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
@ -7,29 +11,26 @@ import java.net.MalformedURLException;
import java.net.URL; import java.net.URL;
import java.util.Map; import java.util.Map;
import com.rarchives.ripme.ui.RipStatusMessage;
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
import com.rarchives.ripme.utils.Utils;
import com.sun.org.apache.xpath.internal.operations.Bool;
public abstract class VideoRipper extends AbstractRipper { public abstract class VideoRipper extends AbstractRipper {
private int bytesTotal = 1, private int bytesTotal = 1;
bytesCompleted = 1; private int bytesCompleted = 1;
protected VideoRipper(URL url) throws IOException { protected VideoRipper(URL url) throws IOException {
super(url); super(url);
} }
public abstract boolean canRip(URL url);
public abstract void rip() throws IOException; public abstract void rip() throws IOException;
public abstract String getHost(); public abstract String getHost();
public abstract String getGID(URL url) throws MalformedURLException; public abstract String getGID(URL url) throws MalformedURLException;
@Override @Override
public void setBytesTotal(int bytes) { public void setBytesTotal(int bytes) {
this.bytesTotal = bytes; this.bytesTotal = bytes;
} }
@Override @Override
public void setBytesCompleted(int bytes) { public void setBytesCompleted(int bytes) {
this.bytesCompleted = bytes; this.bytesCompleted = bytes;
@ -56,8 +57,7 @@ public abstract class VideoRipper extends AbstractRipper {
logger.error("Error while writing to " + urlFile, e); logger.error("Error while writing to " + urlFile, e);
return false; return false;
} }
} } else {
else {
if (isThisATest()) { if (isThisATest()) {
// Tests shouldn't download the whole video // Tests shouldn't download the whole video
// Just change this.url to the download URL so the test knows we found it. // Just change this.url to the download URL so the test knows we found it.
@ -71,52 +71,54 @@ public abstract class VideoRipper extends AbstractRipper {
} }
@Override @Override
public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String,String> cookies, Boolean getFileExtFromMIME) { public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String, String> cookies, Boolean getFileExtFromMIME) {
return addURLToDownload(url, saveAs); return addURLToDownload(url, saveAs);
} }
/** /**
* Creates & sets working directory based on URL. * Creates & sets working directory based on URL.
* @param url *
* Target 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();
if (!path.endsWith(File.separator)) { if (!path.endsWith(File.separator)) {
path += File.separator; path += File.separator;
} }
path += "videos" + File.separator; path += "videos" + File.separator;
this.workingDir = new File(path); workingDir = new File(path);
if (!this.workingDir.exists()) {
logger.info("[+] Creating directory: " + Utils.removeCWD(this.workingDir)); if (!workingDir.exists()) {
this.workingDir.mkdirs(); logger.info("[+] Creating directory: " + Utils.removeCWD(workingDir));
workingDir.mkdirs();
} }
logger.debug("Set working directory to: " + this.workingDir);
logger.debug("Set working directory to: " + workingDir);
} }
/** /**
* @return * @return Returns % of video done downloading.
* 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. * Runs if download successfully completed.
* @param url *
* Target URL * @param url Target URL
* @param saveAs * @param saveAs Path to file, including filename.
* 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) {
return; return;
} }
try { try {
String path = Utils.removeCWD(saveAs); String path = Utils.removeCWD(saveAs);
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, path); RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, path);
@ -127,62 +129,58 @@ 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. * Runs if the download errored somewhere.
* @param url *
* Target URL * @param url Target URL
* @param reason * @param reason Reason why the download failed.
* 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) {
return; return;
} }
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. * Runs if user tries to redownload an already existing File.
* @param url *
* Target URL * @param url Target URL
* @param file * @param file Existing 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) {
return; return;
} }
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_WARN, url + " already saved as " + file)); observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_WARN, url + " already saved as " + file));
checkIfComplete(); checkIfComplete();
} }
/** /**
* Gets the status and changes it to a human-readable form. * Gets the status and changes it to a human-readable form.
* @return *
* Status of current download. * @return Status of current download.
*/ */
@Override @Override
public String getStatusText() { public String getStatusText() {
StringBuilder sb = new StringBuilder(); return String.valueOf(getCompletionPercentage()) +
sb.append(getCompletionPercentage()) "% - " +
.append("% ") Utils.bytesToHumanReadable(bytesCompleted) +
.append(" - ") " / " +
.append(Utils.bytesToHumanReadable(bytesCompleted)) Utils.bytesToHumanReadable(bytesTotal);
.append(" / ")
.append(Utils.bytesToHumanReadable(bytesTotal));
return sb.toString();
} }
@Override
/** /**
* Sanitizes URL. * Sanitizes URL.
* Usually just returns itself. * Usually just returns itself.
*/ */
@Override
public URL sanitizeURL(URL url) throws MalformedURLException { public URL sanitizeURL(URL url) throws MalformedURLException {
return url; return url;
} }
@ -195,8 +193,10 @@ public abstract class VideoRipper extends AbstractRipper {
if (observer == null) { if (observer == null) {
return; return;
} }
if (bytesCompleted >= bytesTotal) { if (bytesCompleted >= bytesTotal) {
super.checkIfComplete(); super.checkIfComplete();
} }
} }
}
}