Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/ram-on/extractor into fix
Browse files Browse the repository at this point in the history
  • Loading branch information
theScrabi committed Jun 11, 2017
2 parents 9767b4a + 306f836 commit ab53038
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 14 deletions.
25 changes: 19 additions & 6 deletions services/youtube/YoutubeStreamExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ private JSONObject getPlayerConfig(String pageContent) throws ParsingException {
Parser.matchGroup1("ytplayer.config\\s*=\\s*(\\{.*?\\});", pageContent);
return new JSONObject(ytPlayerConfigRaw);
} catch (Parser.RegexException e) {
String errorReason = findErrorReason(doc);
String errorReason = getErrorMessage();
switch(errorReason) {
case "GEMA":
throw new GemaException(errorReason);
Expand Down Expand Up @@ -948,15 +948,28 @@ private String decryptSignature(String encryptedSig, String decryptionCode)
return result == null ? "" : result.toString();
}

private String findErrorReason(Document doc) {
String errorMessage = doc.select("h1[id=\"unavailable-message\"]").first().text();
if(errorMessage.contains("GEMA")) {

/**
* {@inheritDoc}
*/
public String getErrorMessage() {
String errorMessage = doc.select("h1[id=\"unavailable-message\"]").first().text();
StringBuilder errorReason;

if (errorMessage == null || errorMessage.isEmpty()) {
errorReason = null;
} else if(errorMessage.contains("GEMA")) {
// Gema sometimes blocks youtube music content in germany:
// https://www.gema.de/en/
// Detailed description:
// https://en.wikipedia.org/wiki/GEMA_%28German_organization%29
return "GEMA";
errorReason = new StringBuilder("GEMA");
} else {
errorReason = new StringBuilder(errorMessage);
errorReason.append(" ");
errorReason.append(doc.select("[id=\"unavailable-submessage\"]").first().text());
}
return "";

return errorReason != null ? errorReason.toString() : null;
}
}
9 changes: 8 additions & 1 deletion stream_info/StreamExtractor.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public ExtractorInitException(String message, Throwable cause) {
}
}

public class ContentNotAvailableException extends ParsingException {
public static class ContentNotAvailableException extends ParsingException {
public ContentNotAvailableException(String message) {
super(message);
}
Expand Down Expand Up @@ -101,4 +101,11 @@ public UrlIdHandler getUrlIdHandler() {
public int getServiceId() {
return serviceId;
}

/**
* Analyses the webpage's document and extracts any error message there might be.
*
* @return Error message; null if there is no error message.
*/
public abstract String getErrorMessage();
}
29 changes: 22 additions & 7 deletions stream_info/StreamInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,34 @@ public void addException(Exception e) {
/**Fills out the video info fields which are common to all services.
* Probably needs to be overridden by subclasses*/
public static StreamInfo getVideoInfo(StreamExtractor extractor)
throws ExtractionException, IOException {
throws ExtractionException, StreamExtractor.ContentNotAvailableException {
StreamInfo streamInfo = new StreamInfo();

streamInfo = extractImportantData(streamInfo, extractor);
streamInfo = extractStreams(streamInfo, extractor);
streamInfo = extractOptionalData(streamInfo, extractor);
try {
streamInfo = extractImportantData(streamInfo, extractor);
streamInfo = extractStreams(streamInfo, extractor);
streamInfo = extractOptionalData(streamInfo, extractor);
} catch (ExtractionException e) {
// Currently YouTube does not distinguish between age restricted videos and videos blocked
// by country. This means that during the initialisation of the extractor, the extractor
// will assume that a video is age restricted while in reality it it blocked by country.
//
// We will now detect whether the video is blocked by country or not.
String errorMsg = extractor.getErrorMessage();

if (errorMsg != null) {
throw new StreamExtractor.ContentNotAvailableException(errorMsg);
} else {
throw e;
}
}

return streamInfo;
}

private static StreamInfo extractImportantData(
StreamInfo streamInfo, StreamExtractor extractor)
throws ExtractionException, IOException {
throws ExtractionException {
/* ---- important data, withoug the video can't be displayed goes here: ---- */
// if one of these is not available an exception is meant to be thrown directly into the frontend.

Expand All @@ -104,15 +119,15 @@ private static StreamInfo extractImportantData(
|| (streamInfo.id == null || streamInfo.id.isEmpty())
|| (streamInfo.title == null /* streamInfo.title can be empty of course */)
|| (streamInfo.age_limit == -1)) {
throw new ExtractionException("Some importand stream information was not given.");
throw new ExtractionException("Some important stream information was not given.");
}

return streamInfo;
}

private static StreamInfo extractStreams(
StreamInfo streamInfo, StreamExtractor extractor)
throws ExtractionException, IOException {
throws ExtractionException {
/* ---- stream extraction goes here ---- */
// At least one type of stream has to be available,
// otherwise an exception will be thrown directly into the frontend.
Expand Down

0 comments on commit ab53038

Please sign in to comment.