Got log4j logging and apache config working (?)

This commit is contained in:
4pr0n 2014-02-25 23:44:22 -08:00
parent db9a87595e
commit 582ecd8ae8
9 changed files with 159 additions and 44 deletions

View File

@ -12,12 +12,13 @@
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<classpathentry kind="src" path="config"/>
<classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry exported="true" kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>

View File

@ -0,0 +1,5 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.5
org.eclipse.jdt.core.compiler.compliance=1.5
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.5

View File

@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1

10
pom.xml
View File

@ -25,6 +25,16 @@
<artifactId>json</artifactId>
<version>20140107</version>
</dependency>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<build>
<plugins>

View File

@ -3,6 +3,9 @@ package com.rarchives.ripme;
import java.io.IOException;
import java.net.URL;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import com.rarchives.ripme.ripper.rippers.ImagefapRipper;
/**
@ -10,10 +13,17 @@ import com.rarchives.ripme.ripper.rippers.ImagefapRipper;
*/
public class App {
public static void main( String[] args ) throws IOException {
Logger logger = Logger.getLogger(App.class);
PropertyConfigurator.configure("config/log4j.properties");
logger.debug("Testing");
URL url = new URL("http://www.imagefap.com/pictures/4117023/Mirror-flat-stomach-small-firm-tits");
System.out.println("URL: " + url.toExternalForm());
ImagefapRipper ir = new ImagefapRipper(url);
System.out.println("Ripping");
ir.rip();
}
public static void initialize() {
}
}

View File

@ -1,24 +1,38 @@
package com.rarchives.ripme.ripper;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import com.rarchives.ripme.utils.Utils;
public abstract class AbstractRipper implements RipperInterface {
protected URL url;
protected File workingDir = null;
public AbstractRipper(URL url) throws MalformedURLException {
// Ensure that the inheriting class can rip this URL.
public abstract void rip() throws IOException;
public abstract void setWorkingDir() throws IOException;
/**
* Ensures inheriting ripper can rip this URL.
* @param url
* URL to rip.
* @throws IOException
* If anything goes wrong.
*/
public AbstractRipper(URL url) throws IOException {
if (!canRip(url)) {
throw new MalformedURLException("Unable to rip url: " + url);
}
this.url = url;
sanitizeURL();
setWorkingDir();
workingDir = Utils.getWorkingDirectory();
}
public URL getURL() {
return url;
}
}
}

View File

@ -1,11 +1,11 @@
package com.rarchives.ripme.ripper;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
public interface RipperInterface {
public void rip() throws IOException;
public void processURL(String url);
public boolean canRip(URL url);
public void sanitizeURL() throws MalformedURLException;
public void setWorkingDir() throws IOException;
}

View File

@ -1,5 +1,6 @@
package com.rarchives.ripme.ripper.rippers;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
@ -11,40 +12,58 @@ import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import com.rarchives.ripme.ripper.AbstractRipper;
import com.rarchives.ripme.utils.Utils;
public class ImagefapRipper extends AbstractRipper {
private static final String HOST = "imagefap.com";
private static final String HOST = "imagefap.com";
public ImagefapRipper(URL url) throws MalformedURLException {
super(url);
}
/**
* Reformat given URL into the desired format (all images on single page)
*/
public void sanitizeURL() throws MalformedURLException {
String gid = null;
Pattern p = Pattern.compile("^.*imagefap.com/gallery.php?gid=([0-9]{1,}).*$");
Matcher m = p.matcher(this.url.toExternalForm());
if (m.matches()) {
gid = m.group(1);
} else {
p = Pattern.compile("^.*imagefap.com/pictures/([0-9]{1,}).*$");
m = p.matcher(this.url.toExternalForm());
private String gid;
public ImagefapRipper(URL url) throws IOException {
super(url);
this.gid = getGID(url);
}
/**
* Reformat given URL into the desired format (all images on single page)
*/
public void sanitizeURL() throws MalformedURLException {
this.url = new URL("http://www.imagefap.com/gallery.php?gid="
+ this.gid + "&view=2");
}
private static String getGID(URL url) throws MalformedURLException {
String gid = null;
Pattern p = Pattern.compile("^.*imagefap.com/gallery.php?gid=([0-9]{1,}).*$");
Matcher m = p.matcher(url.toExternalForm());
if (m.matches()) {
gid = m.group(1);
} else {
p = Pattern.compile("^.*imagefap.com/pictures/([0-9]{1,}).*$");
m = p.matcher(url.toExternalForm());
if (m.matches()) {
gid = m.group(1);
}
}
if (gid == null) {
throw new MalformedURLException("Expected imagefap.com gallery formats:"
+ "imagefap.com/gallery.php?gid=####... or"
+ "imagefap.com/pictures/####...");
}
this.url = new URL("http://www.imagefap.com/gallery.php?gid=" + gid + "&view=2");
}
}
if (gid == null) {
throw new MalformedURLException(
"Expected imagefap.com gallery formats:"
+ "imagefap.com/gallery.php?gid=####... or"
+ "imagefap.com/pictures/####...");
}
return gid;
}
public void rip() throws IOException {
@Override
public void setWorkingDir() throws IOException {
String path = Utils.getWorkingDirectory().getCanonicalPath();
path += this.gid + File.separator;
this.workingDir = new File(path);
}
@Override
public void rip() throws IOException {
System.err.println("Connecting to " + this.url.toExternalForm());
Document doc = Jsoup.connect(this.url.toExternalForm()).get();
for (Element thumb : doc.select("#gallery img")) {
@ -52,17 +71,22 @@ public class ImagefapRipper extends AbstractRipper {
continue;
}
String image = thumb.attr("src");
image = image.replaceAll("http://x.*.fap.to/images/thumb/", "http://fap.to/images/full/");
image = image.replaceAll("http://x.*.fap.to/images/thumb/",
"http://fap.to/images/full/");
processURL(image);
System.err.println(image);
}
}
}
public boolean canRip(URL url) {
if (!url.getHost().endsWith(HOST)) {
return false;
}
return true;
}
public void processURL(String url) {
}
public boolean canRip(URL url) {
if (!url.getHost().endsWith(HOST)) {
return false;
}
return true;
}
}

View File

@ -0,0 +1,47 @@
package com.rarchives.ripme.utils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.jsoup.Connection.Response;
import org.jsoup.Jsoup;
public class Utils {
public static final String RIP_DIRECTORY = "rips";
public static File getWorkingDirectory() throws IOException {
String path = new File(".").getCanonicalPath() + File.separator;
path += RIP_DIRECTORY + File.separator;
File workingDir = new File(path);
if (!workingDir.exists()) {
workingDir.mkdirs();
}
return workingDir;
}
public static String getConfigString(String key) {
Configuration config = null;
try {
config = new PropertiesConfiguration("rip.properties");
} catch (ConfigurationException e) {
System.err.println(e);
return null;
}
return config.getString(key);
}
public static void downloadFile(String url, File saveAs) throws IOException {
Response response = Jsoup.connect(url)
.ignoreContentType(true)
.execute();
FileOutputStream out = (new FileOutputStream(saveAs));
out.write(response.bodyAsBytes());
out.close();
}
}