quizz
Version:
A Simple Quizz
58 lines (57 loc) • 2.5 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var tslib_1 = require("tslib");
var axios_1 = require("axios");
var cheerio = require("cheerio");
var Question_1 = require("../Question");
var OpenQuizzDBPicker = /** @class */ (function () {
function OpenQuizzDBPicker() {
this.client = axios_1.default.create({ baseURL: "https://www.openquizzdb.org" });
}
Object.defineProperty(OpenQuizzDBPicker.prototype, "ready", {
get: function () {
return true;
},
enumerable: true,
configurable: true
});
OpenQuizzDBPicker.prototype.pickQuestion = function () {
return tslib_1.__awaiter(this, void 0, void 0, function () {
var res, html;
return tslib_1.__generator(this, function (_a) {
switch (_a.label) {
case 0: return [4 /*yield*/, this.client.get("ajax.php?flag=3&joueur=1&temp_qid=0&temp_bonus=0")];
case 1:
res = _a.sent();
html = res.data;
return [2 /*return*/, this.extractQuestionFromHtml(html)];
}
});
});
};
OpenQuizzDBPicker.prototype.extractQuestionFromHtml = function (html) {
var _this = this;
var $ = cheerio.load(html);
var category = $("#top_box > div:first-child").text();
var rawQuestion = $("strong").text();
var question = "[" + category + "] " + rawQuestion;
var answers = $('[id^="rep"]').map(function (_i, elem) {
var elem$ = cheerio.load(elem);
var anchor$ = elem$("a");
var isRightAnswer = _this.isOnclickRightAnswer(anchor$.attr("onclick"));
var choice = anchor$.text().trim().substr(1).trim();
return { answer: choice, isRightAnswer: isRightAnswer };
}).get();
var answer = answers.find(function (a) { return a.isRightAnswer; }).answer;
return new Question_1.Question(question, answer);
};
OpenQuizzDBPicker.prototype.isOnclickRightAnswer = function (onclick) {
// AfficherReponse(challenge,bonus,joueur,numero,section,niveau,wiki)
// AfficherReponse('0','20','1','1',4,'1','wiki')
var re = /AfficherReponse\((.*?)\);/;
var cbargs = re.exec(onclick)[1].split(",");
return cbargs[3] === "'" + cbargs[4] + "'";
};
return OpenQuizzDBPicker;
}());
exports.OpenQuizzDBPicker = OpenQuizzDBPicker;