Skip to content

Commit 3fe55b3

Browse files
committed
Add support for Google search redirect url
1 parent c190a30 commit 3fe55b3

6 files changed

Lines changed: 65 additions & 6 deletions

File tree

extractor/src/main/java/org/schabi/newpipe/extractor/StreamingService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import org.schabi.newpipe.extractor.stream.StreamExtractor;
1717
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
1818
import org.schabi.newpipe.extractor.suggestion.SuggestionExtractor;
19+
import org.schabi.newpipe.extractor.utils.Utils;
1920

2021
import javax.annotation.Nullable;
2122
import java.util.Collections;
@@ -277,12 +278,13 @@ public CommentsExtractor getCommentsExtractor(String url) throws ExtractionExcep
277278
* Figures out where the link is pointing to (a channel, a video, a playlist, etc.)
278279
* @param url the url on which it should be decided of which link type it is
279280
* @return the link type of url
280-
* @throws ParsingException
281281
*/
282282
public final LinkType getLinkTypeByUrl(String url) throws ParsingException {
283-
LinkHandlerFactory sH = getStreamLHFactory();
284-
LinkHandlerFactory cH = getChannelLHFactory();
285-
LinkHandlerFactory pH = getPlaylistLHFactory();
283+
url = Utils.followGoogleRedirectIfNeeded(url);
284+
285+
final LinkHandlerFactory sH = getStreamLHFactory();
286+
final LinkHandlerFactory cH = getChannelLHFactory();
287+
final LinkHandlerFactory pH = getPlaylistLHFactory();
286288

287289
if (sH != null && sH.acceptUrl(url)) {
288290
return LinkType.STREAM;

extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/LinkHandlerFactory.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,30 @@ public String getUrl(String id, String baseUrl) throws ParsingException {
4242
// Logic
4343
///////////////////////////////////
4444

45+
/**
46+
* Builds a {@link LinkHandler} from a url.<br>
47+
* Be sure to call {@link Utils#followGoogleRedirectIfNeeded(String)} on the url if overriding
48+
* this function.
49+
* @param url the url to extract path and id from
50+
* @return a {@link LinkHandler} complete with information
51+
*/
4552
public LinkHandler fromUrl(String url) throws ParsingException {
4653
if (url == null) throw new IllegalArgumentException("url can not be null");
54+
url = Utils.followGoogleRedirectIfNeeded(url);
4755
final String baseUrl = Utils.getBaseUrl(url);
4856
return fromUrl(url, baseUrl);
4957
}
5058

59+
/**
60+
* Builds a {@link LinkHandler} from a url and a base url. The url is expected to be already
61+
* polished from google search redirects (otherwise how could {@code baseUrl} have been
62+
* extracted?).<br>
63+
* So do not call {@link Utils#followGoogleRedirectIfNeeded(String)} on the url if overriding
64+
* this function, since that should be done in {@link #fromUrl(String)}.
65+
* @param url the url without google search redirects to extract id from
66+
* @param baseUrl the base url
67+
* @return a {@link LinkHandler} complete with information
68+
*/
5169
public LinkHandler fromUrl(String url, String baseUrl) throws ParsingException {
5270
if (url == null) throw new IllegalArgumentException("url can not be null");
5371
if (!acceptUrl(url)) {

extractor/src/main/java/org/schabi/newpipe/extractor/linkhandler/ListLinkHandlerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ public String getUrl(String id, List<String> contentFilter, String sortFilter, S
3232

3333
@Override
3434
public ListLinkHandler fromUrl(String url) throws ParsingException {
35-
String baseUrl = Utils.getBaseUrl(url);
35+
url = Utils.followGoogleRedirectIfNeeded(url);
36+
final String baseUrl = Utils.getBaseUrl(url);
3637
return fromUrl(url, baseUrl);
3738
}
3839

extractor/src/main/java/org/schabi/newpipe/extractor/utils/Utils.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,26 @@ public static String getBaseUrl(String url) throws ParsingException {
196196
}
197197
}
198198

199+
/**
200+
* If the provided url is a Google search redirect, then the actual url is extracted from the
201+
* {@code url=} query value and returned, otherwise the original url is returned.
202+
* @param url the url which can possibly be a Google search redirect
203+
* @return an url with no Google search redirects
204+
*/
205+
public static String followGoogleRedirectIfNeeded(final String url) {
206+
// if the url is a redirect from a Google search, extract the actual url
207+
try {
208+
final URL decoded = Utils.stringToURL(url);
209+
if (decoded.getHost().contains("google") && decoded.getPath().equals("/url")) {
210+
return URLDecoder.decode(Parser.matchGroup1("&url=([^&]+)(?:&|$)", url), "UTF-8");
211+
}
212+
} catch (final Exception ignored) {
213+
}
214+
215+
// url is not a google search redirect
216+
return url;
217+
}
218+
199219
public static boolean isNullOrEmpty(final String str) {
200220
return str == null || str.isEmpty();
201221
}

extractor/src/test/java/org/schabi/newpipe/extractor/NewPipeTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import static org.junit.Assert.*;
88
import static org.schabi.newpipe.extractor.NewPipe.getServiceByUrl;
9+
import static org.schabi.newpipe.extractor.ServiceList.SoundCloud;
910
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
1011

1112
public class NewPipeTest {
@@ -39,8 +40,10 @@ public void getServiceWithUrl() throws Exception {
3940
assertEquals(getServiceByUrl("https://www.youtube.com/watch?v=_r6CgaFNAGg"), YouTube);
4041
assertEquals(getServiceByUrl("https://www.youtube.com/channel/UCi2bIyFtz-JdI-ou8kaqsqg"), YouTube);
4142
assertEquals(getServiceByUrl("https://www.youtube.com/playlist?list=PLRqwX-V7Uu6ZiZxtDDRCi6uhfTH4FilpH"), YouTube);
43+
assertEquals(getServiceByUrl("https://www.google.it/url?sa=t&rct=j&q=&esrc=s&cd=&cad=rja&uact=8&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHu80uDzh8RY&source=video"), YouTube);
4244

43-
assertNotEquals(getServiceByUrl("https://soundcloud.com/pegboardnerds"), YouTube);
45+
assertEquals(getServiceByUrl("https://soundcloud.com/pegboardnerds"), SoundCloud);
46+
assertEquals(getServiceByUrl("https://www.google.com/url?sa=t&url=https%3A%2F%2Fsoundcloud.com%2Fciaoproduction&rct=j&q=&esrc=s&source=web&cd="), SoundCloud);
4447
}
4548

4649
@Test

extractor/src/test/java/org/schabi/newpipe/extractor/utils/UtilsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,19 @@ public void testGetBaseUrl() throws ParsingException {
3030
assertEquals("vnd.youtube", Utils.getBaseUrl("vnd.youtube://n8X9_MgEdCg"));
3131
assertEquals("https://music.youtube.com", Utils.getBaseUrl("https://music.youtube.com/watch?v=O0EDx9WAelc"));
3232
}
33+
34+
@Test
35+
public void testFollowGoogleRedirect() {
36+
assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY",
37+
Utils.followGoogleRedirectIfNeeded("https://www.google.it/url?sa=t&rct=j&q=&esrc=s&cd=&cad=rja&uact=8&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DHu80uDzh8RY&source=video"));
38+
assertEquals("https://www.youtube.com/watch?v=0b6cFWG45kA",
39+
Utils.followGoogleRedirectIfNeeded("https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=video&cd=&cad=rja&uact=8&url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D0b6cFWG45kA"));
40+
assertEquals("https://soundcloud.com/ciaoproduction",
41+
Utils.followGoogleRedirectIfNeeded("https://www.google.com/url?sa=t&url=https%3A%2F%2Fsoundcloud.com%2Fciaoproduction&rct=j&q=&esrc=s&source=web&cd="));
42+
43+
assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY&param=xyz",
44+
Utils.followGoogleRedirectIfNeeded("https://www.youtube.com/watch?v=Hu80uDzh8RY&param=xyz"));
45+
assertEquals("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello",
46+
Utils.followGoogleRedirectIfNeeded("https://www.youtube.com/watch?v=Hu80uDzh8RY&url=hello"));
47+
}
3348
}

0 commit comments

Comments
 (0)