This repository has been archived by the owner on Sep 9, 2021. It is now read-only.
forked from dofapi/crawlit-dofus-encyclopedia-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
getSpells.js
114 lines (93 loc) · 3 KB
/
getSpells.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
const request = require('request-promise-native');
import cheerio from 'cheerio';
import requestOpts from '../services/utils';
import { getQueryString } from '../../../../services/utils';
import Spell from '../models/spell.model';
import { statsRequest } from '../services/parser-helpers';
async function getSpells(url) {
requestOpts.url = url;
const $ = await request(requestOpts);
const queryString = getQueryString(url);
const level = parseInt(queryString['level']);
if (!levelExists($, level))
return null;
const spellEntity = new Spell(spellParsingInit($, url));
spellEntity.required_level = parseRequiredLevel($);
spellEntity.imgUrl = $('.ak-spell-details-illu img').attr('src');
spellEntity.caracs.pa = parsePa($);
spellEntity.caracs.po = parsePo($);
updateCaracs($, spellEntity.caracs);
// Exctract effects
const effects_lists = $('.ak-spell-details-effects');
$(effects_lists[0]).find('.ak-list-element').each(
(i, div) => spellEntity.effects.push($(div).text().trim())
);
if (effects_lists.length > 1) {
spellEntity.effects_critical = [];
$(effects_lists[1]).find('.ak-list-element').each(
(i, div) => spellEntity.effects_critical.push($(div).text().trim())
);
}
return spellEntity;
}
function levelExists($, level) {
return $('.ak-spell-details-level-selector a').length >= level;
}
function spellParsingInit($, url) {
const name = $('.ak-spell-name').text().split('\n')[1];
const queryString = getQueryString(url);
return {
_id: null,
ankamaId: queryString.id,
name: name,
type: 'spell',
level: parseInt(queryString.level),
url: url,
description: $('.ak-spell-description').text()
};
}
function parsePa($) {
return parseInt($('.ak-spell-po-pa').text().split('/')[1].split('PA')[0]);
}
function parsePo($) {
let range = $('.ak-spell-po-pa')
.text()
.split('/')[0]
.split('PO')[0]
.split('-')
.map(x => parseInt(x));
if (range.length == 1)
range = [0, range[0]];
return range;
}
function parseRequiredLevel($) {
return parseInt($('.ak-spell-details-level .ak-selected').text());
}
const caracsConf = {
'Probabilité de coup critique': [
'critical',
x => parseInt(x.split('%')[0])
],
'Nb. de tours entre deux lancers': ['cooldown', parseInt],
'Nb. de lancers par tour': ['per_turn_cast', parseInt],
'Nb. de lancers par tour par joueur': ['per_player_turn_cast', parseInt],
'Portée modifiable': ['changeable_range', x => x == 'Oui'],
'Ligne de vue': ['line_of_sight', x => x == 'Oui'],
'Lancer en ligne': ['line_cast', x => x == 'Oui'],
'Cellules libres': ['free_cell', x => x == 'Oui'],
'Zone d\'effet': ['zone', x => x],
};
function updateCaracs($, caracs) {
$('.ak-spell-details-other .ak-main-content').each((i, div) => {
const title = $(div).find('.ak-title').text().trim();
if (caracsConf[title]) {
const key = caracsConf[title][0];
const val = caracsConf[title][1]($(div).find('.ak-aside,.ak-text').text().trim());
caracs[key] = val;
}
else {
console.log(`No reference for title '${title}'`);
}
})
}
export default getSpells;