mirror of
https://github.com/azures04/crafatar.git
synced 2026-05-06 19:10:38 +02:00
@@ -4,9 +4,9 @@ var config = {
|
||||
default_size: 160, // size to be used when no size given
|
||||
local_cache_time: 3600, // seconds until we will check if the image changed. should be > 60 to prevent mojang 429 response
|
||||
browser_cache_time: 3600, // seconds until browser will request image again
|
||||
http_timeout: 1000, // ms until connection to mojang is dropped
|
||||
http_timeout: 3000, // ms until connection to mojang is dropped
|
||||
faces_dir: 'skins/faces/', // directory where faces are kept. should have trailing '/'
|
||||
helms_dir: 'skins/helms/' // directory where helms are kept. should have trailing '/'
|
||||
};
|
||||
|
||||
module.exports = config;
|
||||
module.exports = config;
|
||||
|
||||
@@ -5,7 +5,7 @@ var skins = require('./skins');
|
||||
|
||||
// 0098cb60-fa8e-427c-b299-793cbd302c9a
|
||||
var valid_uuid = /^([0-9a-f-]{32,36}|[a-zA-Z0-9_]{1,16})$/; // uuid|username
|
||||
var hash_pattern = /([^\/]+)(?=\.\w{0,16}$)|((?:[a-z][a-z]*[0-9]+[a-z0-9]*))/;
|
||||
var hash_pattern = /[0-9a-f]+$/;
|
||||
|
||||
function get_hash(url) {
|
||||
return hash_pattern.exec(url)[0].toLowerCase();
|
||||
@@ -14,28 +14,15 @@ function get_hash(url) {
|
||||
// requests skin for +uuid+ and extracts face/helm if image hash in +details+ changed
|
||||
// callback contains error, image hash
|
||||
function store_images(uuid, details, callback) {
|
||||
// get profile for +uuid+
|
||||
networking.get_profile(uuid, function(err, profile) {
|
||||
if (err === 0) {
|
||||
// uuid does not exist
|
||||
cache.save_hash(uuid, null);
|
||||
callback(null, null);
|
||||
} else if (err) {
|
||||
// get skin_url for +uuid+
|
||||
networking.get_skin_url(uuid, function(err, skin_url) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
var skinurl = null;
|
||||
|
||||
// Username handling
|
||||
if (uuid.length <= 16) {
|
||||
skinurl = "https://skins.minecraft.net/MinecraftSkins/" + uuid + ".png";
|
||||
console.log(uuid + " is a username");
|
||||
} else {
|
||||
skinurl = skin_url(profile);
|
||||
}
|
||||
if (skinurl) {
|
||||
console.log(uuid + " " + skinurl);
|
||||
if (skin_url) {
|
||||
console.log(uuid + " " + skin_url);
|
||||
// set file paths
|
||||
var hash = get_hash(skinurl);
|
||||
var hash = get_hash(skin_url);
|
||||
if (details && details.hash == hash) {
|
||||
// hash hasn't changed
|
||||
console.log(uuid + " hash has not changed");
|
||||
@@ -47,7 +34,7 @@ function store_images(uuid, details, callback) {
|
||||
var facepath = __dirname + '/../' + config.faces_dir + hash + ".png";
|
||||
var helmpath = __dirname + '/../' + config.helms_dir + hash + ".png";
|
||||
// download skin, extract face/helm
|
||||
networking.skin_file(skinurl, facepath, helmpath, function(err) {
|
||||
networking.skin_file(skin_url, facepath, helmpath, function(err) {
|
||||
if (err) {
|
||||
callback(err, null);
|
||||
} else {
|
||||
@@ -65,30 +52,14 @@ function store_images(uuid, details, callback) {
|
||||
});
|
||||
}
|
||||
|
||||
// exracts the skin url of a +profile+ object
|
||||
// returns null when no url found (user has no skin)
|
||||
function skin_url(profile) {
|
||||
var url = null;
|
||||
if (profile && profile.properties) {
|
||||
profile.properties.forEach(function(prop) {
|
||||
if (prop.name == 'textures') {
|
||||
var json = Buffer(prop.value, 'base64').toString();
|
||||
var props = JSON.parse(json);
|
||||
url = props && props.textures && props.textures.SKIN && props.textures.SKIN.url || null;
|
||||
}
|
||||
});
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
// decides whether to get an image from disk or to download it
|
||||
// callback contains error, status, hash
|
||||
// the status gives information about how the image was received
|
||||
// -1: error
|
||||
// 0: cached as null
|
||||
// 1: found on disk
|
||||
// 2: profile requested/found, skin downloaded from mojang servers
|
||||
// 3: profile requested/found, but it has not changed or no skin
|
||||
// -1: "error"
|
||||
// 0: "none" - cached as null
|
||||
// 1: "cached" - found on disk
|
||||
// 2: "downloaded" - profile downloaded, skin downloaded from mojang servers
|
||||
// 3: "checked" - profile re-downloaded (was too old), but it has either not changed or has no skin
|
||||
function get_image_hash(uuid, callback) {
|
||||
cache.get_details(uuid, function(err, details) {
|
||||
if (err) {
|
||||
@@ -100,12 +71,17 @@ function get_image_hash(uuid, callback) {
|
||||
callback(null, (details.hash ? 1 : 0), details.hash);
|
||||
} else {
|
||||
console.log(uuid + " uuid not known or too old");
|
||||
console.log("details:");
|
||||
console.log(details);
|
||||
console.log("/details");
|
||||
store_images(uuid, details, function(err, hash) {
|
||||
if (err) {
|
||||
callback(err, -1, details && details.hash);
|
||||
} else {
|
||||
console.log(uuid + " hash: " + hash);
|
||||
callback(null, (hash != (details && details.hash) ? 2 : 3), hash);
|
||||
var oldhash = details && details.hash;
|
||||
var status = hash !== oldhash ? 2 : 3;
|
||||
callback(null, status, hash);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,16 +4,60 @@ var skins = require('./skins');
|
||||
var fs = require("fs");
|
||||
|
||||
var session_url = "https://sessionserver.mojang.com/session/minecraft/profile/";
|
||||
var skins_url = "https://skins.minecraft.net/MinecraftSkins/";
|
||||
|
||||
var exp = {};
|
||||
|
||||
// download the Mojang profile for +uuid+
|
||||
// callback contains error, profile object
|
||||
exp.get_profile = function(uuid, callback) {
|
||||
if (uuid.length <= 16) {
|
||||
callback(null, null);
|
||||
return;
|
||||
// exracts the skin url of a +profile+ object
|
||||
// returns null when no url found (user has no skin)
|
||||
function extract_skin_url(profile) {
|
||||
var url = null;
|
||||
if (profile && profile.properties) {
|
||||
profile.properties.forEach(function(prop) {
|
||||
if (prop.name == 'textures') {
|
||||
var json = Buffer(prop.value, 'base64').toString();
|
||||
var props = JSON.parse(json);
|
||||
url = props && props.textures && props.textures.SKIN && props.textures.SKIN.url || null;
|
||||
}
|
||||
});
|
||||
}
|
||||
return url;
|
||||
}
|
||||
|
||||
// make a request to skins.miencraft.net
|
||||
// the skin url is taken from the HTTP redirect
|
||||
var get_username_url = function(name, callback) {
|
||||
request.get({
|
||||
url: skins_url + name + ".png",
|
||||
timeout: config.http_timeout,
|
||||
followRedirect: false
|
||||
}, function(error, response, body) {
|
||||
if (!error && response.statusCode == 301) {
|
||||
// skin_url received successfully
|
||||
console.log(name + " skin url received");
|
||||
callback(null, response.headers.location);
|
||||
} else if (error) {
|
||||
callback(error, null);
|
||||
} else if (response.statusCode == 404) {
|
||||
// skin doesn't exist
|
||||
console.log(name + " has no skin");
|
||||
callback(0, null);
|
||||
} else if (response.statusCode == 429) {
|
||||
// Too Many Requests
|
||||
// Never got this, seems like skins aren't limited
|
||||
console.warn(name + " Too many requests");
|
||||
console.warn(body);
|
||||
callback(null, null);
|
||||
} else {
|
||||
console.error(name + " Unknown error:");
|
||||
console.error(response);
|
||||
console.error(body);
|
||||
callback(null, null);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// make a request to sessionserver
|
||||
// the skin_url is taken from the profile
|
||||
var get_uuid_url = function(uuid, callback) {
|
||||
request.get({
|
||||
url: session_url + uuid,
|
||||
timeout: config.http_timeout // ms
|
||||
@@ -21,7 +65,7 @@ exp.get_profile = function(uuid, callback) {
|
||||
if (!error && response.statusCode == 200) {
|
||||
// profile downloaded successfully
|
||||
console.log(uuid + " profile downloaded");
|
||||
callback(null, JSON.parse(body));
|
||||
callback(null, extract_skin_url(JSON.parse(body)));
|
||||
} else if (error) {
|
||||
callback(error, null);
|
||||
} else if (response.statusCode == 204 || response.statusCode == 404) {
|
||||
@@ -42,6 +86,22 @@ exp.get_profile = function(uuid, callback) {
|
||||
});
|
||||
};
|
||||
|
||||
var exp = {};
|
||||
|
||||
// download skin_url for +uuid+ (name or uuid)
|
||||
// callback contains error, skin_url
|
||||
exp.get_skin_url = function(uuid, callback) {
|
||||
if (uuid.length <= 16) {
|
||||
get_username_url(uuid, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
} else {
|
||||
get_uuid_url(uuid, function(err, url) {
|
||||
callback(err, url);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
// downloads skin file from +url+
|
||||
// stores face image as +facename+
|
||||
// stores helm image as +helmname+
|
||||
|
||||
Reference in New Issue
Block a user