From 3411b5345044dbaf1eb355d6ba4e2a851773269c Mon Sep 17 00:00:00 2001 From: Adam Howard Date: Wed, 11 Nov 2015 01:48:44 +0000 Subject: [PATCH] implemented optimised version of YoutubeExtractor.getVideoId(). new version uses a regular expression instead of creating a HashMap and looping over them. Needs testing before pushing to origin --- .../java/org/schabi/newpipe/MediaFormat.java | 2 +- .../newpipe/VideoItemDetailActivity.java | 3 +-- .../newpipe/youtube/YoutubeExtractor.java | 22 ++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/MediaFormat.java b/app/src/main/java/org/schabi/newpipe/MediaFormat.java index fb2a508c5..6a37c3fd7 100644 --- a/app/src/main/java/org/schabi/newpipe/MediaFormat.java +++ b/app/src/main/java/org/schabi/newpipe/MediaFormat.java @@ -24,7 +24,7 @@ package org.schabi.newpipe; public enum MediaFormat { // id name suffix mime type MPEG_4 (0x0, "MPEG-4", "mp4", "video/mp4"), - v3GPP (0x1, "3GPP", "3gp", "video/3gpp"), + v3GPP (0x1, "3GPP", "3gp", "video/3gpp"), WEBM (0x2, "WebM", "webm", "video/webm"), M4A (0x3, "m4a", "m4a", "audio/mp4"), WEBMA (0x4, "WebM", "webm", "audio/webm"); diff --git a/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java b/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java index 370ac2fb2..34d491233 100644 --- a/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java +++ b/app/src/main/java/org/schabi/newpipe/VideoItemDetailActivity.java @@ -67,8 +67,7 @@ public class VideoItemDetailActivity extends AppCompatActivity { arguments.putInt(VideoItemDetailFragment.STREAMING_SERVICE, i); try { currentStreamingService = i; - extractor = ServiceList.getService(i) - .getExtractorInstance(); + extractor = ServiceList.getService(i).getExtractorInstance(); } catch (Exception e) { e.printStackTrace(); } diff --git a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java index d6b4bb835..a9cf3807a 100644 --- a/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java +++ b/app/src/main/java/org/schabi/newpipe/youtube/YoutubeExtractor.java @@ -130,6 +130,26 @@ public class YoutubeExtractor implements Extractor { } } + @Override + public String getVideoId(String videoUrl) { + //https://www.youtube.com/watch?v=laF2D3QyAFQ + String id; + Pattern pat; + if(videoUrl.contains("youtube")) { + pat = Pattern.compile("youtube\\.com/watch\\?v=([a-zA-Z0-9_]{11})"); + } + else if(videoUrl.contains("youtu.be")) { + pat = Pattern.compile("youtu\\.be/([a-zA-Z0-9_]{11})"); + } + else { + Log.e(TAG, "Error could not parse url: " + videoUrl); + return ""; + } + Matcher mat = pat.matcher(videoUrl); + id = mat.group(1); + return (id == null ? "" : id); + } +/* @Override public String getVideoId(String videoUrl) { try { @@ -165,7 +185,7 @@ public class YoutubeExtractor implements Extractor { } return null; } - +*/ @Override public String getVideoUrl(String videoId) { return "https://www.youtube.com/watch?v=" + videoId;