var yModule = require('youtube-node'),
nodeYoutube = new yModule();
nodeYoutube.setKey("key");
module.exports.getVideoLength = function (vData){
youTube.getById(vData, function (result) {
return convertTime(result['items'][0]['contentDetails']['duration']);
})
};
var convertTime = function (time){
var reptms = /(?:(\d+)DT)?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$/;
var days = "00", hours = "00", minutes = "00", seconds = "00", formattedTime;
//if (reptms.test(time)) {
var matches = reptms.exec(time);
console.log(matches);
if (matches[1]) days = String(matches[1]);
if (matches[2]) hours = String(matches[2]);
if (matches[3]) minutes = String(matches[3]);
if (matches[4]) seconds = String(matches[4]);
formattedTime = "[" + days + ":" + hours + ":" + minutes + ":" + seconds + "]";
return formattedTime;
//}
};
콜백에 대해 몇 가지 내용을 읽은 후에도 이해하기가 어렵습니다. nodeJs callbacks simple example 조금 도움이되었지만 작동 방식에 대해 아직 명확하지 않습니다. 지난 시간에 콜백을 사용하여 이것을 작성하는 방법을 알아 내려고 노력했습니다. 콜백을 사용해야합니까?
이 모듈
이 호출되고 :ytRetrieve.getVideoLength(youtube_parser(text))
youtube_parser의 기능 : 당신은 콜백을 사용할 필요가
function youtube_parser(url){
var regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#\&\?]*).*/;
var match = url.match(regExp);
//console.log(match);
if (match&&match[7]){
return match[7].split(" ")[0];
}
}
'무엇을 bot'입니까? 이것을 만들었나요 아니면 다른 모듈입니까? –
@ExplosionPills 그것은 'irc'모듈입니다 – ECMAScript
이것은 들뜬 일입니다. 콜백이 필요한지 묻는 사용자 이름 "ECMAScript" 짧은 대답 : 예, 항상, 특히 노드에서. 여기에 훌륭한 콜백 리소스가 있습니다. (http://javascriptissexy.com/understand-javascript-callback-functions-and-use-them/) 당신의 머리를 감싸는 데 도움이됩니다. 건배! –