Skip to content

Instantly share code, notes, and snippets.

@jonirrings
Created April 10, 2019 07:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonirrings/3bd5d95433ed8f25fdd1687f5e5ad2ba to your computer and use it in GitHub Desktop.
Save jonirrings/3bd5d95433ed8f25fdd1687f5e5ad2ba to your computer and use it in GitHub Desktop.
downloadIZuKan.js
function parse(rawInfo) {
if (rawInfo.decode_key) {
rawInfo.decode_key = rawInfo.decode_key.split('-')
}
return rawInfo;
}
function decode(info, image, canvas) {
const d2 = canvas.getContext("2d");
canvas.width = image.width;
canvas.height = image.height;
d2.drawImage(image, 0, 0);
const {decode_key} = info;
const width = 96;
const height = 128;
const nx = Math.floor(image.width / width);
// const ny = Math.floor(image.height / height);
for (let i = 0; i < decode_key.length; i++) {
const key = decode_key[i];
const sx = width * Math.floor(i % nx);
const sy = 128 * Math.floor(i / nx);
const x = Math.round(96 * Math.floor(key % nx));
const y = Math.round(128 * Math.floor(key / nx));
d2.drawImage(image, sx, sy, width, height, x, y, width, height);
}
}
function makeUrl(page_no) {
return GUARDIAN_SERVER + "/files/" + FILE_ID + "/images/" + page_no + "/info";
}
const array = [];
let page_no = 0;
const body = document.querySelector('body');
function downloadImg() {
console.log(`try to download image ${title}_${page_no}.jpg`);
const toDownload = array[page_no];
if (toDownload && !toDownload.downloaded) {
const info = toDownload.info;
const image = document.createElement('img');
image.onload = function () {
console.info(`save image ${title}_${page_no}.jpg`);
const canvas = document.createElement('canvas');
decode(info, image, canvas);
const a = document.createElement('a');
a.download = title + '_' + page_no + ".jpg";
canvas.toBlob(blob => {
a.href = URL.createObjectURL(blob);
a.click();
toDownload.downloaded = true;
image.onload = null;
body.removeChild(image);
page_no += 1;
detect();
}, "image/jpeg");
};
image.src = info.image_url;
body.appendChild(image)
}
}
function processInfo(rawInfo) {
console.info(`downloaded page:${page_no} info`);
const info = parse(rawInfo);
array[page_no] = {
info,
downloaded: false,
};
setTimeout(function () {
downloadImg();
}, 200)
}
function reportError() {
console.error("GET INFO FAILED");
console.dir(array);
}
function detect(start) {
if(start){
page_no =start;
}
console.log(`downloading page:${page_no} info`);
$.ajax({
url: makeUrl(page_no),
data: {
license_id: LICENSE_ID,
license_token: TOKEN,
},
dataType: "jsonp",
jsonp: "jsoncallback",
jsonpCallback: "processInfo",
//success: processInfo,
error: reportError
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment