-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added naive support for i.rarchives.com rips #8
- Loading branch information
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
89 changes: 89 additions & 0 deletions
89
src/main/java/com/rarchives/ripme/ripper/rippers/IrarchivesRipper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package com.rarchives.ripme.ripper.rippers; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.jsoup.Jsoup; | ||
|
||
import com.rarchives.ripme.ripper.AlbumRipper; | ||
import com.rarchives.ripme.ripper.rippers.ImgurRipper.ImgurAlbum; | ||
import com.rarchives.ripme.ripper.rippers.ImgurRipper.ImgurImage; | ||
import com.rarchives.ripme.ui.RipStatusMessage.STATUS; | ||
import com.rarchives.ripme.utils.Utils; | ||
|
||
public class IrarchivesRipper extends AlbumRipper { | ||
|
||
private static final int TIMEOUT = 60000; // Long timeout for this poorly-optimized site. | ||
|
||
private static final String DOMAIN = "i.rarchives.com", | ||
HOST = "irarchives"; | ||
private static final Logger logger = Logger.getLogger(IrarchivesRipper.class); | ||
|
||
public IrarchivesRipper(URL url) throws IOException { | ||
super(url); | ||
} | ||
|
||
@Override | ||
public boolean canRip(URL url) { | ||
return url.getHost().endsWith(DOMAIN); | ||
} | ||
|
||
@Override | ||
public URL sanitizeURL(URL url) throws MalformedURLException { | ||
String u = url.toExternalForm(); | ||
String searchTerm = u.substring(u.indexOf("?") + 1); | ||
return new URL("http://i.rarchives.com/search.cgi?" + searchTerm); | ||
} | ||
|
||
@Override | ||
public void rip() throws IOException { | ||
logger.info(" Retrieving " + this.url); | ||
String jsonString = Jsoup.connect(this.url.toExternalForm()) | ||
.ignoreContentType(true) | ||
.timeout(TIMEOUT) | ||
.execute() | ||
.body(); | ||
JSONObject json = new JSONObject(jsonString); | ||
JSONArray posts = json.getJSONArray("posts"); | ||
if (posts.length() == 0) { | ||
logger.error("No posts found at " + this.url); | ||
sendUpdate(STATUS.DOWNLOAD_ERRORED, "No posts found at " + this.url); | ||
throw new IOException("No posts found at this URL"); | ||
} | ||
for (int i = 0; i < posts.length(); i++) { | ||
JSONObject post = (JSONObject) posts.get(i); | ||
String theUrl = post.getString("url"); | ||
if (theUrl.contains("imgur.com/a/")) { | ||
ImgurAlbum album = ImgurRipper.getImgurAlbum(new URL(theUrl)); | ||
int albumIndex = 0; | ||
for (ImgurImage image : album.images) { | ||
albumIndex++; | ||
String saveAs = String.format("%s-%03d_", post.getString("hexid"), albumIndex); | ||
addURLToDownload(image.url, saveAs); | ||
} | ||
} | ||
else { | ||
theUrl = post.getString("imageurl"); | ||
String saveAs = String.format("%s-", post.getString("hexid")); | ||
addURLToDownload(new URL(theUrl), saveAs); | ||
} | ||
} | ||
waitForThreads(); | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return HOST; | ||
} | ||
|
||
@Override | ||
public String getGID(URL url) throws MalformedURLException { | ||
String u = url.toExternalForm(); | ||
String searchTerm = u.substring(u.indexOf("?") + 1); | ||
return Utils.filesystemSafe(searchTerm); | ||
} | ||
} |