Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
675 views
in Technique[技术] by (71.8m points)

javascript - How to get video ids from an YouTube playlist?

"googleapis": "^16.1.0"

I have a playlist where there are two videos. How can I get the videos ids?

I tried this:

// Node.js
const google = require('googleapis');
const youtube = google.youtube('v3');
const secrets = require('./secrets.json');

const results = youtube.playlists.list({
  auth: secrets.web.api_key,
  part: 'id',
  id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm'
});

console.log(results);

Upon the code execution, I receive this: https://gist.github.com/SergeyBondarenko/ea6a2aad546ded32e4a9b3cf53228fef

And there is only the playlist id:

// Node.js
> results.responseContent.body.items
[ { kind: 'youtube#playlist',
    etag: '"gMxXHe-zinKdE9lTnzKu8vjcmDI/cYPhPXIoWu4acW3Qux1D5WZ3WwE"',
    id: 'PLvxLmGsmqdZc-GYVeLhS0N_6jfrzEleQm' } ]

I don't have items property inside the results object:

// Node.js
> request.i
request.isPrototypeOf  
request.init   

And as far as I understand the items property must contain the results, like in the following example:

 // Python
 results = youtube.playlists().list(
    part="snippet,localizations",
    id=playlist_id
  ).execute()

  playlist = results["items"][0]

https://developers.google.com/youtube/v3/docs/playlists/list

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

With Axios you can do something like that :

import axios from "axios";
const KEY = "";

const getPlayListItems = async playlistID => {
    const result = await axios.get(`https://www.googleapis.com/youtube/v3/playlistItems`, {
      params: {
        part: 'id,snippet',
        maxResults: 10,
        playlistId: playlistID
        key: KEY
      }
    });
    return result.data;
  };

  getPlayListItems("PlaylistID").then(data => {
    data.items.forEach(element => {
        console.log(element.snippet.resourceId.videoId)
});

This will print all videoId of a given playlist, up to position 10.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...