2014-04-20 07:41:11 +02:00
|
|
|
package com.rarchives.ripme.ripper;
|
|
|
|
|
2018-06-03 02:49:35 +02:00
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage;
|
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
import java.io.File;
|
2014-06-28 18:49:11 +02:00
|
|
|
import java.io.FileWriter;
|
2014-04-20 07:41:11 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
2014-04-23 05:48:41 +02:00
|
|
|
import java.util.Map;
|
2014-04-20 07:41:11 +02:00
|
|
|
|
|
|
|
|
|
|
|
public abstract class VideoRipper extends AbstractRipper {
|
|
|
|
|
2018-06-03 02:49:35 +02:00
|
|
|
private int bytesTotal = 1;
|
|
|
|
private int bytesCompleted = 1;
|
2014-04-20 07:41:11 +02:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected VideoRipper(URL url) throws IOException {
|
2014-04-20 07:41:11 +02:00
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract void rip() throws IOException;
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
public abstract String getHost();
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
public abstract String getGID(URL url) throws MalformedURLException;
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setBytesTotal(int bytes) {
|
|
|
|
this.bytesTotal = bytes;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public void setBytesCompleted(int bytes) {
|
|
|
|
this.bytesCompleted = bytes;
|
|
|
|
}
|
|
|
|
|
2014-04-20 09:12:48 +02:00
|
|
|
@Override
|
|
|
|
public String getAlbumTitle(URL url) {
|
|
|
|
return "videos";
|
|
|
|
}
|
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
2014-10-21 10:10:09 +02:00
|
|
|
public boolean addURLToDownload(URL url, File saveAs) {
|
2014-06-28 18:49:11 +02:00
|
|
|
if (Utils.getConfigBoolean("urls_only.save", false)) {
|
|
|
|
// Output URL to file
|
|
|
|
String urlFile = this.workingDir + File.separator + "urls.txt";
|
2018-05-30 00:17:00 +02:00
|
|
|
|
|
|
|
try (FileWriter fw = new FileWriter(urlFile, true)) {
|
2014-06-28 18:49:11 +02:00
|
|
|
fw.write(url.toExternalForm());
|
|
|
|
fw.write("\n");
|
2018-05-30 00:17:00 +02:00
|
|
|
|
2014-06-28 18:49:11 +02:00
|
|
|
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
|
|
|
|
observer.update(this, msg);
|
|
|
|
} catch (IOException e) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.error("Error while writing to " + urlFile, e);
|
2014-10-21 10:10:09 +02:00
|
|
|
return false;
|
2014-06-28 18:49:11 +02:00
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
} else {
|
2015-02-06 08:58:17 +01:00
|
|
|
if (isThisATest()) {
|
2015-02-10 08:29:29 +01:00
|
|
|
// Tests shouldn't download the whole video
|
|
|
|
// Just change this.url to the download URL so the test knows we found it.
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.debug("Test rip, found URL: " + url);
|
2015-02-06 12:01:02 +01:00
|
|
|
this.url = url;
|
2015-02-06 08:58:17 +01:00
|
|
|
return true;
|
|
|
|
}
|
2014-06-28 18:49:11 +02:00
|
|
|
threadPool.addThread(new DownloadVideoThread(url, saveAs, this));
|
|
|
|
}
|
2014-10-21 10:10:09 +02:00
|
|
|
return true;
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
|
|
|
|
2014-04-23 05:48:41 +02:00
|
|
|
@Override
|
2018-06-03 02:49:35 +02:00
|
|
|
public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String, String> cookies, Boolean getFileExtFromMIME) {
|
2014-10-21 10:10:09 +02:00
|
|
|
return addURLToDownload(url, saveAs);
|
2014-04-23 05:48:41 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Creates & sets working directory based on URL.
|
2018-06-03 02:49:35 +02:00
|
|
|
*
|
|
|
|
* @param url Target URL
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public void setWorkingDir(URL url) throws IOException {
|
|
|
|
String path = Utils.getWorkingDirectory().getCanonicalPath();
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
if (!path.endsWith(File.separator)) {
|
|
|
|
path += File.separator;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
path += "videos" + File.separator;
|
2018-06-03 02:49:35 +02:00
|
|
|
workingDir = new File(path);
|
|
|
|
|
|
|
|
if (!workingDir.exists()) {
|
2018-06-05 04:36:14 +02:00
|
|
|
LOGGER.info("[+] Creating directory: " + Utils.removeCWD(workingDir));
|
2018-06-03 02:49:35 +02:00
|
|
|
workingDir.mkdirs();
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2018-06-05 04:36:14 +02:00
|
|
|
LOGGER.debug("Set working directory to: " + workingDir);
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
2018-06-03 02:49:35 +02:00
|
|
|
* @return Returns % of video done downloading.
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public int getCompletionPercentage() {
|
|
|
|
return (int) (100 * (bytesCompleted / (float) bytesTotal));
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Runs if download successfully completed.
|
2018-06-03 02:49:35 +02:00
|
|
|
*
|
|
|
|
* @param url Target URL
|
|
|
|
* @param saveAs Path to file, including filename.
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public void downloadCompleted(URL url, File saveAs) {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
try {
|
|
|
|
String path = Utils.removeCWD(saveAs);
|
|
|
|
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, path);
|
|
|
|
observer.update(this, msg);
|
|
|
|
|
|
|
|
checkIfComplete();
|
|
|
|
} catch (Exception e) {
|
2018-06-03 03:14:41 +02:00
|
|
|
LOGGER.error("Exception while updating observer: ", e);
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Runs if the download errored somewhere.
|
2018-06-03 02:49:35 +02:00
|
|
|
*
|
|
|
|
* @param url Target URL
|
|
|
|
* @param reason Reason why the download failed.
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public void downloadErrored(URL url, String reason) {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_ERRORED, url + " : " + reason));
|
|
|
|
checkIfComplete();
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Runs if user tries to redownload an already existing File.
|
2018-06-03 02:49:35 +02:00
|
|
|
*
|
|
|
|
* @param url Target URL
|
|
|
|
* @param file Existing file
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
2015-01-11 14:11:10 +01:00
|
|
|
public void downloadExists(URL url, File file) {
|
2014-04-20 07:41:11 +02:00
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2015-01-11 14:11:10 +01:00
|
|
|
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_WARN, url + " already saved as " + file));
|
2014-04-20 07:41:11 +02:00
|
|
|
checkIfComplete();
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Gets the status and changes it to a human-readable form.
|
2018-06-03 02:49:35 +02:00
|
|
|
*
|
|
|
|
* @return Status of current download.
|
2017-12-28 06:04:23 +01:00
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
@Override
|
|
|
|
public String getStatusText() {
|
2018-06-03 02:49:35 +02:00
|
|
|
return String.valueOf(getCompletionPercentage()) +
|
|
|
|
"% - " +
|
|
|
|
Utils.bytesToHumanReadable(bytesCompleted) +
|
|
|
|
" / " +
|
|
|
|
Utils.bytesToHumanReadable(bytesTotal);
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Sanitizes URL.
|
|
|
|
* Usually just returns itself.
|
|
|
|
*/
|
2018-06-03 02:49:35 +02:00
|
|
|
@Override
|
2014-04-20 07:41:11 +02:00
|
|
|
public URL sanitizeURL(URL url) throws MalformedURLException {
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies observers and updates state if all files have been ripped.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void checkIfComplete() {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
if (bytesCompleted >= bytesTotal) {
|
|
|
|
super.checkIfComplete();
|
|
|
|
}
|
|
|
|
}
|
2018-06-03 02:49:35 +02:00
|
|
|
|
|
|
|
}
|