Skip to content

Instantly share code, notes, and snippets.

@andrew-aladev
Last active January 21, 2016 20:48
Show Gist options
  • Save andrew-aladev/0d115643839d97b9b9ac to your computer and use it in GitHub Desktop.
Save andrew-aladev/0d115643839d97b9b9ac to your computer and use it in GitHub Desktop.
pdd.by data extractor
var async = require("async");
var cheerio = require("cheerio");
var http = require("http");
var fs = require("fs");
var mkdirp = require("mkdirp");
var path = require("path");
var questions = JSON.parse(fs.readFileSync("./pdd_by_questions.json", "utf8"));
questions = questions.map(function (question) {
var $ = cheerio.load(question.h, {decodeEntities : false});
$("*").each(function (index, element) {
var href = $(element).attr("href");
if (href != null) {
$(element).attr({
"href" : "http://pdd.by" + href,
"target" : "_blank"
});
}
});
var hint = $.root().html();
$ = cheerio.load(question.d, {decodeEntities : false});
var description = $.root().text();
var description_images = $("*").map(function (index, element) {
var url = $(element).css("background-image");
if (url != null) {
var match = /url\s*?\(\s*?['"](.*?)['"]/g.exec(url);
return "http://pdd.by" + match[1];
} else {
return null;
}
})
.get();
$ = cheerio.load(question.v, {decodeEntities : false});
var variants = $.root().children().map(function (index, element) {
return $(element).text();
})
.get();
return {
text : question.q,
answer : question.a,
variants : variants,
hint : hint,
description : description,
description_images : description_images
};
});
var index = 0;
var series = [];
questions.forEach(function (question) {
question.description_images = question.description_images.map(function (url) {
var file_path = "./images/" + index + path.extname(url);
(function (index, url, file_path) {
series.push(function (callback) {
var file = fs.createWriteStream(file_path);
http.get(url, function(response) {
response.pipe(file);
callback();
});
});
}) (index, url, file_path);
index ++;
return file_path;
});
});
mkdirp("./images", function () {
async.series(series);
});
questions = JSON.stringify(questions, null, 2);
var ejs = require("ejs");
var template = ejs.render(fs.readFileSync("index.html.ejs", "utf8"), {
questions : questions
});
fs.writeFile("index.html", template);
var save_data = function (data, file_name, type) {
var url = URL.createObjectURL(
new Blob(
[JSON.stringify(data, null, 2)],
{type: type}
)
);
var a = document.createElement("a");
a.href = url;
a.download = file_name;
a.click();
URL.revokeObjectURL(url);
}
save_data(JSON.parse(localStorage.getItem("pdd_by_questions")), "pdd_by_questions.json", "application/json");
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>ПДД</title>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-beta1/jquery.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/knockout/3.4.0/knockout-min.js"></script>
<script type="text/javascript">
(function ($) {
"use strict";
Array.prototype.shuffle = function() {
for (var index = this.length - 1; index > 0; index --) {
var rand_index = Math.floor(Math.random() * (index + 1));
var temp = this[rand_index];
this[rand_index] = this[index];
this[index] = temp;
}
return this;
}
var questions = <%- questions %>.shuffle();
var questions_length = questions.length;
var data = {
answer : ko.observable(null),
is_wrong : ko.observable(null),
index : ko.observable(0),
questions_length : questions_length
};
data.question = ko.computed(function () {
return questions[data.index()];
});
function next_question() {
if (data.index() < data.questions_length - 1) {
data.index(data.index() + 1);
} else {
data.index(0);
}
data.is_wrong(null);
data.answer(null);
}
$(document).ready(function () {
ko.applyBindings(data, $(".data").get(0));
})
.bind("keypress", function (event) {
var key = event.which;
if (key >= 48 && key <= 57 && data.answer() == null) {
data.answer(key - 48);
data.is_wrong(null);
event.preventDefault();
}
})
.bind("keydown", function (event) {
var key = event.which;
if (key == 8) {
data.answer(null);
data.is_wrong(null);
event.preventDefault();
} else if (key == 13) {
var is_wrong = data.is_wrong();
if (is_wrong == null) {
if (data.answer() == null) {
next_question();
} else if (data.answer() == data.question().answer) {
data.is_wrong(false);
} else {
data.is_wrong(true);
}
} else if (!is_wrong) {
next_question();
}
event.preventDefault();
}
});
}) (jQuery);
</script>
<style type="text/css">
.label h2 {
font-size : 1.2em;
}
.question {
display : table;
}
.question .description,
.question .answer {
display : table-cell;
vertical-align : top;
}
.question .description.empty {
display : none;
}
.question .description {
padding : 0 2em 2em 0;
}
.question .description .description_images {
margin : 0;
padding : 0;
list-style-type : none;
}
.question .answer {
padding-top : 1em;
}
.question .answer .variants {
margin : 0;
padding : 0;
list-style-type : none;
}
.question .answer .variants .variant {
line-height : 1.5em;
}
.question .answer .answer_number {
display : inline-block;
margin : 0.5em;
padding : 0.5em;
font-weight : normal;
}
.question .answer .answer_number.is_right {
background-color : #a3cb51;
}
.question .answer .answer_number.is_wrong {
background-color : #fe4042;
color : white;
}
.question_hint {
margin : 0;
}
.hint {
opacity : 0.5;
margin-top : 2em;
font-size : 0.7em;
}
</style>
</head>
<body>
<div class="data">
<div class="label">
<span data-bind="text : (index() + 1) + '/' + questions_length"></span>
<h2 data-bind="text : question().text"></h2>
</div>
<div class="question">
<div class="description" data-bind="css : { 'empty' : question().description_images.length == 0 && $.trim(question().description) == '' }">
<ul class="description_images" data-bind="foreach : question().description_images">
<li>
<img data-bind="attr : { 'src' : $data }" />
</li>
</ul>
<span data-bind="text : question().description"></span>
</div>
<div class="answer">
<ul class="variants" data-bind="foreach : question().variants">
<li class="variant" data-bind="text : $data"></li>
</ul>
<h4 class="answer_number" data-bind="css : { 'is_right' : is_wrong() == false, 'is_wrong' : is_wrong() == true }">
<span>Ответ: </span>
<span data-bind="text : answer"></span>
</h4>
</div>
</div>
<p class="question_hint">
<span>Подсказка: </span>
<span data-bind="html : question().hint"></span>
</p>
<p class="hint">
Ответить или пропустить вопрос - "Enter", удалить ответ - "Backspace"
</p>
</div>
</body>
</html>
(function () {
/*
md5.js
Yi-Cyuan Chen 2014-2015
@license MIT
*/
(function(n) {
function l(a) {
a ? (k[0] = k[16] = k[1] = k[2] = k[3] = k[4] = k[5] = k[6] = k[7] = k[8] = k[9] = k[10] = k[11] = k[12] = k[13] = k[14] = k[15] = 0, this.blocks = k, this.buffer8 = r) : p ? (a = new ArrayBuffer(68), this.buffer8 = new Uint8Array(a), this.blocks = new Uint32Array(a)) : this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
this.h0 = this.h1 = this.h2 = this.h3 = this.start = this.bytes = 0;
this.finalized = this.hashed = !1;
this.first = !0;
}
var t = "object" == typeof process && process.versions && process.versions.node;
t && (n = global);
var x = !n.JS_MD5_TEST && "object" == typeof module && module.exports, y = "function" == typeof define && define.amd, p = !n.JS_MD5_TEST && "undefined" != typeof ArrayBuffer, g = "0123456789abcdef".split(""), z = [128, 32768, 8388608, -2147483648], m = [0, 8, 16, 24], u = ["hex", "array", "digest", "buffer"], k = [], r;
if (p) {
var v = new ArrayBuffer(68);
r = new Uint8Array(v);
k = new Uint32Array(v);
}
var w = function(a) {
return function(d) {
return (new l(!0)).update(d)[a]();
};
}, A = function(a) {
var d, b;
try {
if (n.JS_MD5_TEST) {
throw "JS_MD5_TEST";
}
d = require("crypto");
b = require("buffer").Buffer;
} catch (e) {
return console.log(e), a;
}
var c = function(e) {
if ("string" == typeof e) {
return e.length <= c.utf8Threshold || e.length <= c.asciiThreshold && !/[^\x00-\x7F]/.test(e) ? a(e) : d.createHash("md5").update(e, "utf8").digest("hex");
}
if (e.constructor == ArrayBuffer) {
e = new Uint8Array(e);
} else {
if (void 0 === e.length || e.length <= c.bytesThreshold) {
return a(e);
}
}
return d.createHash("md5").update(new b(e)).digest("hex");
};
c.utf8Threshold = 18;
c.asciiThreshold = 60;
c.bytesThreshold = 245;
return c;
};
l.prototype.update = function(a) {
if (!this.finalized) {
var d = "string" != typeof a;
d && a.constructor == n.ArrayBuffer && (a = new Uint8Array(a));
for (var b, c = 0, e, f = a.length || 0, h = this.blocks, g = this.buffer8;c < f;) {
this.hashed && (this.hashed = !1, h[0] = h[16], h[16] = h[1] = h[2] = h[3] = h[4] = h[5] = h[6] = h[7] = h[8] = h[9] = h[10] = h[11] = h[12] = h[13] = h[14] = h[15] = 0);
if (d) {
if (p) {
for (e = this.start;c < f && 64 > e;++c) {
g[e++] = a[c];
}
} else {
for (e = this.start;c < f && 64 > e;++c) {
h[e >> 2] |= a[c] << m[e++ & 3];
}
}
} else {
if (p) {
for (e = this.start;c < f && 64 > e;++c) {
b = a.charCodeAt(c), 128 > b ? g[e++] = b : (2048 > b ? g[e++] = 192 | b >> 6 : (55296 > b || 57344 <= b ? g[e++] = 224 | b >> 12 : (b = 65536 + ((b & 1023) << 10 | a.charCodeAt(++c) & 1023), g[e++] = 240 | b >> 18, g[e++] = 128 | b >> 12 & 63), g[e++] = 128 | b >> 6 & 63), g[e++] = 128 | b & 63);
}
} else {
for (e = this.start;c < f && 64 > e;++c) {
b = a.charCodeAt(c), 128 > b ? h[e >> 2] |= b << m[e++ & 3] : (2048 > b ? h[e >> 2] |= (192 | b >> 6) << m[e++ & 3] : (55296 > b || 57344 <= b ? h[e >> 2] |= (224 | b >> 12) << m[e++ & 3] : (b = 65536 + ((b & 1023) << 10 | a.charCodeAt(++c) & 1023), h[e >> 2] |= (240 | b >> 18) << m[e++ & 3], h[e >> 2] |= (128 | b >> 12 & 63) << m[e++ & 3]), h[e >> 2] |= (128 | b >> 6 & 63) << m[e++ & 3]), h[e >> 2] |= (128 | b & 63) << m[e++ & 3]);
}
}
}
this.lastByteIndex = e;
this.bytes += e - this.start;
64 <= e ? (this.start = e - 64, this.hash(), this.hashed = !0) : this.start = e;
}
return this;
}
};
l.prototype.finalize = function() {
if (!this.finalized) {
this.finalized = !0;
var a = this.blocks, d = this.lastByteIndex;
a[d >> 2] |= z[d & 3];
56 <= d && (this.hashed || this.hash(), a[0] = a[16], a[16] = a[1] = a[2] = a[3] = a[4] = a[5] = a[6] = a[7] = a[8] = a[9] = a[10] = a[11] = a[12] = a[13] = a[14] = a[15] = 0);
a[14] = this.bytes << 3;
this.hash();
}
};
l.prototype.hash = function() {
var a, d, b, c, e, f = this.blocks;
this.first ? (a = f[0] - 680876937, a = (a << 7 | a >>> 25) - 271733879 << 0, c = (-1732584194 ^ a & 2004318071) + f[1] - 117830708, c = (c << 12 | c >>> 20) + a << 0, b = (-271733879 ^ c & (a ^ -271733879)) + f[2] - 1126478375, b = (b << 17 | b >>> 15) + c << 0, d = (a ^ b & (c ^ a)) + f[3] - 1316259209) : (a = this.h0, d = this.h1, b = this.h2, c = this.h3, a += (c ^ d & (b ^ c)) + f[0] - 680876936, a = (a << 7 | a >>> 25) + d << 0, c += (b ^ a & (d ^ b)) + f[1] - 389564586, c = (c << 12 |
c >>> 20) + a << 0, b += (d ^ c & (a ^ d)) + f[2] + 606105819, b = (b << 17 | b >>> 15) + c << 0, d += (a ^ b & (c ^ a)) + f[3] - 1044525330);
d = (d << 22 | d >>> 10) + b << 0;
a += (c ^ d & (b ^ c)) + f[4] - 176418897;
a = (a << 7 | a >>> 25) + d << 0;
c += (b ^ a & (d ^ b)) + f[5] + 1200080426;
c = (c << 12 | c >>> 20) + a << 0;
b += (d ^ c & (a ^ d)) + f[6] - 1473231341;
b = (b << 17 | b >>> 15) + c << 0;
d += (a ^ b & (c ^ a)) + f[7] - 45705983;
d = (d << 22 | d >>> 10) + b << 0;
a += (c ^ d & (b ^ c)) + f[8] + 1770035416;
a = (a << 7 | a >>> 25) + d << 0;
c += (b ^ a & (d ^ b)) + f[9] - 1958414417;
c = (c << 12 | c >>> 20) + a << 0;
b += (d ^ c & (a ^ d)) + f[10] - 42063;
b = (b << 17 | b >>> 15) + c << 0;
d += (a ^ b & (c ^ a)) + f[11] - 1990404162;
d = (d << 22 | d >>> 10) + b << 0;
a += (c ^ d & (b ^ c)) + f[12] + 1804603682;
a = (a << 7 | a >>> 25) + d << 0;
c += (b ^ a & (d ^ b)) + f[13] - 40341101;
c = (c << 12 | c >>> 20) + a << 0;
b += (d ^ c & (a ^ d)) + f[14] - 1502002290;
b = (b << 17 | b >>> 15) + c << 0;
d += (a ^ b & (c ^ a)) + f[15] + 1236535329;
d = (d << 22 | d >>> 10) + b << 0;
a += (b ^ c & (d ^ b)) + f[1] - 165796510;
a = (a << 5 | a >>> 27) + d << 0;
c += (d ^ b & (a ^ d)) + f[6] - 1069501632;
c = (c << 9 | c >>> 23) + a << 0;
b += (a ^ d & (c ^ a)) + f[11] + 643717713;
b = (b << 14 | b >>> 18) + c << 0;
d += (c ^ a & (b ^ c)) + f[0] - 373897302;
d = (d << 20 | d >>> 12) + b << 0;
a += (b ^ c & (d ^ b)) + f[5] - 701558691;
a = (a << 5 | a >>> 27) + d << 0;
c += (d ^ b & (a ^ d)) + f[10] + 38016083;
c = (c << 9 | c >>> 23) + a << 0;
b += (a ^ d & (c ^ a)) + f[15] - 660478335;
b = (b << 14 | b >>> 18) + c << 0;
d += (c ^ a & (b ^ c)) + f[4] - 405537848;
d = (d << 20 | d >>> 12) + b << 0;
a += (b ^ c & (d ^ b)) + f[9] + 568446438;
a = (a << 5 | a >>> 27) + d << 0;
c += (d ^ b & (a ^ d)) + f[14] - 1019803690;
c = (c << 9 | c >>> 23) + a << 0;
b += (a ^ d & (c ^ a)) + f[3] - 187363961;
b = (b << 14 | b >>> 18) + c << 0;
d += (c ^ a & (b ^ c)) + f[8] + 1163531501;
d = (d << 20 | d >>> 12) + b << 0;
a += (b ^ c & (d ^ b)) + f[13] - 1444681467;
a = (a << 5 | a >>> 27) + d << 0;
c += (d ^ b & (a ^ d)) + f[2] - 51403784;
c = (c << 9 | c >>> 23) + a << 0;
b += (a ^ d & (c ^ a)) + f[7] + 1735328473;
b = (b << 14 | b >>> 18) + c << 0;
d += (c ^ a & (b ^ c)) + f[12] - 1926607734;
d = (d << 20 | d >>> 12) + b << 0;
e = d ^ b;
a += (e ^ c) + f[5] - 378558;
a = (a << 4 | a >>> 28) + d << 0;
c += (e ^ a) + f[8] - 2022574463;
c = (c << 11 | c >>> 21) + a << 0;
e = c ^ a;
b += (e ^ d) + f[11] + 1839030562;
b = (b << 16 | b >>> 16) + c << 0;
d += (e ^ b) + f[14] - 35309556;
d = (d << 23 | d >>> 9) + b << 0;
e = d ^ b;
a += (e ^ c) + f[1] - 1530992060;
a = (a << 4 | a >>> 28) + d << 0;
c += (e ^ a) + f[4] + 1272893353;
c = (c << 11 | c >>> 21) + a << 0;
e = c ^ a;
b += (e ^ d) + f[7] - 155497632;
b = (b << 16 | b >>> 16) + c << 0;
d += (e ^ b) + f[10] - 1094730640;
d = (d << 23 | d >>> 9) + b << 0;
e = d ^ b;
a += (e ^ c) + f[13] + 681279174;
a = (a << 4 | a >>> 28) + d << 0;
c += (e ^ a) + f[0] - 358537222;
c = (c << 11 | c >>> 21) + a << 0;
e = c ^ a;
b += (e ^ d) + f[3] - 722521979;
b = (b << 16 | b >>> 16) + c << 0;
d += (e ^ b) + f[6] + 76029189;
d = (d << 23 | d >>> 9) + b << 0;
e = d ^ b;
a += (e ^ c) + f[9] - 640364487;
a = (a << 4 | a >>> 28) + d << 0;
c += (e ^ a) + f[12] - 421815835;
c = (c << 11 | c >>> 21) + a << 0;
e = c ^ a;
b += (e ^ d) + f[15] + 530742520;
b = (b << 16 | b >>> 16) + c << 0;
d += (e ^ b) + f[2] - 995338651;
d = (d << 23 | d >>> 9) + b << 0;
a += (b ^ (d | ~c)) + f[0] - 198630844;
a = (a << 6 | a >>> 26) + d << 0;
c += (d ^ (a | ~b)) + f[7] + 1126891415;
c = (c << 10 | c >>> 22) + a << 0;
b += (a ^ (c | ~d)) + f[14] - 1416354905;
b = (b << 15 | b >>> 17) + c << 0;
d += (c ^ (b | ~a)) + f[5] - 57434055;
d = (d << 21 | d >>> 11) + b << 0;
a += (b ^ (d | ~c)) + f[12] + 1700485571;
a = (a << 6 | a >>> 26) + d << 0;
c += (d ^ (a | ~b)) + f[3] - 1894986606;
c = (c << 10 | c >>> 22) + a << 0;
b += (a ^ (c | ~d)) + f[10] - 1051523;
b = (b << 15 | b >>> 17) + c << 0;
d += (c ^ (b | ~a)) + f[1] - 2054922799;
d = (d << 21 | d >>> 11) + b << 0;
a += (b ^ (d | ~c)) + f[8] + 1873313359;
a = (a << 6 | a >>> 26) + d << 0;
c += (d ^ (a | ~b)) + f[15] - 30611744;
c = (c << 10 | c >>> 22) + a << 0;
b += (a ^ (c | ~d)) + f[6] - 1560198380;
b = (b << 15 | b >>> 17) + c << 0;
d += (c ^ (b | ~a)) + f[13] + 1309151649;
d = (d << 21 | d >>> 11) + b << 0;
a += (b ^ (d | ~c)) + f[4] - 145523070;
a = (a << 6 | a >>> 26) + d << 0;
c += (d ^ (a | ~b)) + f[11] - 1120210379;
c = (c << 10 | c >>> 22) + a << 0;
b += (a ^ (c | ~d)) + f[2] + 718787259;
b = (b << 15 | b >>> 17) + c << 0;
d += (c ^ (b | ~a)) + f[9] - 343485551;
d = (d << 21 | d >>> 11) + b << 0;
this.first ? (this.h0 = a + 1732584193 << 0, this.h1 = d - 271733879 << 0, this.h2 = b - 1732584194 << 0, this.h3 = c + 271733878 << 0, this.first = !1) : (this.h0 = this.h0 + a << 0, this.h1 = this.h1 + d << 0, this.h2 = this.h2 + b << 0, this.h3 = this.h3 + c << 0);
};
l.prototype.hex = function() {
this.finalize();
var a = this.h0, d = this.h1, b = this.h2, c = this.h3;
return g[a >> 4 & 15] + g[a & 15] + g[a >> 12 & 15] + g[a >> 8 & 15] + g[a >> 20 & 15] + g[a >> 16 & 15] + g[a >> 28 & 15] + g[a >> 24 & 15] + g[d >> 4 & 15] + g[d & 15] + g[d >> 12 & 15] + g[d >> 8 & 15] + g[d >> 20 & 15] + g[d >> 16 & 15] + g[d >> 28 & 15] + g[d >> 24 & 15] + g[b >> 4 & 15] + g[b & 15] + g[b >> 12 & 15] + g[b >> 8 & 15] + g[b >> 20 & 15] + g[b >> 16 & 15] + g[b >> 28 & 15] + g[b >> 24 & 15] + g[c >> 4 & 15] + g[c & 15] + g[c >> 12 & 15] + g[c >> 8 & 15] + g[c >> 20 & 15] +
g[c >> 16 & 15] + g[c >> 28 & 15] + g[c >> 24 & 15];
};
l.prototype.toString = l.prototype.hex;
l.prototype.digest = function() {
this.finalize();
var a = this.h0, d = this.h1, b = this.h2, c = this.h3;
return [a & 255, a >> 8 & 255, a >> 16 & 255, a >> 24 & 255, d & 255, d >> 8 & 255, d >> 16 & 255, d >> 24 & 255, b & 255, b >> 8 & 255, b >> 16 & 255, b >> 24 & 255, c & 255, c >> 8 & 255, c >> 16 & 255, c >> 24 & 255];
};
l.prototype.array = l.prototype.digest;
l.prototype.buffer = function() {
this.finalize();
var a = new ArrayBuffer(16), d = new Uint32Array(a);
d[0] = this.h0;
d[1] = this.h1;
d[2] = this.h2;
d[3] = this.h3;
return a;
};
var q = function() {
var a = w("hex");
t && (a = A(a));
a.create = function() {
return new l;
};
a.update = function(b) {
return a.create().update(b);
};
for (var d = 0;d < u.length;++d) {
var b = u[d];
a[b] = w(b);
}
return a;
}();
x ? module.exports = q : (n.md5 = q, y && define(function() {
return q;
}));
})(this);
}) ();
(function ($) {
"use strict";
function flow_recursive_series (functions, success, error) {
var next_function = functions.shift();
next_function(
function () {
if (functions.length == 0) {
if (success != null) {
success();
}
} else {
flow_recursive_series(functions, success, error);
}
}, function () {
if (error != null) {
error();
}
}
);
}
function flow_series (functions, success, error) {
if (functions.length == 0) {
if (success != null) {
success();
}
} else {
flow_recursive_series(functions, success, error);
}
}
function get_random_number (min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
function logout(timeout, success, error) {
$.ajax({
url : "http://pdd.by/tasks/o/",
data : {
meth : "exit"
},
timeout : timeout,
success : function (data) {
if ($.isFunction(success)) {
success();
}
},
error : function () {
console.error("can't logout");
if ($.isFunction(error)) {
error();
}
}
});
}
function parse_questions_list(timeout, type, success, error) {
$.ajax({
url : "http://pdd.by/tasks/o/",
data : {
meth : "start",
extype : type
},
dataType : "json",
timeout : timeout,
success : function (data) {
var tickets = data.tickets;
if (tickets == null) {
console.error("no tickets found");
if ($.isFunction(error)) {
error();
}
return;
}
var series = [];
for (var index = 0; index < tickets.length; index ++) {
(function (index) {
series.push(function (success, error) {
parse_question (timeout, index, success, error);
});
}) (index + 1);
}
flow_series(
series,
function () {
console.log("test is passed");
if ($.isFunction(success)) {
success();
}
},
function () {
if ($.isFunction(error)) {
error();
}
});
},
error : function () {
console.error("can't get questions list");
if ($.isFunction(error)) {
error();
}
}
});
}
function parse_question (timeout, index, success, error) {
$.ajax({
url : "http://pdd.by/tasks/o/",
data : {
meth : "select",
n : index
},
dataType : "json",
timeout : timeout,
success : function (data) {
var ticket = data.ticket;
if (ticket == null) {
console.error("no ticket data found");
if ($.isFunction(error)) {
error();
}
return;
}
ticket = $(ticket);
var question = ticket.find(".b-question");
var raw_question = $.trim(question.html());
var variants = ticket.find(".b-variants ul");
var raw_variants = $.trim(variants.html());
var variants_count = variants.find("li").size();
variants.remove();
var description = ticket.find(".b-variants");
var raw_description = $.trim(description.html());
if (get_question_index(raw_question, raw_description) != null) {
console.log("question is known");
if ($.isFunction(success)) {
success();
}
return;
}
get_question_answer (timeout, variants_count, function (answer, hint) {
store_question(raw_question, raw_description, raw_variants, variants_count, answer, hint);
console.log("new question is stored");
if ($.isFunction(success)) {
success();
}
}, error);
},
error : function () {
console.error("can't get question");
if ($.isFunction(error)) {
error();
}
}
});
}
function get_question_index(raw_question, raw_description) {
var questions_hash = localStorage.getItem("pdd_by_questions_hash");
if (questions_hash == null) {
return null;
}
questions_hash = JSON.parse(questions_hash);
return questions_hash[md5(raw_question + raw_description)];
}
function store_question(raw_question, raw_description, raw_variants, variants_count, answer, hint) {
var questions = localStorage.getItem("pdd_by_questions");
if (questions == null) {
questions = [];
} else {
questions = JSON.parse(questions);
}
questions.push({
q : raw_question,
d : raw_description,
v : raw_variants,
vc : variants_count,
a : answer,
h : hint
});
localStorage.setItem("pdd_by_questions", JSON.stringify(questions));
var questions_hash = localStorage.getItem("pdd_by_questions_hash");
if (questions_hash == null) {
questions_hash = {};
} else {
questions_hash = JSON.parse(questions_hash);
}
questions_hash[md5(raw_question + raw_description)] = questions.length - 1;
localStorage.setItem("pdd_by_questions_hash", JSON.stringify(questions_hash));
}
function get_question_answer (timeout, variants_count, success, error) {
$.ajax({
url : "http://pdd.by/tasks/o/",
data : {
meth : "set",
n : get_random_number(1, variants_count)
},
dataType : "json",
timeout : timeout,
success : function (data) {
var answer = data.answ;
if (answer == null) {
console.error("no answer data found");
if ($.isFunction(error)) {
error();
}
return;
}
answer = parseInt(answer, 10);
if (isNaN(answer)) {
console.error("answer is invalid");
if ($.isFunction(error)) {
error();
}
return;
}
if ($.isFunction(success)) {
success(answer, data.hint);
}
},
error : function () {
console.error("can't answer question");
if ($.isFunction(error)) {
error();
}
}
});
}
var timeout = 1000;
function handler() {
setTimeout(function () {
process();
}, 11000);
}
function process () {
logout(timeout, function () {
parse_questions_list(timeout, 0, handler, handler);
}, handler);
}
process();
}) (jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment