@micmac/youtube
Advanced tools
Comparing version 4.0.1 to 4.0.2
250
main.js
// fonction searchVideos retourne les 10 dernières videos de la chaine beerus | ||
var { | ||
} = require ('googleapis'); | ||
var moment = require ('moment'); | ||
var youtube = google.youtube ({ | ||
} = require('googleapis'); | ||
var moment = require('moment'); | ||
var youtube = google.youtube({ | ||
version: 'v3', | ||
auth: process.env.APIKEY | ||
}); | ||
const download = require('image-downloader'); | ||
function getVideosID () { | ||
return new Promise (function (resolve, reject) { | ||
moment.locale ('fr'); | ||
youtube.search.list ({ | ||
function getVideosID() { | ||
return new Promise(function (resolve, reject) { | ||
moment.locale('fr'); | ||
youtube.search.list({ | ||
"part": "id,snippet", | ||
@@ -21,10 +22,8 @@ "channelId": "UCtToNyHU2Fjxuh__ypkHiIQ", | ||
}, function (error, response) { | ||
if (error) | ||
{ | ||
reject (error); | ||
} | ||
if (response) | ||
{ | ||
resolve (response); | ||
} | ||
if (error) { | ||
reject(error); | ||
} | ||
if (response) { | ||
resolve(response); | ||
} | ||
}); | ||
@@ -36,5 +35,5 @@ }); | ||
var getVideoInfo = async function (_idVideo, _noVideo) { | ||
var videoPromise = new Promise (function (resolve, reject) { | ||
moment.locale ('fr'); | ||
youtube.videos.list ({ | ||
var videoPromise = new Promise(function (resolve, reject) { | ||
moment.locale('fr'); | ||
youtube.videos.list({ | ||
"part": "id,snippet,contentDetails", | ||
@@ -44,16 +43,14 @@ "id": _idVideo, | ||
}, function (error, response) { | ||
if (error) | ||
{ | ||
reject (error); | ||
} | ||
if (response) | ||
{ | ||
resolve ({ | ||
data: response, | ||
noVideo: _noVideo | ||
}); | ||
} | ||
if (error) { | ||
reject(error); | ||
} | ||
if (response) { | ||
resolve({ | ||
data: response, | ||
noVideo: _noVideo | ||
}); | ||
} | ||
}); | ||
}); | ||
var result = await videoPromise.then (function (data) { | ||
var result = await videoPromise.then(function (data) { | ||
return data; | ||
@@ -65,5 +62,5 @@ }); | ||
var getVideoComments = async function (_idVideo, _noVideo) { | ||
var commentPromise = new Promise (function (resolve, reject) { | ||
moment.locale ('fr'); | ||
youtube.commentThreads.list ({ | ||
var commentPromise = new Promise(function (resolve, reject) { | ||
moment.locale('fr'); | ||
youtube.commentThreads.list({ | ||
"part": "id,snippet", | ||
@@ -74,18 +71,16 @@ "maxResults": 10, | ||
}, function (error, response) { | ||
if (error) | ||
{ | ||
reject (error); | ||
} | ||
if (response) | ||
{ | ||
resolve ({ | ||
data: response, | ||
noVideo: _noVideo | ||
}); | ||
} | ||
if (error) { | ||
reject(error); | ||
} | ||
if (response) { | ||
resolve({ | ||
data: response, | ||
noVideo: _noVideo | ||
}); | ||
} | ||
}); | ||
}); | ||
var result = await commentPromise.then (function (data) { | ||
// console.log ('comment :'); | ||
// console.log(data); | ||
var result = await commentPromise.then(function (data) { | ||
// console.log ('comment :'); | ||
// console.log(data); | ||
return data; | ||
@@ -96,19 +91,19 @@ }); | ||
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); | ||
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); | ||
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'), | ||
date: moment(videoData.data.data.items[0].snippet.publishedAt).format('LL'), | ||
description1: videoData.data.data.items[0].snippet.description, | ||
@@ -119,16 +114,47 @@ description2: description.chaine1, | ||
tags: videoData.data.data.items[0].snippet.tags, | ||
comments: new Array () | ||
comments: new Array() | ||
}; | ||
// console.log ('resultDataBuffer : '); | ||
// console.log (resultDataBuffer); | ||
const options = { | ||
url: videoData.data.data.items[0].snippet.thumbnails.medium.url, | ||
dest: '/public/img/thumbnails' | ||
} | ||
downloadIMG(options) | ||
// console.log ('resultDataBuffer : '); | ||
// console.log (resultDataBuffer); | ||
return resultDataBuffer; | ||
}); | ||
// console.log ('resultData[n] : '); | ||
// console.log (resultData[n]); | ||
// 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; | ||
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) { | ||
resultCommentBuffer[m] = { | ||
author: q.snippet.topLevelComment.snippet.authorDisplayName, | ||
text: q.snippet.topLevelComment.snippet.textOriginal, | ||
avatar: q.snippet.topLevelComment.snippet.authorProfileImageUrl, | ||
date: moment(q.snippet.topLevelComment.snippet.publishedAt).format('LL') | ||
}; | ||
}); | ||
// console.log ('resultCommentBuffer :'); | ||
// console.log (resultCommentBuffer); | ||
return resultCommentBuffer; | ||
} | ||
else { | ||
// console.log ('pas de commentaire pour cette video'); | ||
return [ | ||
@@ -142,36 +168,8 @@ { | ||
]; | ||
}) | ||
.then (function (responseComments) { | ||
if (authorizedComment && responseComments.data.data.items.length > 0) | ||
{ | ||
var resultCommentBuffer = new Array (); | ||
responseComments.data.data.items.forEach (function (q, m) { | ||
resultCommentBuffer[m] = { | ||
author: q.snippet.topLevelComment.snippet.authorDisplayName, | ||
text: q.snippet.topLevelComment.snippet.textOriginal, | ||
avatar: q.snippet.topLevelComment.snippet.authorProfileImageUrl, | ||
date: moment (q.snippet.topLevelComment.snippet.publishedAt).format ('LL') | ||
}; | ||
}); | ||
// console.log ('resultCommentBuffer :'); | ||
// console.log (resultCommentBuffer); | ||
return resultCommentBuffer; | ||
} | ||
else | ||
{ | ||
// console.log ('pas de commentaire pour cette video'); | ||
return [ | ||
{ | ||
author: '', | ||
text: '', | ||
avatar: '', | ||
date: '' | ||
} | ||
]; | ||
} | ||
}); | ||
} | ||
}); | ||
resultData[n].comments = resultComment[n]; | ||
}).then (function () { | ||
// console.log ('resultData : '); | ||
// console.log (resultData); | ||
}).then(function () { | ||
// console.log ('resultData : '); | ||
// console.log (resultData); | ||
return resultData; | ||
@@ -181,8 +179,8 @@ }); | ||
}); | ||
// console.log ('RESULTAT'); | ||
// console.log ('------------------------------'); | ||
// console.log (result); | ||
// console.log ('RESULTAT'); | ||
// console.log ('------------------------------'); | ||
// console.log (result); | ||
return result; | ||
} | ||
; | ||
; | ||
module.exports = { | ||
@@ -199,13 +197,12 @@ getAllVideos: getAllVideos | ||
function splitTxt (_chaine, _middle) { | ||
function splitTxt(_chaine, _middle) { | ||
let lgTxt = _chaine.length; | ||
let chaine1 = _chaine.slice (0, Math.max (Math.floor (lgTxt / 2), _middle)); | ||
let chaine2 = _chaine.slice (Math.max (Math.floor (lgTxt / 2), _middle)); | ||
let lastSpacePos = chaine1.lastIndexOf (' '); | ||
if (lgTxt !== chaine1.length) | ||
{ | ||
let pieceOfChaine = chaine1.slice (lastSpacePos); | ||
chaine1 = chaine1.slice (0, lastSpacePos); | ||
chaine2 = pieceOfChaine + chaine2; | ||
} | ||
let chaine1 = _chaine.slice(0, Math.max(Math.floor(lgTxt / 2), _middle)); | ||
let chaine2 = _chaine.slice(Math.max(Math.floor(lgTxt / 2), _middle)); | ||
let lastSpacePos = chaine1.lastIndexOf(' '); | ||
if (lgTxt !== chaine1.length) { | ||
let pieceOfChaine = chaine1.slice(lastSpacePos); | ||
chaine1 = chaine1.slice(0, lastSpacePos); | ||
chaine2 = pieceOfChaine + chaine2; | ||
} | ||
return { | ||
@@ -217,9 +214,16 @@ chaine1: chaine1, | ||
; | ||
async function asyncForEach (array, callback) { | ||
async function asyncForEach(array, callback) { | ||
for (let index = 0; | ||
index < array.length; | ||
index ++) | ||
{ | ||
await callback (array[index], index, array); | ||
} | ||
index < array.length; | ||
index++) { | ||
await callback(array[index], index, array); | ||
} | ||
}; | ||
async function downloadIMG(_options) { | ||
try { | ||
const { filename, image } = await download.image(_options) | ||
console.log(filename) // => /path/to/dest/image.jpg | ||
} catch (e) { | ||
console.error(e) | ||
} | ||
} |
{ | ||
"name": "@micmac/youtube", | ||
"version": "4.0.1", | ||
"version": "4.0.2", | ||
"description": "utilisation api youtube data", | ||
@@ -13,4 +13,5 @@ "main": "main.js", | ||
"googleapis": "45.0.0", | ||
"image-downloader": "^3.5.0", | ||
"moment": "^2.24.0" | ||
} | ||
} |
8490
212
3
+ Addedimage-downloader@^3.5.0
+ Addedajv@6.12.6(transitive)
+ Addedasn1@0.2.6(transitive)
+ Addedassert-plus@1.0.0(transitive)
+ Addedasynckit@0.4.0(transitive)
+ Addedaws-sign2@0.7.0(transitive)
+ Addedaws4@1.13.2(transitive)
+ Addedbcrypt-pbkdf@1.0.2(transitive)
+ Addedcaseless@0.12.0(transitive)
+ Addedcombined-stream@1.0.8(transitive)
+ Addedcore-util-is@1.0.2(transitive)
+ Addeddashdash@1.14.1(transitive)
+ Addeddelayed-stream@1.0.0(transitive)
+ Addedecc-jsbn@0.1.2(transitive)
+ Addedextsprintf@1.3.0(transitive)
+ Addedfast-deep-equal@3.1.3(transitive)
+ Addedfast-json-stable-stringify@2.1.0(transitive)
+ Addedforever-agent@0.6.1(transitive)
+ Addedform-data@2.3.3(transitive)
+ Addedgetpass@0.1.7(transitive)
+ Addedhar-schema@2.0.0(transitive)
+ Addedhar-validator@5.1.5(transitive)
+ Addedhttp-signature@1.2.0(transitive)
+ Addedimage-downloader@3.6.0(transitive)
+ Addedis-typedarray@1.0.0(transitive)
+ Addedisstream@0.1.2(transitive)
+ Addedjsbn@0.1.1(transitive)
+ Addedjson-schema@0.4.0(transitive)
+ Addedjson-schema-traverse@0.4.1(transitive)
+ Addedjson-stringify-safe@5.0.1(transitive)
+ Addedjsprim@1.4.2(transitive)
+ Addedmime-db@1.52.0(transitive)
+ Addedmime-types@2.1.35(transitive)
+ Addedoauth-sign@0.9.0(transitive)
+ Addedperformance-now@2.1.0(transitive)
+ Addedpsl@1.15.0(transitive)
+ Addedpunycode@2.3.1(transitive)
+ Addedqs@6.5.3(transitive)
+ Addedrequest@2.88.2(transitive)
+ Addedsafer-buffer@2.1.2(transitive)
+ Addedsshpk@1.18.0(transitive)
+ Addedtough-cookie@2.5.0(transitive)
+ Addedtunnel-agent@0.6.0(transitive)
+ Addedtweetnacl@0.14.5(transitive)
+ Addeduri-js@4.4.1(transitive)
+ Addeduuid@3.4.0(transitive)
+ Addedverror@1.10.0(transitive)