Skip to content

Commit

Permalink
Merge branch 'vget-1.0.34'
Browse files Browse the repository at this point in the history
  • Loading branch information
axet committed Dec 9, 2012
2 parents 7db7194 + b8d66eb commit c40aa15
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 56 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.axet</groupId>
<artifactId>vget</artifactId>
<packaging>jar</packaging>
<version>1.0.33</version>
<version>1.0.34</version>
<name>vget</name>
<description>vget java video download library</description>
<url>https://github.com/axet/vget</url>
Expand Down
56 changes: 50 additions & 6 deletions src/main/java/com/github/axet/vget/VGet.java
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ void target(DownloadInfo dinfo) {
boolean retry(Throwable e) {
if (e == null)
return true;

if (e instanceof DownloadIOCodeError) {
DownloadIOCodeError c = (DownloadIOCodeError) e;
switch (c.getCode()) {
Expand All @@ -215,19 +216,62 @@ boolean retry(Throwable e) {
return false;
}
}

return false;
}

/**
* return status of download information. subclassing for VideoInfo.empty();
*
* @return
*/
public boolean empty() {
return getVideo().empty();
}

/**
* extract video information, retry until success
*
* @param stop
* @param notify
*/
public void extract(AtomicBoolean stop, Runnable notify) {
while (!done(stop)) {
try {
if (info.empty()) {
info.extract(stop, notify);
info.setState(States.EXTRACTING_DONE);
notify.run();
}
return;
} catch (DownloadRetry e) {
retry(stop, notify, e);
} catch (DownloadMultipartError e) {
for (Part ee : e.getInfo().getParts()) {
if (!retry(ee.getException())) {
throw e;
}
}
retry(stop, notify, e);
} catch (DownloadIOCodeError e) {
if (retry(e))
retry(stop, notify, e);
else
throw e;
} catch (DownloadIOError e) {
retry(stop, notify, e);
}
}
}

public void download(final AtomicBoolean stop, final Runnable notify) {
try {
if (empty()) {
extract(stop, notify);
}

while (!done(stop)) {
try {
if (info.empty()) {
info.extract(stop, notify);
info.setState(States.EXTRACTING_DONE);
notify.run();
}

final DownloadInfo dinfo = info.getInfo();

if (dinfo.getContentType() == null || !dinfo.getContentType().contains("video/")) {
Expand Down
114 changes: 65 additions & 49 deletions src/main/java/com/github/axet/vget/info/YouTubeParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@

public class YouTubeParser extends VGetParser {

public static class VideoUnavailablePlayer extends DownloadError {
/**
*
*/
private static final long serialVersionUID = 10905065542230199L;

public VideoUnavailablePlayer() {
super("unavailable-player");
}
}

public static class AgeException extends DownloadError {
private static final long serialVersionUID = 1L;

Expand Down Expand Up @@ -281,71 +292,76 @@ public static Map<String, String> getQueryMap(String qs) {
}

void extractHtmlInfo(VideoInfo info, String html) throws Exception {
Pattern age = Pattern.compile("(verify_age)");
Matcher ageMatch = age.matcher(html);
if (ageMatch.find())
throw new AgeException();

Pattern gen = Pattern.compile("\"(http(.*)generate_204(.*))\"");
Matcher genMatch = gen.matcher(html);
if (genMatch.find()) {
String sline = genMatch.group(1);

sline = StringEscapeUtils.unescapeJava(sline);
{
Pattern age = Pattern.compile("(verify_age)");
Matcher ageMatch = age.matcher(html);
if (ageMatch.find())
throw new AgeException();
}

Pattern urlencod = Pattern.compile("\"url_encoded_fmt_stream_map\": \"([^\"]*)\"");
Matcher urlencodMatch = urlencod.matcher(html);
if (urlencodMatch.find()) {
String url_encoded_fmt_stream_map;
url_encoded_fmt_stream_map = urlencodMatch.group(1);

// normal embedded video, unable to grab age restricted videos
Pattern encod = Pattern.compile("url=(.*)");
Matcher encodMatch = encod.matcher(url_encoded_fmt_stream_map);
if (encodMatch.find()) {
String sline = encodMatch.group(1);
{
Pattern age = Pattern.compile("(unavailable-player)");
Matcher ageMatch = age.matcher(html);
if (ageMatch.find())
throw new VideoUnavailablePlayer();
}

extractUrlEncodedVideos(sline);
}
{
Pattern urlencod = Pattern.compile("\"url_encoded_fmt_stream_map\": \"([^\"]*)\"");
Matcher urlencodMatch = urlencod.matcher(html);
if (urlencodMatch.find()) {
String url_encoded_fmt_stream_map;
url_encoded_fmt_stream_map = urlencodMatch.group(1);

// normal embedded video, unable to grab age restricted videos
Pattern encod = Pattern.compile("url=(.*)");
Matcher encodMatch = encod.matcher(url_encoded_fmt_stream_map);
if (encodMatch.find()) {
String sline = encodMatch.group(1);

extractUrlEncodedVideos(sline);
}

// stream video
Pattern encodStream = Pattern.compile("stream=(.*)");
Matcher encodStreamMatch = encodStream.matcher(url_encoded_fmt_stream_map);
if (encodStreamMatch.find()) {
String sline = encodStreamMatch.group(1);
// stream video
Pattern encodStream = Pattern.compile("stream=(.*)");
Matcher encodStreamMatch = encodStream.matcher(url_encoded_fmt_stream_map);
if (encodStreamMatch.find()) {
String sline = encodStreamMatch.group(1);

String[] urlStrings = sline.split("stream=");
String[] urlStrings = sline.split("stream=");

for (String urlString : urlStrings) {
urlString = StringEscapeUtils.unescapeJava(urlString);
for (String urlString : urlStrings) {
urlString = StringEscapeUtils.unescapeJava(urlString);

Pattern link = Pattern.compile("(sparams.*)&itag=(\\d+)&.*&conn=rtmpe(.*),");
Matcher linkMatch = link.matcher(urlString);
if (linkMatch.find()) {
Pattern link = Pattern.compile("(sparams.*)&itag=(\\d+)&.*&conn=rtmpe(.*),");
Matcher linkMatch = link.matcher(urlString);
if (linkMatch.find()) {

String sparams = linkMatch.group(1);
String itag = linkMatch.group(2);
String url = linkMatch.group(3);
String sparams = linkMatch.group(1);
String itag = linkMatch.group(2);
String url = linkMatch.group(3);

url = "http" + url + "?" + sparams;
url = "http" + url + "?" + sparams;

url = URLDecoder.decode(url, "UTF-8");
url = URLDecoder.decode(url, "UTF-8");

addVideo(Integer.decode(itag), url);
addVideo(Integer.decode(itag), url);
}
}
}
}
}

Pattern title = Pattern.compile("<meta name=\"title\" content=(.*)");
Matcher titleMatch = title.matcher(html);
if (titleMatch.find()) {
String sline = titleMatch.group(1);
String name = sline.replaceFirst("<meta name=\"title\" content=", "").trim();
name = StringUtils.strip(name, "\">");
name = StringEscapeUtils.unescapeHtml4(name);
info.setTitle(name);
{
Pattern title = Pattern.compile("<meta name=\"title\" content=(.*)");
Matcher titleMatch = title.matcher(html);
if (titleMatch.find()) {
String sline = titleMatch.group(1);
String name = sline.replaceFirst("<meta name=\"title\" content=", "").trim();
name = StringUtils.strip(name, "\">");
name = StringEscapeUtils.unescapeHtml4(name);
info.setTitle(name);
}
}
}

Expand Down

0 comments on commit c40aa15

Please sign in to comment.