2014-02-25 10:28:22 +01:00
|
|
|
package com.rarchives.ripme;
|
|
|
|
|
2018-03-30 11:58:46 +02:00
|
|
|
import java.awt.*;
|
2015-10-18 01:55:09 +02:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.IOException;
|
2016-08-18 02:10:07 +02:00
|
|
|
import java.io.BufferedReader;
|
|
|
|
import java.io.FileReader;
|
|
|
|
import java.io.FileNotFoundException;
|
|
|
|
|
2014-02-27 10:28:23 +01:00
|
|
|
import java.net.MalformedURLException;
|
2014-02-25 10:28:22 +01:00
|
|
|
import java.net.URL;
|
2014-04-23 06:16:23 +02:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
2014-02-25 10:28:22 +01:00
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
import javax.swing.SwingUtilities;
|
|
|
|
|
2014-02-27 10:28:23 +01:00
|
|
|
import org.apache.commons.cli.BasicParser;
|
|
|
|
import org.apache.commons.cli.CommandLine;
|
|
|
|
import org.apache.commons.cli.HelpFormatter;
|
|
|
|
import org.apache.commons.cli.Options;
|
|
|
|
import org.apache.commons.cli.ParseException;
|
2018-03-30 11:58:46 +02:00
|
|
|
import org.apache.commons.lang.SystemUtils;
|
2014-02-26 08:44:22 +01:00
|
|
|
import org.apache.log4j.Logger;
|
|
|
|
|
2014-02-27 10:28:23 +01:00
|
|
|
import com.rarchives.ripme.ripper.AbstractRipper;
|
2015-10-18 01:55:09 +02:00
|
|
|
import com.rarchives.ripme.ui.History;
|
|
|
|
import com.rarchives.ripme.ui.HistoryEntry;
|
2015-12-20 20:58:37 +01:00
|
|
|
import com.rarchives.ripme.ui.MainWindow;
|
2014-04-05 11:11:04 +02:00
|
|
|
import com.rarchives.ripme.ui.UpdateUtils;
|
2015-10-18 01:55:09 +02:00
|
|
|
import com.rarchives.ripme.utils.RipUtils;
|
2015-12-20 20:58:37 +01:00
|
|
|
import com.rarchives.ripme.utils.Utils;
|
2014-02-25 10:28:22 +01:00
|
|
|
|
|
|
|
/**
|
2015-12-20 20:58:37 +01:00
|
|
|
* Entry point to application.
|
|
|
|
* Decides to display UI or to run silently via command-line.
|
2014-02-25 10:28:22 +01:00
|
|
|
*/
|
|
|
|
public class App {
|
2014-02-27 10:28:23 +01:00
|
|
|
|
2018-03-30 11:58:46 +02:00
|
|
|
public static final Logger logger = Logger.getLogger(App.class);
|
2015-10-18 01:55:09 +02:00
|
|
|
private static final History HISTORY = new History();
|
2014-02-27 10:28:23 +01:00
|
|
|
|
|
|
|
public static void main(String[] args) throws MalformedURLException {
|
2017-10-12 10:12:10 +02:00
|
|
|
CommandLine cl = getArgs(args);
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2017-10-12 10:12:10 +02:00
|
|
|
if (args.length > 0 && cl.hasOption('v')){
|
2018-03-30 11:58:46 +02:00
|
|
|
logger.info(UpdateUtils.getThisJarVersion());
|
2017-10-12 10:12:10 +02:00
|
|
|
System.exit(0);
|
|
|
|
}
|
|
|
|
|
2018-04-03 12:29:04 +02:00
|
|
|
if (GraphicsEnvironment.isHeadless() || args.length > 0) {
|
2014-04-23 06:16:23 +02:00
|
|
|
handleArguments(args);
|
2014-03-01 11:13:32 +01:00
|
|
|
} else {
|
2018-03-30 11:58:46 +02:00
|
|
|
if (SystemUtils.IS_OS_MAC_OSX) {
|
|
|
|
System.setProperty("apple.laf.useScreenMenuBar", "true");
|
|
|
|
System.setProperty("com.apple.mrj.application.apple.menu.about.name", "RipMe");
|
|
|
|
}
|
|
|
|
|
|
|
|
Utils.configureLogger();
|
|
|
|
|
|
|
|
logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion());
|
|
|
|
|
2014-03-01 11:13:32 +01:00
|
|
|
MainWindow mw = new MainWindow();
|
|
|
|
SwingUtilities.invokeLater(mw);
|
2014-02-27 10:28:23 +01:00
|
|
|
}
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Creates an abstract ripper and instructs it to rip.
|
|
|
|
* @param url URL to be ripped
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static void rip(URL url) throws Exception {
|
2014-04-23 06:16:23 +02:00
|
|
|
AbstractRipper ripper = AbstractRipper.getRipper(url);
|
2014-06-11 05:22:28 +02:00
|
|
|
ripper.setup();
|
2014-04-23 06:16:23 +02:00
|
|
|
ripper.rip();
|
2014-02-26 08:44:22 +01:00
|
|
|
}
|
2014-02-28 04:49:28 +01:00
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* For dealing with command-line arguments.
|
|
|
|
* @param args Array of Command-line arguments
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static void handleArguments(String[] args) {
|
2014-02-27 10:28:23 +01:00
|
|
|
CommandLine cl = getArgs(args);
|
2018-03-30 11:58:46 +02:00
|
|
|
|
|
|
|
if (cl.hasOption('h') || args.length == 0) {
|
2014-02-27 10:28:23 +01:00
|
|
|
HelpFormatter hf = new HelpFormatter();
|
2014-04-23 06:16:23 +02:00
|
|
|
hf.printHelp("java -jar ripme.jar [OPTIONS]", getOptions());
|
2014-02-27 10:28:23 +01:00
|
|
|
System.exit(0);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
|
|
|
Utils.configureLogger();
|
|
|
|
logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion());
|
|
|
|
|
2014-02-27 10:28:23 +01:00
|
|
|
if (cl.hasOption('w')) {
|
|
|
|
Utils.setConfigBoolean("file.overwrite", true);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-04-06 11:41:04 +02:00
|
|
|
if (cl.hasOption('t')) {
|
|
|
|
Utils.setConfigInteger("threads.size", Integer.parseInt(cl.getOptionValue('t')));
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-12-24 05:19:52 +01:00
|
|
|
if (cl.hasOption('4')) {
|
|
|
|
Utils.setConfigBoolean("errors.skip404", true);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-04-23 06:16:23 +02:00
|
|
|
if (cl.hasOption('r')) {
|
|
|
|
// Re-rip all via command-line
|
|
|
|
List<String> history = Utils.getConfigList("download.history");
|
|
|
|
for (String urlString : history) {
|
|
|
|
try {
|
|
|
|
URL url = new URL(urlString.trim());
|
|
|
|
rip(url);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("[!] Failed to rip URL " + urlString, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
logger.warn("[!] Interrupted while re-ripping history");
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Exit
|
|
|
|
System.exit(0);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2015-10-18 01:55:09 +02:00
|
|
|
if (cl.hasOption('R')) {
|
|
|
|
loadHistory();
|
2017-12-28 06:04:23 +01:00
|
|
|
if (HISTORY.toList().isEmpty()) {
|
2017-11-17 11:35:37 +01:00
|
|
|
logger.error("There are no history entries to re-rip. Rip some albums first");
|
2015-10-18 01:55:09 +02:00
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
int added = 0;
|
|
|
|
for (HistoryEntry entry : HISTORY.toList()) {
|
2017-05-10 00:03:12 +02:00
|
|
|
if (entry.selected) {
|
2015-10-18 01:55:09 +02:00
|
|
|
added++;
|
|
|
|
try {
|
|
|
|
URL url = new URL(entry.url);
|
|
|
|
rip(url);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("[!] Failed to rip URL " + entry.url, e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
Thread.sleep(500);
|
|
|
|
} catch (InterruptedException e) {
|
|
|
|
logger.warn("[!] Interrupted while re-ripping history");
|
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (added == 0) {
|
2017-11-17 11:35:37 +01:00
|
|
|
logger.error("No history entries have been 'Checked'\n" +
|
2017-05-10 00:03:12 +02:00
|
|
|
"Check an entry by clicking the checkbox to the right of the URL or Right-click a URL to check/uncheck all items");
|
2015-10-18 01:55:09 +02:00
|
|
|
System.exit(-1);
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-12-24 05:19:52 +01:00
|
|
|
if (cl.hasOption('d')) {
|
|
|
|
Utils.setConfigBoolean("download.save_order", true);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-12-24 05:19:52 +01:00
|
|
|
if (cl.hasOption('D')) {
|
|
|
|
Utils.setConfigBoolean("download.save_order", false);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2014-12-24 05:19:52 +01:00
|
|
|
if ((cl.hasOption('d'))&&(cl.hasOption('D'))) {
|
2017-11-17 11:35:37 +01:00
|
|
|
logger.error("\nCannot specify '-d' and '-D' simultaneously");
|
2014-12-24 05:19:52 +01:00
|
|
|
System.exit(-1);
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2017-05-10 00:03:12 +02:00
|
|
|
if (cl.hasOption('l')) {
|
2015-10-11 22:19:31 +02:00
|
|
|
// change the default rips directory
|
|
|
|
Utils.setConfigString("rips.directory", cl.getOptionValue('l'));
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2016-08-18 02:10:07 +02:00
|
|
|
if (cl.hasOption('f')) {
|
2017-05-10 00:03:12 +02:00
|
|
|
String filename = cl.getOptionValue('f');
|
2014-04-23 06:16:23 +02:00
|
|
|
try {
|
2016-08-18 02:10:07 +02:00
|
|
|
String url;
|
2017-05-10 00:03:12 +02:00
|
|
|
BufferedReader br = new BufferedReader(new FileReader(filename));
|
2017-05-10 02:50:32 +02:00
|
|
|
while ((url = br.readLine()) != null) {
|
2016-08-18 02:10:07 +02:00
|
|
|
// loop through each url in the file and proces each url individually.
|
|
|
|
ripURL(url.trim(), cl.hasOption("n"));
|
|
|
|
}
|
|
|
|
} catch (FileNotFoundException fne) {
|
|
|
|
logger.error("[!] File containing list of URLs not found. Cannot continue.");
|
|
|
|
} catch (IOException ioe) {
|
|
|
|
logger.error("[!] Failed reading file containing list of URLs. Cannot continue.");
|
|
|
|
}
|
|
|
|
}
|
2018-03-30 11:58:46 +02:00
|
|
|
|
2016-08-18 02:10:07 +02:00
|
|
|
if (cl.hasOption('u')) {
|
|
|
|
String url = cl.getOptionValue('u').trim();
|
|
|
|
ripURL(url, cl.hasOption("n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Attempt to rip targetURL.
|
|
|
|
* @param targetURL URL to rip
|
|
|
|
* @param saveConfig Whether or not you want to save the config (?)
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static void ripURL(String targetURL, boolean saveConfig) {
|
2016-08-18 02:10:07 +02:00
|
|
|
try {
|
|
|
|
URL url = new URL(targetURL);
|
|
|
|
rip(url);
|
|
|
|
List<String> history = Utils.getConfigList("download.history");
|
2017-12-28 06:04:23 +01:00
|
|
|
if (!history.contains(url.toExternalForm())) {//if you haven't already downloaded the file before
|
|
|
|
history.add(url.toExternalForm());//add it to history so you won't have to redownload
|
2016-08-18 02:10:07 +02:00
|
|
|
Utils.setConfigList("download.history", Arrays.asList(history.toArray()));
|
2017-05-10 01:13:42 +02:00
|
|
|
if (saveConfig) {
|
2016-08-18 02:10:07 +02:00
|
|
|
Utils.saveConfig();
|
2014-04-23 06:16:23 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-18 02:10:07 +02:00
|
|
|
} catch (MalformedURLException e) {
|
|
|
|
logger.error("[!] Given URL is not valid. Expected URL format is http://domain.com/...");
|
|
|
|
// System.exit(-1);
|
|
|
|
} catch (Exception e) {
|
|
|
|
logger.error("[!] Error while ripping URL " + targetURL, e);
|
|
|
|
// System.exit(-1);
|
2014-04-23 06:16:23 +02:00
|
|
|
}
|
2014-02-27 10:28:23 +01:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Creates an Options object, returns it.
|
|
|
|
* @return Returns all acceptable command-line options.
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static Options getOptions() {
|
2014-02-27 10:28:23 +01:00
|
|
|
Options opts = new Options();
|
2017-05-10 00:03:12 +02:00
|
|
|
opts.addOption("h", "help", false, "Print the help");
|
|
|
|
opts.addOption("u", "url", true, "URL of album to rip");
|
|
|
|
opts.addOption("t", "threads", true, "Number of download threads per rip");
|
2014-02-27 10:28:23 +01:00
|
|
|
opts.addOption("w", "overwrite", false, "Overwrite existing files");
|
2017-05-10 00:03:12 +02:00
|
|
|
opts.addOption("r", "rerip", false, "Re-rip all ripped albums");
|
|
|
|
opts.addOption("R", "rerip-selected", false, "Re-rip all selected albums");
|
|
|
|
opts.addOption("d", "saveorder", false, "Save the order of images in album");
|
2014-12-24 05:19:52 +01:00
|
|
|
opts.addOption("D", "nosaveorder", false, "Don't save order of images");
|
2017-05-10 00:03:12 +02:00
|
|
|
opts.addOption("4", "skip404", false, "Don't retry after a 404 (not found) error");
|
2015-10-11 22:19:31 +02:00
|
|
|
opts.addOption("l", "ripsdirectory", true, "Rips Directory (Default: ./rips)");
|
2016-02-14 17:09:45 +01:00
|
|
|
opts.addOption("n", "no-prop-file", false, "Do not create properties file.");
|
2016-08-18 02:10:07 +02:00
|
|
|
opts.addOption("f", "urls-file", true, "Rip URLs from a file.");
|
2017-10-12 10:12:10 +02:00
|
|
|
opts.addOption("v", "version", false, "Show current version");
|
2014-02-27 10:28:23 +01:00
|
|
|
return opts;
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:04:23 +01:00
|
|
|
/**
|
|
|
|
* Tries to parse commandline arguments.
|
|
|
|
* @param args Array of commandline arguments.
|
|
|
|
* @return CommandLine object containing arguments.
|
|
|
|
*/
|
2017-10-24 16:33:28 +02:00
|
|
|
private static CommandLine getArgs(String[] args) {
|
2014-02-27 10:28:23 +01:00
|
|
|
BasicParser parser = new BasicParser();
|
|
|
|
try {
|
2017-10-24 16:33:28 +02:00
|
|
|
return parser.parse(getOptions(), args, false);
|
2014-02-27 10:28:23 +01:00
|
|
|
} catch (ParseException e) {
|
2017-05-09 23:02:24 +02:00
|
|
|
logger.error("[!] Error while parsing command-line arguments: " + Arrays.toString(args), e);
|
2014-02-27 10:28:23 +01:00
|
|
|
System.exit(-1);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-12-28 06:04:23 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Loads history from history file into memory.
|
|
|
|
*/
|
2015-10-18 01:55:09 +02:00
|
|
|
private static void loadHistory() {
|
2017-10-15 14:21:58 +02:00
|
|
|
File historyFile = new File(Utils.getConfigDir() + File.separator + "history.json");
|
2015-10-18 01:55:09 +02:00
|
|
|
HISTORY.clear();
|
|
|
|
if (historyFile.exists()) {
|
|
|
|
try {
|
2017-10-15 14:21:58 +02:00
|
|
|
logger.info("Loading history from " + historyFile.getCanonicalPath());
|
|
|
|
HISTORY.fromFile(historyFile.getCanonicalPath());
|
2015-10-18 01:55:09 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
logger.error("Failed to load history from file " + historyFile, e);
|
2017-11-17 11:35:37 +01:00
|
|
|
logger.warn(
|
2015-10-18 01:55:09 +02:00
|
|
|
"RipMe failed to load the history file at " + historyFile.getAbsolutePath() + "\n\n" +
|
2017-05-10 00:03:12 +02:00
|
|
|
"Error: " + e.getMessage() + "\n\n" +
|
2015-10-18 01:55:09 +02:00
|
|
|
"Closing RipMe will automatically overwrite the contents of this file,\n" +
|
|
|
|
"so you may want to back the file up before closing RipMe!");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
logger.info("Loading history from configuration");
|
|
|
|
HISTORY.fromList(Utils.getConfigList("download.history"));
|
|
|
|
if (HISTORY.toList().size() == 0) {
|
|
|
|
// Loaded from config, still no entries.
|
|
|
|
// Guess rip history based on rip folder
|
2017-10-24 16:33:28 +02:00
|
|
|
String[] dirs = Utils.getWorkingDirectory().list((dir, file) -> new File(dir.getAbsolutePath() + File.separator + file).isDirectory());
|
2015-10-18 01:55:09 +02:00
|
|
|
for (String dir : dirs) {
|
|
|
|
String url = RipUtils.urlFromDirectoryName(dir);
|
|
|
|
if (url != null) {
|
|
|
|
// We found one, add it to history
|
|
|
|
HistoryEntry entry = new HistoryEntry();
|
|
|
|
entry.url = url;
|
|
|
|
HISTORY.add(entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-02-25 10:28:22 +01:00
|
|
|
}
|