skins.js 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. var http = require('http');
  2. var https = require('https');
  3. var fs = require('fs');
  4. var imagemagick = require('imagemagick');
  5. /*
  6. * Skin retrieval methods are based on @jomo's CLI Crafatar implementation.
  7. * https://github.com/jomo/Crafatar
  8. */
  9. module.exports = {
  10. get_profile: function(uuid, callback) {
  11. https.get("https://sessionserver.mojang.com/session/minecraft/profile/" + uuid, function(res) {
  12. res.on('data', function(d) {
  13. var profile = JSON.parse(d);
  14. if (profile.error) throw profile.error;
  15. callback(profile);
  16. });
  17. });
  18. },
  19. skin_url: function(profile) {
  20. var url = null;
  21. if (profile && profile.properties) {
  22. profile.properties.forEach(function(prop) {
  23. if (prop.name == 'textures') {
  24. var json = Buffer(prop.value, 'base64').toString();
  25. var props = JSON.parse(json);
  26. url = props.textures.SKIN.url;
  27. }
  28. });
  29. }
  30. return url;
  31. },
  32. skin_file: function(url, filename, callback) {
  33. var file = fs.createWriteStream(filename);
  34. http.get(url, function(res) {
  35. res.on('data', function(data) {
  36. file.write(data);
  37. }).on('end', function() {
  38. file.end();
  39. callback();
  40. });
  41. });
  42. },
  43. extract_face: function(infile, outfile, callback) {
  44. imagemagick.convert([infile, '-crop', '8x8+8+8', outfile], function(err, stdout) {
  45. if (err) throw err;
  46. callback();
  47. });
  48. }
  49. };