@micmac/youtube
Advanced tools
Comparing version 3.2.2 to 4.0.0
160
main.js
@@ -11,3 +11,3 @@ // fonction searchVideos retourne les 10 dernières videos de la chaine beerus | ||
var getVideosID = function () { | ||
function getVideosID () { | ||
return new Promise (function (resolve, reject) { | ||
@@ -32,6 +32,7 @@ moment.locale ('fr'); | ||
}); | ||
}; | ||
} | ||
; | ||
var getVideoInfo = function (_idVideo, _noVideo) { | ||
return new Promise (function (resolve, reject) { | ||
var getVideoInfo = async function (_idVideo, _noVideo) { | ||
var videoPromise = new Promise (function (resolve, reject) { | ||
moment.locale ('fr'); | ||
@@ -56,6 +57,10 @@ youtube.videos.list ({ | ||
}); | ||
var result = await videoPromise.then (function (data) { | ||
return data; | ||
}); | ||
return result; | ||
}; | ||
var getVideoComments = function (_idVideo, _noVideo) { | ||
return new Promise (function (resolve, reject) { | ||
var getVideoComments = async function (_idVideo, _noVideo) { | ||
var commentPromise = new Promise (function (resolve, reject) { | ||
moment.locale ('fr'); | ||
@@ -81,40 +86,60 @@ youtube.commentThreads.list ({ | ||
}); | ||
var result = await commentPromise.then (function (data) { | ||
// console.log ('comment :'); | ||
// console.log(data); | ||
return data; | ||
}); | ||
return result; | ||
}; | ||
var getAllVideosInfos = function () { | ||
var videoFullInfos = { | ||
video: new Array () | ||
}; | ||
var promiseInfo = new Array (); | ||
var promiseComment = new Array (); | ||
var nbOfVideo = 0; | ||
return new Promise (function (resolveAll, reject) { | ||
moment.locale ('fr'); | ||
getVideosID ().catch (function (error) { | ||
console.log ('getVideosID ERROR : ' + error); | ||
}).then (function (responseVideosID) { | ||
nbOfVideo = responseVideosID.data.items.length; | ||
console.log ('nombre de videos : ' + nbOfVideo); | ||
responseVideosID.data.items.forEach (function (p, n) { | ||
promiseInfo[n] = getVideoInfo (p.id.videoId, n).catch (function (errorVideoInfo) { | ||
console.log ('getVideoInfo : ' + errorVideoInfo); | ||
}).then (function (responseVideoInfo) { | ||
let description = splitTxt (responseVideoInfo.data.data.items[0].snippet.description, 100); | ||
videoFullInfos.video[n] = { | ||
id: p.id.videoId, | ||
title: responseVideoInfo.data.data.items[0].snippet.title, | ||
date: moment (responseVideoInfo.data.data.items[0].snippet.publishedAt).format ('LL'), | ||
description1: responseVideoInfo.data.data.items[0].snippet.description, | ||
description2: description.chaine1, | ||
thumbnail: responseVideoInfo.data.data.items[0].snippet.thumbnails.standard.url, | ||
tags: responseVideoInfo.data.data.items[0].snippet.tags, | ||
comments: new Array () | ||
}; | ||
promiseComment[n] = getVideoComments (p.id.videoId, n).catch (function (errorComments) { | ||
console.log ('comment ERROR : ' + errorComments); | ||
}).then (function (responseComments) { | ||
if (responseComments.data.data.items.length > 0) | ||
var getAllVideos = async function () { | ||
moment.locale ('fr'); | ||
var result = await getVideosID ().catch (function (errorGetVideosID) { | ||
console.log ('errorGetVideosID : ' + errorGetVideosID); | ||
}).then (function (dataListVideo) { | ||
// console.log ('dataListVideo : '); | ||
// console.log (dataListVideo); | ||
var nbOfVideo = dataListVideo.data.pageInfo.totalResults; | ||
var resultData = new Array (); | ||
var resultComment = new Array (); | ||
console.log ('nombre de videos : ' + nbOfVideo); | ||
var resultForEach = asyncForEach (dataListVideo.data.items, async function (p, n) { | ||
resultData[n] = await getVideoInfo (p.id.videoId, n).then (function (videoData) { | ||
let description = splitTxt (videoData.data.data.items[0].snippet.description, 100); | ||
let resultDataBuffer = { | ||
id: p.id.videoId, | ||
title: videoData.data.data.items[0].snippet.title, | ||
date: moment (videoData.data.data.items[0].snippet.publishedAt).format ('LL'), | ||
description1: videoData.data.data.items[0].snippet.description, | ||
description2: description.chaine1, | ||
thumbnail: videoData.data.data.items[0].snippet.thumbnails.standard.url, | ||
tags: videoData.data.data.items[0].snippet.tags, | ||
comments: new Array () | ||
}; | ||
// console.log ('resultDataBuffer : '); | ||
// console.log (resultDataBuffer); | ||
return resultDataBuffer; | ||
}); | ||
// console.log ('resultData[n] : '); | ||
// console.log (resultData[n]); | ||
var authorizedComment = true; | ||
resultComment[n] = await getVideoComments (p.id.videoId, n) | ||
.catch (function (e) { | ||
// console.log ('error comment' + e); | ||
// console.log ('error video ' + n); | ||
authorizedComment = false; | ||
return [ | ||
{ | ||
author: '', | ||
text: '', | ||
avatar: '', | ||
date: '' | ||
} | ||
]; | ||
}) | ||
.then (function (responseComments) { | ||
if (authorizedComment && responseComments.data.data.items.length > 0) | ||
{ | ||
var resultCommentBuffer = new Array (); | ||
responseComments.data.data.items.forEach (function (q, m) { | ||
console.log ('no de la video : ' + n + ' no du commentaire :' + m + ' commentaire : ' + JSON.stringify (q.snippet.topLevelComment.snippet.textOriginal)); | ||
videoFullInfos.video[n].comments[m] = { | ||
resultCommentBuffer[m] = { | ||
author: q.snippet.topLevelComment.snippet.authorDisplayName, | ||
@@ -125,21 +150,36 @@ text: q.snippet.topLevelComment.snippet.textOriginal, | ||
}; | ||
//console.log('comments : ' + JSON.stringify( videoFullInfos.video[responseComments.noVideo].comments[m])); | ||
}); | ||
// console.log ('resultCommentBuffer :'); | ||
// console.log (resultCommentBuffer); | ||
return resultCommentBuffer; | ||
} | ||
else | ||
{ | ||
// console.log ('pas de commentaire pour cette video'); | ||
return [ | ||
{ | ||
author: '', | ||
text: '', | ||
avatar: '', | ||
date: '' | ||
} | ||
]; | ||
} | ||
}); | ||
}); | ||
}); | ||
setTimeout (function () { | ||
var promiseAll = Promise.all (promiseInfo, promiseComment) | ||
.then (function () { | ||
console.log ('FINAL : ' + JSON.stringify (videoFullInfos)); | ||
resolveAll (videoFullInfos); | ||
}); | ||
}); | ||
}, 2000); | ||
resultData[n].comments = resultComment[n]; | ||
}).then (function () { | ||
// console.log ('resultData : '); | ||
// console.log (resultData); | ||
return resultData; | ||
}); | ||
return resultForEach; | ||
}); | ||
}; | ||
// console.log ('RESULTAT'); | ||
// console.log ('------------------------------'); | ||
// console.log (result); | ||
return result; | ||
} | ||
; | ||
module.exports = { | ||
getAllVideosInfos: getAllVideosInfos | ||
getAllVideos: getAllVideos | ||
}; | ||
@@ -170,2 +210,10 @@ // autres écritures possible : | ||
} | ||
; | ||
; | ||
async function asyncForEach (array, callback) { | ||
for (let index = 0; | ||
index < array.length; | ||
index ++) | ||
{ | ||
await callback (array[index], index, array); | ||
} | ||
} |
{ | ||
"name": "@micmac/youtube", | ||
"version": "3.2.2", | ||
"version": "4.0.0", | ||
"description": "utilisation api youtube data", | ||
@@ -5,0 +5,0 @@ "main": "main.js", |
8357
207