|
| 1 | +package com.rarchives.ripme.ripper.rippers; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.net.MalformedURLException; |
| 5 | +import java.net.URL; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | +import java.util.regex.Matcher; |
| 9 | +import java.util.regex.Pattern; |
| 10 | + |
| 11 | +import org.apache.log4j.Logger; |
| 12 | +import org.jsoup.Connection.Method; |
| 13 | +import org.jsoup.Jsoup; |
| 14 | +import org.jsoup.Connection.Response; |
| 15 | +import org.jsoup.nodes.Document; |
| 16 | +import org.jsoup.nodes.Element; |
| 17 | +import org.jsoup.select.Elements; |
| 18 | + |
| 19 | +import com.rarchives.ripme.ripper.AlbumRipper; |
| 20 | +import com.rarchives.ripme.ui.RipStatusMessage.STATUS; |
| 21 | + |
| 22 | +public class GifyoRipper extends AlbumRipper { |
| 23 | + |
| 24 | + private static final String DOMAIN = "gifyo.com", |
| 25 | + HOST = "gifyo"; |
| 26 | + private static final Logger logger = Logger.getLogger(GifyoRipper.class); |
| 27 | + |
| 28 | + public GifyoRipper(URL url) throws IOException { |
| 29 | + super(url); |
| 30 | + } |
| 31 | + |
| 32 | + @Override |
| 33 | + public boolean canRip(URL url) { |
| 34 | + return (url.getHost().endsWith(DOMAIN)); |
| 35 | + } |
| 36 | + |
| 37 | + @Override |
| 38 | + public URL sanitizeURL(URL url) throws MalformedURLException { |
| 39 | + Pattern p = Pattern.compile("^https?://gifyo\\.com/([a-zA-Z0-9\\-_]+)/?$"); |
| 40 | + Matcher m = p.matcher(url.toExternalForm()); |
| 41 | + if (m.matches()) { |
| 42 | + return new URL("http://gifyo.com/" + m.group(1) + "/"); |
| 43 | + } |
| 44 | + throw new MalformedURLException("Expected username in URL (gifyo.com/username/ and not " + url); |
| 45 | + } |
| 46 | + @Override |
| 47 | + public void rip() throws IOException { |
| 48 | + int page = 0; |
| 49 | + Map<String,String> cookies = new HashMap<String,String>(); |
| 50 | + while (true) { |
| 51 | + this.sendUpdate(STATUS.LOADING_RESOURCE, this.url.toExternalForm() + " (page #" + page + ")"); |
| 52 | + logger.info(" Retrieving " + this.url + "(page #" + page + ")"); |
| 53 | + Response resp = null; |
| 54 | + if (page == 0) { |
| 55 | + resp = Jsoup.connect(this.url.toExternalForm()) |
| 56 | + .ignoreContentType(true) |
| 57 | + .userAgent(USER_AGENT) |
| 58 | + .method(Method.GET) |
| 59 | + .execute(); |
| 60 | + cookies = resp.cookies(); |
| 61 | + } |
| 62 | + else { |
| 63 | + Map<String,String> postData = new HashMap<String,String>(); |
| 64 | + postData.put("cmd", "refreshData"); |
| 65 | + postData.put("view", "gif"); |
| 66 | + postData.put("layout", "grid"); |
| 67 | + postData.put("page", Integer.toString(page)); |
| 68 | + resp = Jsoup.connect(this.url.toExternalForm()) |
| 69 | + .ignoreContentType(true) |
| 70 | + .userAgent(USER_AGENT) |
| 71 | + .data(postData) |
| 72 | + .cookies(cookies) |
| 73 | + .method(Method.POST) |
| 74 | + .execute(); |
| 75 | + cookies.putAll(resp.cookies()); |
| 76 | + } |
| 77 | + Document doc = resp.parse(); |
| 78 | + Elements images = doc.select("div.gif img"); |
| 79 | + logger.info("Found " + images.size() + " images"); |
| 80 | + for (Element image : images) { |
| 81 | + String imageUrl = image.attr("src"); |
| 82 | + if (imageUrl.startsWith("//")) { |
| 83 | + imageUrl = "http:" + imageUrl; |
| 84 | + } |
| 85 | + imageUrl = imageUrl.replace("/medium/", "/large/"); |
| 86 | + imageUrl = imageUrl.replace("_s.gif", ".gif"); |
| 87 | + addURLToDownload(new URL(imageUrl)); |
| 88 | + } |
| 89 | + if (images.size() == 0) { |
| 90 | + if (doc.html().contains("profile is private")) { |
| 91 | + sendUpdate(STATUS.RIP_ERRORED, "User has private profile"); |
| 92 | + throw new IOException("User has private profile"); |
| 93 | + } |
| 94 | + else { |
| 95 | + logger.info("Page " + page + " has 0 images"); |
| 96 | + } |
| 97 | + break; |
| 98 | + } |
| 99 | + try { |
| 100 | + Thread.sleep(3000); |
| 101 | + } catch (InterruptedException e) { |
| 102 | + logger.error("[!] Interrupted while waiting to load next album:", e); |
| 103 | + break; |
| 104 | + } |
| 105 | + page++; |
| 106 | + } |
| 107 | + waitForThreads(); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getHost() { |
| 112 | + return HOST; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getGID(URL url) throws MalformedURLException { |
| 117 | + Pattern p = Pattern.compile("^https?://[w.]*gifyo.com/([a-zA-Z0-9\\-_]+)/?$"); |
| 118 | + Matcher m = p.matcher(url.toExternalForm()); |
| 119 | + if (m.matches()) { |
| 120 | + return m.group(1); |
| 121 | + } |
| 122 | + throw new MalformedURLException("Gifyo user not found in " + url + ", expected http://gifyo.com/username"); |
| 123 | + } |
| 124 | + |
| 125 | +} |
0 commit comments