Last active
August 29, 2015 13:57
-
-
Save twolfson/9538629 to your computer and use it in GitHub Desktop.
HipChat icon scraper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://www.hipchat.com/docs/apiv2/method/get_all_emoticons | |
var assert = require('assert'); | |
var async = require('async'); | |
var request = require('request'); | |
// Assert that we have a token from env | |
var apiToken = process.env.HIPCHAT_V2_TOKEN; | |
assert(apiToken, '`HIPCHAT_V2_TOKEN` was not found as an environment variable. Please provide one. This can be found at https://<your account>.hipchat.com/account/api'); | |
// Request a few pages (you probably don't have more than these) | |
// DEV: This is also quick and dirty (yey) | |
async.times(10, function getEmoticons (i, cb) { | |
request({ | |
url: 'http://api.hipchat.com/v2/emoticon', | |
qs: { | |
auth_token: apiToken, | |
type: 'all', | |
'start-index': i * 100 | |
} | |
}, function handleResponse (err, res, body) { | |
cb(err, body); | |
}) | |
}, function handleEmoticons (err, bodies) { | |
// If there was an error, throw it | |
if (err) { | |
throw err; | |
} | |
// Collect the emoticons | |
var parsedBodies = bodies.map(JSON.parse); | |
var emoticons = []; | |
parsedBodies.forEach(function addEmoticons (parsedBody) { | |
emoticons.push.apply(emoticons, parsedBody.items); | |
}); | |
// Generate a webpage | |
// DEV: This is the most secure thing ever. | |
console.log('<table style="text-align: center">'); | |
console.log('<tr>'); | |
console.log('<th>Emoticon</th>'); | |
console.log('<th>Preview</th>'); | |
console.log('</tr>'); | |
emoticons.forEach(function (emoticon) { | |
console.log('<tr>'); | |
console.log('<td><pre>(' + emoticon.shortcut + ')</pre></td>'); | |
console.log('<td><img src="' + emoticon.url + '"/></td>'); | |
console.log('</tr>'); | |
}); | |
console.log('</table>'); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "gist-hipchat-scraper", | |
"version": "0.1.0", | |
"description": "Quick and dirty HipChat icon scraper", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"repository": { | |
"type": "git", | |
"url": "https://gist.github.com/9538629.git" | |
}, | |
"author": "Todd Wolfson <todd@twolfson.com>", | |
"license": "MIT", | |
"bugs": { | |
"url": "https://gist.github.com/9538629.git" | |
}, | |
"homepage": "https://gist.github.com/9538629.git", | |
"private": true, | |
"dependencies": { | |
"request": "~2.34.0", | |
"async": "~0.2.10" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment