1
+ exports . action = function ( data , callback , config , SARAH ) {
2
+
3
+ var place = data . place || 'B0172' ;
4
+
5
+ var url = 'http://iphone.allocine.fr/salle/seances_gen_csalle=' + place + '.html' ;
6
+ var request = require ( 'request' ) ;
7
+ request ( { 'uri' : url } , function ( err , response , body ) {
8
+
9
+ if ( err || response . statusCode != 200 ) {
10
+ callback ( { 'tts' : "L'action a échoué" } ) ;
11
+ return ;
12
+ }
13
+
14
+ var $ = require ( 'cheerio' ) . load ( body , { xmlMode : true , ignoreWhitespace : false , lowerCaseTags : false } ) ;
15
+ var options = list ( $ ) ;
16
+ options . tts = 'Voici la liste des films au ' + options . theatre + ': ' + options . movies . join ( ', ' ) ;
17
+
18
+ if ( data . movie ) {
19
+ options . hours = hours ( $ , data . movie ) ;
20
+ options . tts = 'Voici les horaries de ' + options . movies [ data . movie ] + ' au ' + options . theatre + ': ' ;
21
+ options . tts += options . hours . join ( '. ' ) ;
22
+ } else {
23
+ update ( data . directory , options . movies , place ) ;
24
+ }
25
+
26
+ callback ( options ) ;
27
+ } ) ;
28
+ }
29
+
30
+
31
+ // ------------------------------------------
32
+ // SCRAPING
33
+ // ------------------------------------------
34
+
35
+ var list = function ( $ ) {
36
+ var theatre = $ ( 'DIV.titre B' ) . text ( ) ;
37
+ var movies = $ ( 'DIV.cell A[href^="/film"]' ) . map ( function ( ) { return $ ( this ) . text ( ) ; } ) ;
38
+ return {
39
+ 'theatre' : theatre ,
40
+ 'movies' : movies
41
+ } ;
42
+ }
43
+
44
+ var hours = function ( $ , pos ) {
45
+ return $ ( 'DIV.cell' ) . eq ( pos ) . find ( 'DIV[style*=red]' ) . map ( function ( ) { return clean ( $ ( this ) . text ( ) ) ; } ) ;
46
+ }
47
+
48
+ var clean = function ( hours ) {
49
+ hours = hours . replace ( / < b r > / g, '. ' ) . replace ( / L u n [ - , ] * / g, 'Lundi ' )
50
+ . replace ( / M a r [ - , ] * / g, 'Mardi ' ) . replace ( / M e r [ - , ] * / g, 'Mercredi ' )
51
+ . replace ( / J e u [ - , ] * / g, 'Jeudi ' ) . replace ( / V e n [ - , ] * / g, 'Vendredi ' )
52
+ . replace ( / S a m [ - , ] * / g, 'Samedi ' ) . replace ( / D i m [ - , ] * / g, 'Dimanche ' ) ;
53
+ return hours ;
54
+ }
55
+
56
+ // ------------------------------------------
57
+ // UPDATING XML
58
+ // ------------------------------------------
59
+
60
+ var update = function ( directory , movies , place ) {
61
+ if ( ! directory ) { return ; }
62
+ if ( ! movies || movies . length == 0 ) { return ; }
63
+
64
+ var fs = require ( 'fs' ) ;
65
+ var file = directory + '/../plugins/allocine/allocine.xml' ;
66
+ var xml = fs . readFileSync ( file , 'utf8' ) ;
67
+
68
+ var replace = '§ -->\n' ;
69
+ replace += '<rule id="ruleMovieName">\n' ;
70
+ replace += ' <tag>out.place="' + place + '";</tag>\n' ;
71
+ replace += ' <one-of>\n' ;
72
+
73
+ for ( var i = 0 ; i < movies . length ; i ++ ) {
74
+ var movie = movies [ i ] ; movie = movie . indexOf ( ':' ) > 0 ? movie . substring ( 0 , movie . indexOf ( ':' ) ) : movie ; // Split at ':'
75
+ replace += ' <item>' + movie + '<tag>out.movie="' + i + '";</tag></item>\n' ;
76
+ }
77
+ replace += ' </one-of>\n' ;
78
+ replace += '</rule>\n' ;
79
+ replace += '<!-- §' ;
80
+
81
+ var regexp = new RegExp ( '§[^§]+§' , 'gm' ) ;
82
+ var xml = xml . replace ( regexp , replace ) ;
83
+
84
+ fs . writeFileSync ( file , xml , 'utf8' ) ;
85
+ }
86
+
0 commit comments