replaced 4-line regex blocks with call to equivalent method; removed unecessary try-catch blocks surrounding them: regex methods don't throw runtime exceptions

This commit is contained in:
Adam Howard 2015-11-03 12:56:11 +00:00
parent 7e193751c4
commit 3c72113f4c
4 changed files with 34 additions and 60 deletions

View file

@ -6,7 +6,7 @@
<option name="distributionType" value="LOCAL" /> <option name="distributionType" value="LOCAL" />
<option name="externalProjectPath" value="$PROJECT_DIR$" /> <option name="externalProjectPath" value="$PROJECT_DIR$" />
<option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.4" /> <option name="gradleHome" value="$APPLICATION_HOME_DIR$/gradle/gradle-2.4" />
<option name="gradleJvm" value="1.8" /> <option name="gradleJvm" value="1.7" />
<option name="modules"> <option name="modules">
<set> <set>
<option value="$PROJECT_DIR$" /> <option value="$PROJECT_DIR$" />

View file

@ -37,7 +37,7 @@
<ConfirmationsSetting value="0" id="Add" /> <ConfirmationsSetting value="0" id="Add" />
<ConfirmationsSetting value="0" id="Remove" /> <ConfirmationsSetting value="0" id="Remove" />
</component> </component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK"> <component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" default="true" assert-keyword="true" jdk-15="true" project-jdk-name="1.7" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" /> <output url="file://$PROJECT_DIR$/build/classes" />
</component> </component>
<component name="ProjectType"> <component name="ProjectType">

View file

@ -92,12 +92,12 @@
</content> </content>
<orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" /> <orderEntry type="jdk" jdkName="Android API 23 Platform" jdkType="Android SDK" />
<orderEntry type="sourceFolder" forTests="false" /> <orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" exported="" name="rhino-1.7.7" level="project" />
<orderEntry type="library" exported="" name="recyclerview-v7-23.1.0" level="project" /> <orderEntry type="library" exported="" name="recyclerview-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
<orderEntry type="library" exported="" name="jsoup-1.8.3" level="project" /> <orderEntry type="library" exported="" name="jsoup-1.8.3" level="project" />
<orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" /> <orderEntry type="library" exported="" name="support-v4-23.1.0" level="project" />
<orderEntry type="library" exported="" name="rhino-1.7.7" level="project" />
<orderEntry type="library" exported="" name="design-23.1.0" level="project" />
<orderEntry type="library" exported="" name="appcompat-v7-23.1.0" level="project" />
<orderEntry type="library" exported="" name="support-annotations-23.1.0" level="project" />
</component> </component>
</module> </module>

View file

@ -3,6 +3,7 @@ package org.schabi.newpipe.youtube;
import android.util.Log; import android.util.Log;
import android.util.Xml; import android.util.Xml;
import org.json.JSONException;
import org.json.JSONObject; import org.json.JSONObject;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
@ -141,14 +142,7 @@ public class YoutubeExtractor implements Extractor {
Document doc = Jsoup.parse(site, siteUrl); Document doc = Jsoup.parse(site, siteUrl);
try { videoInfo.id = matchGroup1("v=([0-9a-zA-Z]*)", siteUrl);
Pattern p = Pattern.compile("v=([0-9a-zA-Z]*)");
Matcher m = p.matcher(siteUrl);
m.find();
videoInfo.id = m.group(1);
} catch (Exception e) {
e.printStackTrace();
}
videoInfo.age_limit = 0; videoInfo.age_limit = 0;
videoInfo.webpage_url = siteUrl; videoInfo.webpage_url = siteUrl;
@ -160,16 +154,14 @@ public class YoutubeExtractor implements Extractor {
JSONObject ytAssets = null; JSONObject ytAssets = null;
String dashManifest; String dashManifest;
{ {
Pattern p = Pattern.compile("ytplayer.config\\s*=\\s*(\\{.*?\\});"); String jsonString = matchGroup1("ytplayer.config\\s*=\\s*(\\{.*?\\});", site);
Matcher m = p.matcher(site);
m.find();
try { try {
playerArgs = (new JSONObject(m.group(1))) JSONObject jsonObj = new JSONObject(jsonString);
.getJSONObject("args"); playerArgs = jsonObj.getJSONObject("args");
ytAssets = (new JSONObject(m.group(1))) ytAssets = jsonObj.getJSONObject("assets");
.getJSONObject("assets"); }
}catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
// If we fail in this part the video is most likely not available. // If we fail in this part the video is most likely not available.
// Determining why is done later. // Determining why is done later.
@ -263,15 +255,9 @@ public class YoutubeExtractor implements Extractor {
// upload date // upload date
videoInfo.upload_date = doc.select("strong[class=\"watch-time-text\"").first() videoInfo.upload_date = doc.select("strong[class=\"watch-time-text\"").first()
.text(); .text();
// Try to only use date not the text around it // Try to only use date not the text around it
try { videoInfo.upload_date = matchGroup1("([0-9.]*$)", videoInfo.upload_date);
Pattern p = Pattern.compile("([0-9.]*$)");
Matcher m = p.matcher(videoInfo.upload_date);
m.find();
videoInfo.upload_date = m.group(1);
} catch (Exception e) {
e.printStackTrace();
}
// description // description
videoInfo.description = doc.select("p[id=\"eow-description\"]").first() videoInfo.description = doc.select("p[id=\"eow-description\"]").first()
@ -324,14 +310,10 @@ public class YoutubeExtractor implements Extractor {
if(!dashManifest.contains("/signature/")) { if(!dashManifest.contains("/signature/")) {
String encryptedSig = ""; String encryptedSig = "";
String decryptedSig; String decryptedSig;
try {
Pattern p = Pattern.compile("/s/([a-fA-F0-9\\.]+)"); //regex methods do not throw runtime exceptions, if their arguments are valid
Matcher m = p.matcher(dashManifest); encryptedSig = matchGroup1("/s/([a-fA-F0-9\\.]+)", dashManifest);
m.find();
encryptedSig = m.group(1);
} catch (Exception e) {
e.printStackTrace();
}
decryptedSig = decryptSignature(encryptedSig, decryptoinCode); decryptedSig = decryptSignature(encryptedSig, decryptoinCode);
dashManifest = dashManifest.replace("/s/" + encryptedSig, "/signature/" + decryptedSig); dashManifest = dashManifest.replace("/s/" + encryptedSig, "/signature/" + decryptedSig);
} }
@ -396,10 +378,7 @@ public class YoutubeExtractor implements Extractor {
info.webpage_url = li.select("a[class*=\"content-link\"]").first() info.webpage_url = li.select("a[class*=\"content-link\"]").first()
.attr("abs:href"); .attr("abs:href");
try { try {
Pattern p = Pattern.compile("v=([0-9a-zA-Z-]*)"); info.id = matchGroup1("v=([0-9a-zA-Z-]*)", info.webpage_url);
Matcher m = p.matcher(info.webpage_url);
m.find();
info.id=m.group(1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
@ -446,27 +425,15 @@ public class YoutubeExtractor implements Extractor {
String decryptionCode; String decryptionCode;
try { try {
Pattern p = Pattern.compile("\\.sig\\|\\|([a-zA-Z0-9$]+)\\("); decryptionFuncName = matchGroup1("\\.sig\\|\\|([a-zA-Z0-9$]+)\\(", playerCode);
Matcher m = p.matcher(playerCode);
m.find();
decryptionFuncName = m.group(1);
String functionPattern = "(function " + decryptionFuncName.replace("$", "\\$") + "\\([a-zA-Z0-9_]*\\)\\{.+?\\})"; String functionPattern = "(function " + decryptionFuncName.replace("$", "\\$") + "\\([a-zA-Z0-9_]*\\)\\{.+?\\})";
p = Pattern.compile(functionPattern); decryptionFunc = matchGroup1(functionPattern, playerCode);
m = p.matcher(playerCode);
m.find();
decryptionFunc = m.group(1);
p = Pattern.compile(";([A-Za-z0-9_\\$]{2})\\...\\("); helperObjectName = matchGroup1(";([A-Za-z0-9_\\$]{2})\\...\\(", decryptionFunc);
m = p.matcher(decryptionFunc);
m.find();
helperObjectName = m.group(1);
String helperPattern = "(var " + helperObjectName.replace("$", "\\$") + "=\\{.+?\\}\\};)function"; String helperPattern = "(var " + helperObjectName.replace("$", "\\$") + "=\\{.+?\\}\\};)function";
p = Pattern.compile(helperPattern); helperObject = matchGroup1(helperPattern, playerCode);
m = p.matcher(playerCode);
m.find();
helperObject = m.group(1);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -493,4 +460,11 @@ public class YoutubeExtractor implements Extractor {
Context.exit(); Context.exit();
return result.toString(); return result.toString();
} }
private String matchGroup1(String pattern, String input) {
Pattern pat = Pattern.compile(pattern);
Matcher mat = pat.matcher(input);
mat.find();
return mat.group(1);
}
} }