Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add search link type in the extractor #950

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ public enum LinkType {
NONE,
STREAM,
CHANNEL,
PLAYLIST
PLAYLIST,
SEARCH,
}

private final int serviceId;
Expand Down Expand Up @@ -140,6 +141,13 @@ public String toString() {
*/
public abstract ListLinkHandlerFactory getChannelLHFactory();

/**
* Must return a new instance of an implementation of ListLinkHandlerFactory for channels.
* If support for channels is not given null must be returned.
* @return an instance of a ListLinkHandlerFactory for search link or null
*/
public abstract ListLinkHandlerFactory getSearchLHFactory();

/**
* Must return a new instance of an implementation of ListLinkHandlerFactory for playlists.
* If support for playlists is not given null must be returned.
Expand Down Expand Up @@ -282,6 +290,27 @@ public CommentsExtractor getCommentsExtractor(final String url) throws Extractio
// Utils
//////////////////////////////////////////////////////////////////////////*/

public final String getIdByUrl(final String url) throws ParsingException{
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of adding this function just to use it for search URLs in the client, I think a better option is what I suggested in the client review

final String polishedUrl = Utils.followGoogleRedirectIfNeeded(url);

final LinkHandlerFactory sH = getStreamLHFactory();
final LinkHandlerFactory cH = getChannelLHFactory();
final LinkHandlerFactory pH = getPlaylistLHFactory();
final LinkHandlerFactory searchH = getSearchLHFactory();

if (sH != null && sH.acceptUrl(polishedUrl)) {
return sH.getId(url);
} else if (searchH != null && searchH.acceptUrl(polishedUrl)) {
return searchH.getId(url);
} else if (cH != null && cH.acceptUrl(polishedUrl)) {
return cH.getId(url);
} else if (pH != null && pH.acceptUrl(polishedUrl)) {
return pH.getId(url);
} else {
return "";
}
}

/**
* Figures out where the link is pointing to (a channel, a video, a playlist, etc.)
* @param url the url on which it should be decided of which link type it is
Expand All @@ -293,9 +322,12 @@ public final LinkType getLinkTypeByUrl(final String url) throws ParsingException
final LinkHandlerFactory sH = getStreamLHFactory();
final LinkHandlerFactory cH = getChannelLHFactory();
final LinkHandlerFactory pH = getPlaylistLHFactory();
final LinkHandlerFactory searchH = getSearchLHFactory();

if (sH != null && sH.acceptUrl(polishedUrl)) {
return LinkType.STREAM;
} else if (searchH != null && searchH.acceptUrl(polishedUrl)) {
return LinkType.SEARCH;
} else if (cH != null && cH.acceptUrl(polishedUrl)) {
return LinkType.CHANNEL;
} else if (pH != null && pH.acceptUrl(polishedUrl)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public ListLinkHandlerFactory getChannelLHFactory() {
return new BandcampChannelLinkHandlerFactory();
}

@Override
public ListLinkHandlerFactory getSearchLHFactory() {
return null;
}

@Override
public ListLinkHandlerFactory getPlaylistLHFactory() {
return new BandcampPlaylistLinkHandlerFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ public ListLinkHandlerFactory getChannelLHFactory() {
return new MediaCCCConferenceLinkHandlerFactory();
}

@Override
public ListLinkHandlerFactory getSearchLHFactory() {
return null;
}

@Override
public ListLinkHandlerFactory getPlaylistLHFactory() {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ public ListLinkHandlerFactory getChannelLHFactory() {
return PeertubeChannelLinkHandlerFactory.getInstance();
}

@Override
public ListLinkHandlerFactory getSearchLHFactory() {
return null;
}

@Override
public ListLinkHandlerFactory getPlaylistLHFactory() {
return PeertubePlaylistLinkHandlerFactory.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public ListLinkHandlerFactory getChannelLHFactory() {
return SoundcloudChannelLinkHandlerFactory.getInstance();
}

@Override
public ListLinkHandlerFactory getSearchLHFactory() {
return null;
}

@Override
public ListLinkHandlerFactory getPlaylistLHFactory() {
return SoundcloudPlaylistLinkHandlerFactory.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeChannelLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeCommentsLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubePlaylistLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeTrendingLinkHandlerFactory;
Expand Down Expand Up @@ -88,6 +89,11 @@ public ListLinkHandlerFactory getChannelLHFactory() {
return YoutubeChannelLinkHandlerFactory.getInstance();
}

@Override
public ListLinkHandlerFactory getSearchLHFactory() {
return YoutubeSearchLinkHandlerFactory.getInstance();
}

@Override
public ListLinkHandlerFactory getPlaylistLHFactory() {
return YoutubePlaylistLinkHandlerFactory.getInstance();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package org.schabi.newpipe.extractor.services.youtube.linkHandler;

import org.schabi.newpipe.extractor.exceptions.ParsingException;
import org.schabi.newpipe.extractor.linkhandler.LinkHandler;
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
import org.schabi.newpipe.extractor.services.youtube.YoutubeParsingHelper;
import org.schabi.newpipe.extractor.utils.Utils;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

public class YoutubeSearchLinkHandlerFactory extends ListLinkHandlerFactory {
private static final YoutubeSearchLinkHandlerFactory INSTANCE
= new YoutubeSearchLinkHandlerFactory();

private YoutubeSearchLinkHandlerFactory() {}

public static YoutubeSearchLinkHandlerFactory getInstance() {
return INSTANCE;
}

@Override
public String getUrl(final String id, final List<String> contentFilters,
final String sortFilter) {
return "https://www.youtube.com/results?search_query=" + id;
}

@Override
public String getId(String url) throws ParsingException {
try {
final URL urlObj = Utils.stringToURL(url);

if (!Utils.isHTTP(urlObj) || !(YoutubeParsingHelper.isYoutubeURL(urlObj)
|| YoutubeParsingHelper.isInvidioURL(urlObj))) {
throw new ParsingException("the url given is not a YouTube-URL");
}

final String listID = Utils.getQueryValue(urlObj, "search_query");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change listId to searchQuery and update the exception message accordingly


if (listID == null) {
throw new ParsingException("the URL given does not include a playlist");
}

return listID;

} catch (final Exception exception) {
throw new ParsingException("Error could not parse URL: " + exception.getMessage(),
exception);
}
}

@Override
public boolean onAcceptUrl(String url) throws ParsingException {
try {
getId(url);
} catch (final ParsingException e) {
return false;
}
return true;
}
}