How can I search inside a youtube playlist?

前端 未结 3 1403
余生分开走
余生分开走 2021-02-04 15:45

I need to know if youtube API V3 supports searching inside a specific youtube playlist? And is there any other way to do that?

3条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-04 15:56

    I have read the YouTuBe API documentation regarding requests on playlists and they don't provide such functionality, however you could use this npm module to achieve that:

    npm install youtube-playlist-search
    

    or

    yarn install youtube-playlist-search
    

    The way to use it is something like this on javascript:

    var search = require('youtube-playlist-search');
    
    var params = {
      part: 'snippet,contentDetails',
      maxResults: 25,
      key: 'your_api_key'
    };
    
    search('searchTerm', params, function(err, videos) {
      if(err) return console.log(err);
      console.dir(videos);
    });
    

    Or you can also take a look here where I'm using it with React, the code it will be something like:

    import _ from 'lodash';
    import YTSearch from 'youtube-playlist-search';
    import React, { Component } from 'react';
    import logo from './logo.svg';
    import './App.css';
    import SearchBar from './components/search_bar';
    import VideoList from './components/video_list';
    import VideoDetail from './components/video_detail';
    
    class App extends Component {
      constructor(props) {
        super(props);
    
        this.state = {
          videos: [],
          selectedVideo: null
        };
    
        this.key = process.env.REACT_APP_YTB_API_KEY_DEV
        if (process.env.NODE_ENV === 'production') {
          this.key = process.env.REACT_APP_YTB_API_KEY_PROD
        }
    
        this.params = {
          part: 'snippet,contentDetails',
          playlistId: 'PLH99prTh-VPqO7ld0o2Sny6bLxpf80Js0',
          key: this.key
        };
    
        this.videoSearch('')
      }
    
      videoSearch(term) {
        YTSearch(term, this.params, (err, videos) => {
          this.setState({
            videos: videos,
            selectedVideo: videos[0]
          });
        });
      }
    
      render() {
        const videoSearch = _.debounce((term) => {this.videoSearch(term)}, 300);
        return (
          
    logo

    Welcome to React

    this.setState({selectedVideo})} videos={this.state.videos}/>
    ); } } export default App;

提交回复
热议问题