11var request = require ( 'request-promise' ) ;
22var cheerio = require ( 'cheerio' ) ;
3+ var { getId, getElement, getDate, sanatizer } = require ( './helpers' ) ;
4+ var { effectParse, recipeParse, descriptionParse } = require ( './parsing-service/helpers' ) ;
35
46var requestOpts = {
57 url : '' ,
68 transform : function ( body ) {
79 return cheerio . load ( body ) ;
810 }
911} ;
12+ var item = { } ;
13+ var body = '' ;
1014
1115var getConsumables = exports . getConsumables = function ( url ) {
1216 requestOpts . url = url ;
1317 return request ( requestOpts ) . then ( function ( $ ) {
14- var itemId = url . replace ( / \D / g, '' ) ;
15- var type = $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.ak-encyclo-detail-type.col-xs-6' ) . find ( 'span' ) . text ( ) . trim ( ) ;
16- var name = $ ( 'h1.ak-return-link' ) . text ( ) . trim ( ) ;
17- var description = $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.ak-container.ak-panel' ) . first ( ) . find ( 'div.ak-panel-content' ) . text ( ) . trim ( ) ;
18- var lvl = $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.ak-encyclo-detail-level.col-xs-6.text-right' ) . text ( ) . trim ( ) . replace ( / \D / g, '' ) ;
19- var imgUrl = $ ( 'div.ak-encyclo-detail-illu' ) . find ( 'img' ) . attr ( 'src' ) . replace ( 'dofus/ng/img/../../../' , '' ) ;
18+ /////// Global initializations ///////
19+ body = $ . html ( ) ;
2020
21- var item = {
22- item_identifiant : itemId ,
23- name : name ,
24- description : description ,
25- lvl : lvl ,
26- type : type ,
27- imgUrl : imgUrl ,
28- url : url
29- }
21+ /////// Description parse ///////
22+ item = descriptionParse ( body , url ) ;
3023
31- item [ "effect" ] = [ ] ;
24+ /////// Tabs initializations ///////
25+ item [ "stats" ] = [ ] ;
3226 item [ "condition" ] = [ ] ;
3327 item [ "recipe" ] = [ ] ;
3428
35- if ( $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.col-sm-6' ) . eq ( 0 ) . find ( 'div.ak-container.ak-panel' ) . eq ( 0 ) . text ( ) !== '' ) {
36- $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.col-sm-6' ) . eq ( 0 ) . find ( 'div.ak-container.ak-panel' ) . eq ( 0 ) . find ( 'div.ak-list-element' ) . each ( function ( i , element ) {
37- var stat = $ ( this ) . find ( "div.ak-title" ) . text ( ) . trim ( ) ;
38- var element = stat . replace ( / d e | à | [ ( ) ] | \+ | \- | t o | a n d | ( - ? \d [ \d \. ] * ) / gi, '' ) . trim ( ) ;
39- element = element . charAt ( 0 ) . toUpperCase ( ) + element . slice ( 1 ) ;
40- var numbers = [ ] ;
41- stat . replace ( / ( - ? \d [ \d \. ] * ) / g, function ( x ) {
42- var n = Number ( x ) ; if ( x == n ) { numbers . push ( x ) ; }
43- } ) ;
44- var groupeElement = { [ element ] : numbers [ 0 ] } ;
45- item [ "effect" ] . push ( groupeElement ) ;
46-
47- } ) ;
48- }
49-
50- if ( $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.col-sm-6' ) . eq ( 1 ) . find ( 'div.ak-container.ak-panel.no-padding' ) . eq ( 0 ) . text ( ) !== '' ) {
51- $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.col-sm-6' ) . eq ( 1 ) . find ( 'div.ak-container.ak-panel.no-padding' ) . eq ( 0 ) . find ( 'div.ak-list-element' ) . each ( function ( i , element ) {
52- var condition = $ ( this ) . find ( "div.ak-title" ) . remove ( "br" ) . text ( ) . trim ( ) ;
53- var conditionTab = condition . split ( 'et\n' ) ;
54- item [ "condition" ] = conditionTab ;
29+ /////// Effects & condtions parse ///////
30+ var $akContainer = $ ( 'div.ak-encyclo-detail-right.ak-nocontentpadding' ) . find ( 'div.ak-container.ak-panel' ) ;
31+ if ( typeof $akContainer . eq ( 1 ) !== 'undefined' ) {
32+ $akContainer . eq ( 1 ) . find ( 'div.col-sm-6' ) . each ( function ( i , element ) {
33+ var infoCategory = $ ( this ) . find ( 'div.ak-panel-title' ) . text ( ) . trim ( ) . toLowerCase ( ) ;
34+ categorySwitch ( infoCategory , $ ( this ) . html ( ) ) ;
5535 } ) ;
5636 }
5737
38+ /////// Recipes parse ///////
5839 if ( typeof $ ( 'div.ak-container.ak-panel.ak-crafts' ) !== 'undefined' ) {
59- $ ( 'div.ak-container.ak-panel.ak-crafts' ) . find ( 'div.ak-panel-content' ) . find ( 'div.ak-container.ak-content-list' ) . find ( 'div.ak-column' ) . each ( function ( i , element ) {
60- var setUrl = 'https://www.dofus-touch.com' + $ ( this ) . find ( 'div.ak-title' ) . find ( 'a' ) . attr ( 'href' ) ;
61- var setId = $ ( this ) . find ( 'div.ak-title' ) . find ( 'a' ) . attr ( 'href' ) . replace ( / \D / g, '' ) ;
62- var setImage = $ ( this ) . find ( 'div.ak-image' ) . find ( 'a' ) . find ( 'span.ak-linker' ) . find ( 'img' ) . attr ( 'src' ) . replace ( 'dofus/ng/img/../../../' , '' ) ;
63- var setQuantity = $ ( this ) . find ( 'div.ak-front' ) . text ( ) . replace ( / \x / g, '' ) . trim ( ) ;
64- var setName = $ ( this ) . find ( 'div.ak-content' ) . find ( 'div.ak-title' ) . find ( 'a' ) . find ( 'span.ak-linker' ) . text ( ) . trim ( ) ;
65- var setType = $ ( this ) . find ( 'div.ak-content' ) . find ( 'div.ak-text' ) . text ( ) . trim ( ) ;
66- var setLvl = $ ( this ) . find ( 'div.ak-aside' ) . text ( ) . replace ( / \D / g, '' ) . trim ( ) ;
67-
68- var groupeElement = { [ setName ] : {
69- 'id' : setId ,
70- 'name' : setName ,
71- 'url' : setUrl ,
72- 'imgUrl' : setImage ,
73- 'type' : setType ,
74- 'lvl' : setLvl ,
75- 'quantity' : setQuantity
76- } } ;
77- item [ "recipe" ] . push ( groupeElement ) ;
78- } ) ;
40+ item [ "recipe" ] = recipeParse ( body ) ; ;
7941 }
80-
8142 return item ;
8243 } ) ;
44+ }
45+
46+ function conditionParse ( body ) {
47+ var $ = cheerio . load ( body ) ;
48+ var condition = $ ( 'div.ak-container.ak-panel.no-padding' ) . find ( 'div.ak-list-element' ) . find ( "div.ak-title" ) . remove ( "br" ) . text ( ) . trim ( ) ;
49+ condition = sanatizer ( condition ) ;
50+ var conditionTab = condition . split ( 'et' ) ;
51+ conditionTab = conditionTab . map ( function ( string ) {
52+ return sanatizer ( string ) . trim ( ) ;
53+ } ) ;
54+ item [ "condition" ] = conditionTab ;
55+ }
56+
57+ function categorySwitch ( infoCategory , body ) {
58+ var $ = cheerio . load ( body ) ;
59+ switch ( infoCategory ) {
60+ case 'effets' :
61+ item [ "stats" ] = effectParse ( body ) ;
62+ break ;
63+ case 'effects' :
64+ item [ "stats" ] = effectParse ( body ) ;
65+ break ;
66+ case 'conditions' :
67+ conditionParse ( body ) ;
68+ break ;
69+ console . log ( 'Sorry, we are out of ' + infoCategory + '.' ) ;
70+ }
8371}
0 commit comments