-
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.
- Loading branch information
Showing
1 changed file
with
98 additions
and
0 deletions.
There are no files selected for viewing
98 changes: 98 additions & 0 deletions
98
src/main/java/com/rarchives/ripme/ripper/rippers/VineRipper.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,98 @@ | ||
package com.rarchives.ripme.ripper.rippers; | ||
|
||
import java.io.IOException; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.log4j.Logger; | ||
import org.json.JSONArray; | ||
import org.json.JSONObject; | ||
import org.jsoup.HttpStatusException; | ||
import org.jsoup.Jsoup; | ||
import org.jsoup.nodes.Document; | ||
|
||
import com.rarchives.ripme.ripper.AlbumRipper; | ||
import com.rarchives.ripme.ui.RipStatusMessage.STATUS; | ||
|
||
public class VineRipper extends AlbumRipper { | ||
|
||
private static final String DOMAIN = "vine.co", | ||
HOST = "vine"; | ||
private static final Logger logger = Logger.getLogger(VineRipper.class); | ||
|
||
public VineRipper(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 { | ||
return new URL("http://vine.co/u/" + getGID(url)); | ||
} | ||
|
||
@Override | ||
public void rip() throws IOException { | ||
int page = 0; | ||
String baseURL = "https://vine.co/api/timelines/users/" + getGID(this.url); | ||
Document doc; | ||
while (true) { | ||
page++; | ||
String theURL = baseURL; | ||
if (page > 1) { | ||
theURL += "?page=" + page; | ||
} | ||
try { | ||
logger.info(" Retrieving " + theURL); | ||
sendUpdate(STATUS.LOADING_RESOURCE, theURL); | ||
doc = Jsoup.connect(theURL) | ||
.ignoreContentType(true) | ||
.timeout(5 * 1000) | ||
.get(); | ||
} catch (HttpStatusException e) { | ||
logger.debug("Hit end of pages at page " + page, e); | ||
break; | ||
} | ||
String jsonString = doc.body().html(); | ||
jsonString = jsonString.replace(""", "\""); | ||
JSONObject json = new JSONObject(jsonString); | ||
JSONArray records = json.getJSONObject("data").getJSONArray("records"); | ||
for (int i = 0; i < records.length(); i++) { | ||
String videoURL = records.getJSONObject(i).getString("videoUrl"); | ||
addURLToDownload(new URL(videoURL)); | ||
} | ||
if (records.length() == 0) { | ||
logger.info("Zero records returned"); | ||
break; | ||
} | ||
try { | ||
Thread.sleep(2000); | ||
} catch (InterruptedException e) { | ||
logger.error("[!] Interrupted while waiting to load next page", e); | ||
break; | ||
} | ||
} | ||
waitForThreads(); | ||
} | ||
|
||
@Override | ||
public String getHost() { | ||
return HOST; | ||
} | ||
|
||
@Override | ||
public String getGID(URL url) throws MalformedURLException { | ||
Pattern p = Pattern.compile("^https?://(www\\.)?vine\\.co/u/([0-9]{1,}).*$"); | ||
Matcher m = p.matcher(url.toExternalForm()); | ||
if (!m.matches()) { | ||
throw new MalformedURLException("Expected format: http://vine.co/u/######"); | ||
} | ||
return m.group(m.groupCount()); | ||
} | ||
|
||
} |