Skip to content

Instantly share code, notes, and snippets.

@DannyDelott
Created February 15, 2015 00:09
Show Gist options
  • Save DannyDelott/3508b74c3c11d065a4e4 to your computer and use it in GitHub Desktop.
Save DannyDelott/3508b74c3c11d065a4e4 to your computer and use it in GitHub Desktop.
Save .mp4 Vine videos present in the TweetBuffer
public class VineUtil {
public static String findVineUrl(Status status) {
String vineUrl;
for (URLEntity url: status.getURLEntities()) {
vineUrl = url.getExpandedURL();
if (vineUrl.contains(“vine.co / v / “)) {
return vineUrl;
}
}
return null;
}
public static String parseDownloadUrl(String html) {
if (html == null ||
(html.contains(“Page not found”)) ||
(html.contains(“<title>Vine</title>”))) {return null;}
try {
String[] split = html.split(“player: stream\” content = \””);
split = split[1].split(“\\ ? versionId”);
return split[0];
} catch (ArrayIndexOutOfBoundsException e1) {
return null;
}
}
public static boolean downloadVine(String saveDirectory, long id,
String downloadUrl) {
try {
File f = new File(saveDirectory + id + “.mp4”);
FileUtils.copyURLToFile(new URL(downloadUrl), f);
return true;
} catch (MalformedURLException e) {
return false;
} catch (IOException e) {
return false;
}
}
public static String sendGet(String u) throws IOException {
String url = u;
String USER_AGENT = “Mozilla / 5.0(Windows NT 6.3; Win64; x64) AppleWebKit / 537.36(KHTML, like Gecko) Chrome / 37.0.2049.0 Safari / 537.36”;
URL obj = new URL(url);
try {
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// handles the response code 301 redirect from vine URLs
String redirect = con.getHeaderField(“Location”);
if (redirect != null) {
obj = new URL(redirect);
con = (HttpURLConnection) obj.openConnection();
}
con.setRequestMethod(“GET”);
con.setRequestProperty(“User - Agent”, USER_AGENT);
int responseCode = con.getResponseCode();
if (responseCode > 200) {
return null;
}
BufferedReader in = new BufferedReader(
new InputStreamReader(
con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in .readLine()) != null) {
response.append(inputLine);
} in .close();
return response.toString();
} catch (Exception e) {
return null;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment