sealedtx / java-youtube-downloader

Simple, almost zero-dependency java parser for retrieving youtube video metadata
Other
413 stars 115 forks source link

Search feature #92

Closed angelkoh closed 2 years ago

angelkoh commented 3 years ago

Feature request: A simple search function to retrieve a list of video links/ videoIds (and maybe with their associated thumbnail/description).

zhangyinglong1993 commented 2 years ago

You want to be able to search for videos and music with unlimited frequency

Grodou commented 2 years ago

I'm working on the implementation of the search feature. @sealedtx I'd like your opinion on the design, especially on the naming, so here is the README part.

RequestSearchResult request = new RequestSearchResult("search query")
    .type(TypeField.VIDEO)                 // Videos only
    .match(FeatureField._3D,
        FeatureField.HD,
        FeatureField.SUBTITLES)            // 3D HD videos with subtitles
    .during(DurationField.OVER_20_MINUTES) // more than 20 minutes videos
    .uploadedThis(UploadDateField.MONTH)   // uploaded this month
    .sortBy(SortField.VIEW_COUNT);         // results sorted by view count
// or
RequestSearchResult request = new RequestSearchResult("search query")
    .select(
        TypeField.VIDEO,
        FeatureField.HD,
        (...)
        UploadDateField.MONTH)
    .sortBy(SortField.VIEW_COUNT);

SearchResult result = downloader.search(request).data();

// Retrieve next page (about 20 items per page)
if (result.hasNext()) {
    RequestSearchContinuation nextRequest = new RequestSearchContinuation(result);
    SearchResult nextResult = downloader.getNextPage(nextRequest).data();
}

// result details
System.out.println(result.estimatedResults());

// result items
List<SearchResultItem> items = result.items();
List<SearchResultVideoDetails> videos = result.videos();
List<SearchResultChannelDetails> channels = result.channels();
List<SearchResultPlaylistDetails> playlists = result.playlists();
List<SearchResultShelfDetails> shelves = result.shelves();

// item cast
SearchResultItem item = result.items().get(0);
if (item.isChannel()) {
    System.out.println(item.asChannel().author());
} else if (item.isShelf()) {
    for(SearchResultVideoDetails video : item.asShelf().videos()) {
        System.out.println(video.videoId());
    }
}

// Base 64 (use another base 64 encoder for search parameters)

// Classic JDK and Android API >= 26
Base64Encoder.Default.set(bytes -> Base64.getUrlEncoder().encodeToString(bytes));
// or
JdkBase64Encoder.setAsDefault();

// Android API < 26
Base64Encoder.Default.set(bytes -> Base64.encodeToString(bytes, Base64.URL_SAFE));
sealedtx commented 2 years ago

@Grodou wow, thank a lot for contribution!

Can you submit Pull request so it would be easier to review your code?