corsica-xkcd
Advanced tools
Comparing version 0.0.0 to 0.1.0
72
index.js
@@ -12,2 +12,5 @@ /* Description: | ||
* mythmon | ||
* | ||
* Compatible with the command plugin | ||
* "xkcd comic=123" or "xkcd 123" | ||
*/ | ||
@@ -24,29 +27,11 @@ | ||
corsica.on('content', function(content) { | ||
var match = /xkcd.com\/(\d+)?/.exec(content.url); | ||
if (content.type === 'url' && /xkcd.com/.exec(content.url)) { | ||
return new Promise(function(resolve, reject) { | ||
var jsonUrl = content.url; | ||
if (jsonUrl.charAt(jsonUrl.length - 1) !== '/') { | ||
jsonUrl += '/'; | ||
} | ||
jsonUrl += '/info.0.json'; | ||
var opts = { | ||
url: jsonUrl, | ||
json: true, | ||
}; | ||
request(opts, function (err, res, data) { | ||
if (err || res.statusCode >= 400) { | ||
console.warn('[xkcd]', 'Could not get info for comic at', jsonUrl); | ||
resolve(content); | ||
} else { | ||
resolve(makeXkcdPage(content, data)); | ||
} | ||
var match = /xkcd.com\/?(\d+)?/.exec(content.url); | ||
if (content.type === 'url' && match) { | ||
return makeComicContent(match[1]) | ||
.then(function(comicContent) { | ||
return corsica.utils.merge(content, comicContent); | ||
}, function (err) { | ||
console.warn('[xkcd] Could not get info for comic at', content.url); | ||
return content; | ||
}); | ||
}) | ||
.catch(function(err) { | ||
console.error('error', err); | ||
throw err; | ||
}); | ||
} else { | ||
@@ -56,11 +41,42 @@ return content; | ||
}); | ||
// command-compatible short code. | ||
corsica.on('xkcd', function(content) { | ||
console.log('[xkcd]', 'on xkcd', content); | ||
var comic = content.comic || content._args[0]; | ||
makeComicContent(comic) | ||
.then(function(comicContent) { | ||
corsica.sendMessage('content', corsica.utils.merge(content, comicContent)); | ||
}); | ||
return content; | ||
}); | ||
}; | ||
function makeComicContent(comicNum) { | ||
console.log('[xkcd]', 'makeComicContent', 'comicNum =', comicNum); | ||
return new Promise(function (resolve, reject) { | ||
var url; | ||
if (!comicNum) { | ||
url = 'http://xkcd.com/info.0.json'; | ||
} else { | ||
url = 'http://xkcd.com/' + comicNum + '/info.0.json'; | ||
} | ||
request({url: url, json: true}, function(err, res, data) { | ||
if (err || res.statusCode >= 400) { | ||
reject(err || {statusCode: res.statusCode}); | ||
} else { | ||
resolve(makeXkcdPage(data)); | ||
} | ||
}); | ||
}); | ||
} | ||
var pageTemplate = '<body style="margin:0;height:100%;background:url(%s) ' + | ||
'no-repeat center #000;background-size:contain;"></body>'; | ||
function makeXkcdPage(content, data) { | ||
function makeXkcdPage(data) { | ||
var html = format(pageTemplate, data.img); | ||
var res = { | ||
screen: content.screen, | ||
type: 'html', | ||
@@ -67,0 +83,0 @@ content: html, |
{ | ||
"name": "corsica-xkcd", | ||
"version": "0.0.0", | ||
"version": "0.1.0", | ||
"description": "A Corsica plugin to display XKCD comics.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
3176
5
73