node.js Get.Request&Pagination&Async

问题描述 投票:0回答:1

我在这里组织流程非常艰难,因为我是自学成才,所以想知道是否有人可以提供帮助。

var channelIds = ['XYZ','ABC','QRS']
var playlistIds = [];
var videoIds = [];

程序顺序

1.获取所有播放列表ID:如果返回Get Request JSON包含nextPageToken,则在转到(2)之前再次使用该页面运行Get Request

2.获取所有视频ID:如果返回Get Request JSON包含nextPageToken,则在转到(3)之前再次使用该页面运行Get Request

3.聚合成最终数组:我需要将所有数组放入如下数组:var ArrFinal = [{channelId,playlistID,videoId},{channelId,playlistID,videoId},{channelId,playlistID,videoId}];

我不一定需要有人来写整件事。我想要更好地了解最有效的方法来了解上一步的完成时间,还要处理nextPageToken迭代。

node.js youtube-data-api
1个回答
0
投票

我不熟悉youtube api。但你基本上需要的是每个端点的get函数。这个函数也应该关心“nextPageToken”。

这样的事情:(未经测试)

'use strict';

const Promise = require('bluebird');
const request = Promise.promisifyAll(require('request'));

const playlistEndpoint = '/youtube/v3/playlists';
const baseUrl = 'https://www.googleapis.com'

const channelIds = ['xy', 'ab', 'cd'];

const getPlaylist = async (channelId, pageToken, playlists) => {

  const url = `${baseUrl}${playlistEndpoint}`;
  const qs = { 
    channelId,
    maxResults: 25,
    pageToken
  };

  try {
    const playlistRequest = await request.getAsync({ url, qs });
    const nextPageToken = playlistRequest.body.nextPageToken;
    // if we already had items, combine with the new ones
    const items = playlists ? playlists.concat(playlistRequest.body.items) : playlistRequest.body.items;
    if (nextPageToken) {
      // if token, do the same again and pass results to function
      return getPlaylist(channelId, nextPageToken, items);
    }
    // if no token we are finished 
    return items;
  }
  catch (e) {
    console.log(e.message);
  }
};

const getVideos = async (playlistId, pageToken, videos) => {

  // pretty much the same as above
}

function awesome(channelIds) {

  const fancyArray = [];

  await Promise.map(channelIds, async (channelId) => {

    const playlists = await getPlaylist(channelId);

    const videos = await Promise.map(playlists, async (playlistId) => {

      const videos = await getVideos(playlistId);

      videos.forEach(videoId => {
        fancyArray.push({ channelId, playlistId, videoId })
      })
    });
  });

  return fancyArray;
}

awesome(channelIds)

//更新

这可能是很多并发请求,您可以通过使用来限制它们

Promise.map(items, item => { somefunction() }, { concurrency: 5 });
© www.soinside.com 2019 - 2024. All rights reserved.