diff --git a/squeebot.repo.json b/squeebot.repo.json index b7295c0..8c1056f 100644 --- a/squeebot.repo.json +++ b/squeebot.repo.json @@ -39,7 +39,7 @@ }, { "name": "urlreply", - "version": "1.0.0" + "version": "1.0.2" }, { "name": "utility", diff --git a/urlreply/plugin.json b/urlreply/plugin.json index 83b5eb5..7c0275a 100644 --- a/urlreply/plugin.json +++ b/urlreply/plugin.json @@ -2,7 +2,7 @@ "main": "plugin.js", "name": "urlreply", "description": "Fetch titles from web pages, specifically made for IRC", - "version": "1.0.1", + "version": "1.0.2", "tags": ["irc"], "dependencies": [], "npmDependencies": ["cheerio@^1.0.0-rc.3"] diff --git a/urlreply/plugin.ts b/urlreply/plugin.ts index dfe4b44..5da186e 100644 --- a/urlreply/plugin.ts +++ b/urlreply/plugin.ts @@ -33,45 +33,24 @@ function findUrls(text: string): string[] { return urlArray; } -// http://stackoverflow.com/a/22149575 -function ytDuration(source: string): string { - const a = source.match(/\d+/g); - let res; +// https://gist.github.com/denniszhao/8972cd4ae637cf10fe01 +function ytDuration(time: string): string { + const a = time.match(/\d+H|\d+M|\d+S/g); - if (!a) { - return ''; + let result = 0; + + const d: {[key: string]: number} = { H: 3600, M: 60, S: 1 }; + let num; + let type; + + for (const char of a || []) { + num = char.slice(0, char.length - 1); + type = char.slice(char.length - 1, char.length); + + result += parseInt(num, 10) * d[type]; } - if (source.indexOf('M') >= 0 && source.indexOf('H') === -1 && source.indexOf('S') === -1) { - res = [0, a[0], 0]; - } - - if (source.indexOf('H') >= 0 && source.indexOf('M') === -1) { - res = [a[0], 0, a[1]]; - } - - if (source.indexOf('H') >= 0 && source.indexOf('M') === -1 && source.indexOf('S') === -1) { - res = [a[0], 0, 0]; - } - - let duration = 0; - - if (a.length === 3) { - duration = duration + parseInt(a[0], 10) * 3600; - duration = duration + parseInt(a[1], 10) * 60; - duration = duration + parseInt(a[2], 10); - } - - if (a.length === 2) { - duration = duration + parseInt(a[0], 10) * 60; - duration = duration + parseInt(a[1], 10); - } - - if (a.length === 1) { - duration = duration + parseInt(a[0], 10); - } - - return toHHMMSS(duration.toString()); + return toHHMMSS(result.toString()); } async function dailymotionFromId(id: string, msg: IMessage): Promise {