2014-04-20 07:41:11 +02:00
|
|
|
package com.rarchives.ripme.ripper;
|
|
|
|
|
|
|
|
import java.io.File;
|
2014-06-28 18:47:46 +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;
|
|
|
|
import java.util.Collections;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage;
|
|
|
|
import com.rarchives.ripme.ui.RipStatusMessage.STATUS;
|
|
|
|
import com.rarchives.ripme.utils.Utils;
|
|
|
|
|
2018-02-11 14:24:14 +01:00
|
|
|
// Should this file even exist? It does the same thing as abstractHTML ripper
|
2017-12-28 06:04:23 +01:00
|
|
|
|
|
|
|
/**'
|
|
|
|
* For ripping delicious albums off the interwebz.
|
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
public abstract class AlbumRipper extends AbstractRipper {
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
private Map<URL, File> itemsPending = Collections.synchronizedMap(new HashMap<URL, File>());
|
|
|
|
private Map<URL, File> itemsCompleted = Collections.synchronizedMap(new HashMap<URL, File>());
|
|
|
|
private Map<URL, String> itemsErrored = Collections.synchronizedMap(new HashMap<URL, String>());
|
2014-04-20 07:41:11 +02:00
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected AlbumRipper(URL url) throws IOException {
|
2014-04-20 07:41:11 +02:00
|
|
|
super(url);
|
|
|
|
}
|
|
|
|
|
|
|
|
public abstract boolean canRip(URL url);
|
|
|
|
public abstract URL sanitizeURL(URL url) throws MalformedURLException;
|
|
|
|
public abstract void rip() throws IOException;
|
|
|
|
public abstract String getHost();
|
|
|
|
public abstract String getGID(URL url) throws MalformedURLException;
|
|
|
|
|
2017-10-24 16:33:28 +02:00
|
|
|
protected boolean allowDuplicates() {
|
2014-05-01 05:13:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-11 09:23:43 +01:00
|
|
|
@Override
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Returns total amount of files attempted.
|
|
|
|
*/
|
2015-01-11 09:23:43 +01:00
|
|
|
public int getCount() {
|
2015-01-11 13:31:30 +01:00
|
|
|
return itemsCompleted.size() + itemsErrored.size();
|
2015-01-11 09:23:43 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
@Override
|
|
|
|
/**
|
|
|
|
* Queues multiple URLs of single images to download from a single Album URL
|
|
|
|
*/
|
2018-05-05 11:36:00 +02:00
|
|
|
public boolean addURLToDownload(URL url, File saveAs, String referrer, Map<String,String> cookies, Boolean getFileExtFromMIME) {
|
2015-02-06 08:58:17 +01:00
|
|
|
// Only download one file if this is a test.
|
|
|
|
if (super.isThisATest() &&
|
|
|
|
(itemsPending.size() > 0 || itemsCompleted.size() > 0 || itemsErrored.size() > 0)) {
|
|
|
|
stop();
|
|
|
|
return false;
|
|
|
|
}
|
2014-05-01 05:13:44 +02:00
|
|
|
if (!allowDuplicates()
|
|
|
|
&& ( itemsPending.containsKey(url)
|
|
|
|
|| itemsCompleted.containsKey(url)
|
|
|
|
|| itemsErrored.containsKey(url) )) {
|
2014-04-20 07:41:11 +02:00
|
|
|
// Item is already downloaded/downloading, skip it.
|
|
|
|
logger.info("[!] Skipping " + url + " -- already attempted: " + Utils.removeCWD(saveAs));
|
2014-10-15 14:02:36 +02:00
|
|
|
return false;
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
2014-06-28 18:47:46 +02:00
|
|
|
if (Utils.getConfigBoolean("urls_only.save", false)) {
|
|
|
|
// Output URL to file
|
|
|
|
String urlFile = this.workingDir + File.separator + "urls.txt";
|
|
|
|
try {
|
|
|
|
FileWriter fw = new FileWriter(urlFile, true);
|
|
|
|
fw.write(url.toExternalForm());
|
|
|
|
fw.write("\n");
|
|
|
|
fw.close();
|
|
|
|
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, urlFile);
|
|
|
|
itemsCompleted.put(url, new File(urlFile));
|
|
|
|
observer.update(this, msg);
|
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("Error while writing to " + urlFile, e);
|
|
|
|
}
|
2014-04-23 05:48:41 +02:00
|
|
|
}
|
2014-06-28 18:47:46 +02:00
|
|
|
else {
|
|
|
|
itemsPending.put(url, saveAs);
|
2018-05-05 11:36:00 +02:00
|
|
|
DownloadFileThread dft = new DownloadFileThread(url, saveAs, this, getFileExtFromMIME);
|
2014-06-28 18:47:46 +02:00
|
|
|
if (referrer != null) {
|
|
|
|
dft.setReferrer(referrer);
|
|
|
|
}
|
|
|
|
if (cookies != null) {
|
|
|
|
dft.setCookies(cookies);
|
|
|
|
}
|
|
|
|
threadPool.addThread(dft);
|
2014-04-23 05:48:41 +02:00
|
|
|
}
|
2014-10-15 14:02:36 +02:00
|
|
|
return true;
|
2014-04-23 05:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-10-15 14:02:36 +02:00
|
|
|
public boolean addURLToDownload(URL url, File saveAs) {
|
2018-05-05 11:36:00 +02:00
|
|
|
return addURLToDownload(url, saveAs, null, null, false);
|
2014-04-23 05:48:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Queues image to be downloaded and saved.
|
|
|
|
* Uses filename from URL to decide filename.
|
|
|
|
* @param url
|
|
|
|
* URL to download
|
2017-06-19 19:32:57 +02:00
|
|
|
* @return
|
2014-10-15 14:02:36 +02:00
|
|
|
* True on success
|
2014-04-23 05:48:41 +02:00
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
protected boolean addURLToDownload(URL url) {
|
2014-04-23 05:48:41 +02:00
|
|
|
// Use empty prefix and empty subdirectory
|
2014-10-15 14:02:36 +02:00
|
|
|
return addURLToDownload(url, "", "");
|
2014-04-20 07:41:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Cleans up & tells user about successful download
|
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
public void downloadCompleted(URL url, File saveAs) {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
String path = Utils.removeCWD(saveAs);
|
|
|
|
RipStatusMessage msg = new RipStatusMessage(STATUS.DOWNLOAD_COMPLETE, path);
|
|
|
|
itemsPending.remove(url);
|
|
|
|
itemsCompleted.put(url, saveAs);
|
|
|
|
observer.update(this, msg);
|
|
|
|
|
|
|
|
checkIfComplete();
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("Exception while updating observer: ", e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Cleans up & tells user about failed download.
|
|
|
|
*/
|
2014-04-20 07:41:11 +02:00
|
|
|
public void downloadErrored(URL url, String reason) {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
itemsPending.remove(url);
|
|
|
|
itemsErrored.put(url, reason);
|
|
|
|
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_ERRORED, url + " : " + reason));
|
|
|
|
|
|
|
|
checkIfComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Tells user that a single file in the album they wish to download has
|
|
|
|
* already been downloaded in the past.
|
|
|
|
*/
|
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;
|
|
|
|
}
|
2015-01-11 14:11:10 +01:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
itemsPending.remove(url);
|
2015-01-11 14:11:10 +01:00
|
|
|
itemsCompleted.put(url, file);
|
|
|
|
observer.update(this, new RipStatusMessage(STATUS.DOWNLOAD_WARN, url + " already saved as " + file.getAbsolutePath()));
|
2017-06-19 19:32:57 +02:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
checkIfComplete();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notifies observers and updates state if all files have been ripped.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
protected void checkIfComplete() {
|
|
|
|
if (observer == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (itemsPending.isEmpty()) {
|
|
|
|
super.checkIfComplete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets directory to save all ripped files to.
|
|
|
|
* @param url
|
2014-07-25 09:48:52 +02:00
|
|
|
* URL to define how the working directory should be saved.
|
2017-06-19 19:32:57 +02:00
|
|
|
* @throws
|
|
|
|
* IOException
|
2014-04-20 07:41:11 +02:00
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public void setWorkingDir(URL url) throws IOException {
|
|
|
|
String path = Utils.getWorkingDirectory().getCanonicalPath();
|
|
|
|
if (!path.endsWith(File.separator)) {
|
|
|
|
path += File.separator;
|
|
|
|
}
|
2014-07-25 09:48:52 +02:00
|
|
|
String title;
|
|
|
|
if (Utils.getConfigBoolean("album_titles.save", true)) {
|
|
|
|
title = getAlbumTitle(this.url);
|
|
|
|
} else {
|
|
|
|
title = super.getAlbumTitle(this.url);
|
|
|
|
}
|
2015-02-10 08:29:29 +01:00
|
|
|
logger.debug("Using album title '" + title + "'");
|
2018-01-20 15:13:46 +01:00
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
title = Utils.filesystemSafe(title);
|
2018-01-20 15:13:46 +01:00
|
|
|
path += title;
|
|
|
|
path = Utils.getOriginalDirectory(path) + File.separator; // check for case sensitive (unix only)
|
|
|
|
|
2014-04-20 07:41:11 +02:00
|
|
|
this.workingDir = new File(path);
|
|
|
|
if (!this.workingDir.exists()) {
|
|
|
|
logger.info("[+] Creating directory: " + Utils.removeCWD(this.workingDir));
|
|
|
|
this.workingDir.mkdirs();
|
|
|
|
}
|
|
|
|
logger.debug("Set working directory to: " + this.workingDir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
* Integer between 0 and 100 defining the progress of the album rip.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public int getCompletionPercentage() {
|
|
|
|
double total = itemsPending.size() + itemsErrored.size() + itemsCompleted.size();
|
|
|
|
return (int) (100 * ( (total - itemsPending.size()) / total));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return
|
|
|
|
* Human-readable information on the status of the current rip.
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public String getStatusText() {
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
sb.append(getCompletionPercentage())
|
|
|
|
.append("% ")
|
|
|
|
.append("- Pending: " ).append(itemsPending.size())
|
|
|
|
.append(", Completed: ").append(itemsCompleted.size())
|
|
|
|
.append(", Errored: " ).append(itemsErrored.size());
|
|
|
|
return sb.toString();
|
|
|
|
}
|
|
|
|
}
|