-
Notifications
You must be signed in to change notification settings - Fork 666
Expand file tree
/
Copy pathApp.java
More file actions
409 lines (366 loc) · 15.3 KB
/
App.java
File metadata and controls
409 lines (366 loc) · 15.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
package com.rarchives.ripme;
import com.rarchives.ripme.ripper.AbstractRipper;
import com.rarchives.ripme.ui.History;
import com.rarchives.ripme.ui.HistoryEntry;
import com.rarchives.ripme.ui.MainWindow;
import com.rarchives.ripme.ui.UpdateUtils;
import com.rarchives.ripme.utils.Proxy;
import com.rarchives.ripme.utils.RipUtils;
import com.rarchives.ripme.utils.Utils;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.lang.SystemUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import javax.swing.*;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.stream.Stream;
/**
* Entry point to application.
* This is where all the fun happens, with the main method.
* Decides to display UI or to run silently via command-line.
*
* As the "controller" to all other classes, it parses command line parameters and loads the history.
*/
public class App {
private static final Logger logger = LogManager.getLogger(App.class);
public static String stringToAppendToFoldername = null;
private static final History HISTORY = new History();
/**
* Where everything starts. Takes in, and tries to parse as many commandline arguments as possible.
* Otherwise, it launches a GUI.
*
* @param args Array of command line arguments.
*/
public static void main(String[] args) throws IOException {
CommandLine cl = getArgs(args);
if (args.length > 0 && cl.hasOption('v')){
System.out.println(UpdateUtils.getThisJarVersion());
System.exit(0);
}
if (Utils.getConfigString("proxy.http", null) != null) {
Proxy.setHTTPProxy(Utils.getConfigString("proxy.http", null));
} else if (Utils.getConfigString("proxy.socks", null) != null) {
Proxy.setSocks(Utils.getConfigString("proxy.socks", null));
}
// This has to be here instead of handleArgs because handleArgs isn't parsed until after a item is ripper
if (cl.hasOption("a")) {
logger.info(cl.getOptionValue("a"));
stringToAppendToFoldername = cl.getOptionValue("a");
}
if (GraphicsEnvironment.isHeadless() || args.length > 0) {
handleArguments(args);
} else {
// Antialiasing hint, especially for Linux
System.setProperty("awt.useSystemAAFontSettings", "on");
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());
MainWindow mw = new MainWindow();
SwingUtilities.invokeLater(mw);
}
}
/**
* Creates an abstract ripper and instructs it to rip.
* @param url URL to be ripped
* @throws Exception Nothing too specific here, just a catch-all.
*
*/
private static void rip(URL url) throws Exception {
AbstractRipper ripper = AbstractRipper.getRipper(url);
ripper.setup();
ripper.rip();
String u = ripper.getURL().toExternalForm();
Date date = new Date();
if (HISTORY.containsURL(u)) {
HistoryEntry entry = HISTORY.getEntryByURL(u);
entry.modifiedDate = date;
} else {
HistoryEntry entry = new HistoryEntry();
entry.url = u;
entry.dir = ripper.getWorkingDir().getAbsolutePath();
try {
entry.title = ripper.getAlbumTitle(ripper.getURL());
} catch (MalformedURLException ignored) { }
HISTORY.add(entry);
}
}
/**
* For dealing with command-line arguments.
* @param args Array of Command-line arguments
*/
private static void handleArguments(String[] args) throws IOException {
CommandLine cl = getArgs(args);
//Help (list commands)
if (cl.hasOption('h') || args.length == 0) {
HelpFormatter hf = new HelpFormatter();
hf.printHelp("java -jar ripme.jar [OPTIONS]", getOptions());
System.exit(0);
}
Utils.configureLogger();
logger.info("Initialized ripme v" + UpdateUtils.getThisJarVersion());
//Set history file
if (cl.hasOption('H')) {
String historyLocation = cl.getOptionValue('H');
Utils.setConfigString("history.location", historyLocation);
logger.info("Set history file to " + historyLocation);
}
//Allow file overwriting
if (cl.hasOption('w')) {
Utils.setConfigBoolean("file.overwrite", true);
}
//SOCKS proxy server
if (cl.hasOption('s')) {
String sservfull = cl.getOptionValue('s').trim();
Proxy.setSocks(sservfull);
}
//HTTP proxy server
if (cl.hasOption('p')) {
String proxyserverfull = cl.getOptionValue('p').trim();
Proxy.setHTTPProxy(proxyserverfull);
}
//Number of threads
if (cl.hasOption('t')) {
Utils.setConfigInteger("threads.size", Integer.parseInt(cl.getOptionValue('t')));
}
//Ignore 404
if (cl.hasOption('4')) {
Utils.setConfigBoolean("errors.skip404", true);
}
//Destination directory
if (cl.hasOption('l')) {
// change the default rips directory
Utils.setConfigString("rips.directory", cl.getOptionValue('l'));
}
//Re-rip <i>all</i> previous albums
if (cl.hasOption('r')) {
// Re-rip all via command-line
loadHistory();
if (HISTORY.toList().isEmpty()) {
logger.error("There are no history entries to re-rip. Rip some albums first");
System.exit(-1);
}
for (HistoryEntry entry : HISTORY.toList()) {
try {
URL url = new URI(entry.url).toURL();
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);
}
}
// Exit
System.exit(0);
}
//Re-rip all <i>selected</i> albums
if (cl.hasOption('R')) {
loadHistory();
if (HISTORY.toList().isEmpty()) {
logger.error("There are no history entries to re-rip. Rip some albums first");
System.exit(-1);
}
int added = 0;
for (HistoryEntry entry : HISTORY.toList()) {
if (entry.selected) {
added++;
try {
URL url = new URI(entry.url).toURL();
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) {
logger.error("No history entries have been 'Checked'\n" +
"Check an entry by clicking the checkbox to the right of the URL or Right-click a URL to check/uncheck all items");
System.exit(-1);
}
}
//Save the order of images in album
if (cl.hasOption('d')) {
Utils.setConfigBoolean("download.save_order", true);
}
//Don't save the order of images in album
if (cl.hasOption('D')) {
Utils.setConfigBoolean("download.save_order", false);
}
//In case specify both, break and exit since it isn't possible.
if ((cl.hasOption('d'))&&(cl.hasOption('D'))) {
logger.error("\nCannot specify '-d' and '-D' simultaneously");
System.exit(-1);
}
//Read URLs from File
if (cl.hasOption('f')) {
Path urlfile = Paths.get(cl.getOptionValue('f'));
try (BufferedReader br = Files.newBufferedReader(urlfile)) {
String url;
while ((url = br.readLine()) != null) {
if (url.startsWith("//") || url.startsWith("#")) {
logger.debug("Skipping over line \"" + url + "\"because it is a comment");
} else {
// loop through each url in the file and process 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.");
}
}
//The URL to rip.
if (cl.hasOption('u')) {
loadHistory();
String url = cl.getOptionValue('u').trim();
ripURL(url, !cl.hasOption("n"));
}
if (cl.hasOption('j')) {
UpdateUtils.updateProgramCLI();
}
}
/**
* Attempt to rip targetURL.
* @param targetURL URL to rip
* @param saveConfig Whether you want to save the config (?)
*/
private static void ripURL(String targetURL, boolean saveConfig) {
try {
URL url = new URI(targetURL).toURL();
rip(url);
saveHistory();
} 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);
}
}
/**
* Creates an Options object, returns it.
* @return Returns all acceptable command-line options.
*/
private static Options getOptions() {
Options opts = new Options();
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");
opts.addOption("w", "overwrite", false, "Overwrite existing files");
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");
opts.addOption("D", "nosaveorder", false, "Don't save order of images");
opts.addOption("4", "skip404", false, "Don't retry after a 404 (not found) error");
opts.addOption("l", "ripsdirectory", true, "Rips Directory (Default: ./rips)");
opts.addOption("n", "no-prop-file", false, "Do not create properties file.");
opts.addOption("f", "urls-file", true, "Rip URLs from a file.");
opts.addOption("v", "version", false, "Show current version");
opts.addOption("s", "socks-server", true, "Use socks server ([user:password]@host[:port])");
opts.addOption("p", "proxy-server", true, "Use HTTP Proxy server ([user:password]@host[:port])");
opts.addOption("j", "update", false, "Update ripme");
opts.addOption("a","append-to-folder", true, "Append a string to the output folder name");
opts.addOption("H", "history", true, "Set history file location.");
return opts;
}
/**
* Tries to parse commandline arguments.
* @param args Array of commandline arguments.
* @return CommandLine object containing arguments.
*/
private static CommandLine getArgs(String[] args) {
var parser = new DefaultParser();
try {
return parser.parse(getOptions(), args, false);
} catch (ParseException e) {
logger.error("[!] Error while parsing command-line arguments: " + Arrays.toString(args), e);
System.exit(-1);
return null;
}
}
/**
* Loads history from history file into memory.
*/
private static void loadHistory() throws IOException {
Path historyFile = Paths.get(Utils.getConfigDir() + "/history.json");
HISTORY.clear();
if (Files.exists(historyFile)) {
try {
logger.info("Loading history from " + historyFile);
HISTORY.fromFile(historyFile.toString());
} catch (IOException e) {
logger.error("Failed to load history from file " + historyFile, e);
logger.warn(
"RipMe failed to load the history file at " + historyFile + "\n\n" +
"Error: " + e.getMessage() + "\n\n" +
"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().isEmpty()) {
// Loaded from config, still no entries.
// Guess rip history based on rip folder
Stream<Path> stream = Files.list(Utils.getWorkingDirectory())
.filter(Files::isDirectory);
stream.forEach(dir -> {
String url = RipUtils.urlFromDirectoryName(dir.toString());
if (url != null) {
// We found one, add it to history
HistoryEntry entry = new HistoryEntry();
entry.url = url;
HISTORY.add(entry);
}
});
}
}
}
/*
* @see MainWindow.saveHistory
*/
private static void saveHistory() {
Path historyFile = Paths.get(Utils.getConfigDir() + "/history.json");
try {
if (!Files.exists(historyFile)) {
Files.createDirectories(historyFile.getParent());
Files.createFile(historyFile);
}
HISTORY.toFile(historyFile.toString());
Utils.setConfigList("download.history", Collections.emptyList());
} catch (IOException e) {
logger.error("Failed to save history to file " + historyFile, e);
}
}
}