1- const jsdom = require ( 'jsdom' ) ;
2- const argv = require ( 'yargs' ) . argv ;
3- const fs = require ( 'fs' ) ;
4- const _ = require ( 'lodash' ) ;
5- const request = require ( 'request' ) ;
6- const jsonfile = require ( 'jsonfile' ) ;
7- const logger = require ( " ./utils/logger" ) ;
1+ import jsdom from 'jsdom' ;
2+ import yargs from 'yargs' ;
3+ import fs from 'fs' ;
4+ import _ from 'lodash' ;
5+ import request from 'request' ;
6+ import jsonfile from 'jsonfile' ;
7+ import logger from ' ./utils/logger' ;
88
99let multiplier = 0 ;
1010let IFTTTParams = { } ;
11- let IFTTTTimers = { } ;
12- const { endpoints, interval, ifttt, random } = argv ;
11+ const IFTTTTimers = { } ;
12+ const { endpoints, interval, ifttt, random } = yargs . argv ;
1313const options = {
14- selector : 'body :not(script) ' ,
14+ selector : 'body' ,
1515 jQuerySrc : 'http://code.jquery.com/jquery.js' ,
1616 defaultTimeout : 10 ,
1717 defaultRandom : 20
1818} ;
1919
2020// Ensure we have required arguments
21- if ( ! _ . isString ( endpoints ) || ! _ . isInteger ( interval ) || interval < 0 ) {
22- console . error ( '--endpoints and --interval are required' ) ;
21+ if ( ! _ . isString ( endpoints ) || ! _ . isInteger ( interval ) || interval < 0 ) {
22+ logger . error ( '--endpoints and --interval are required' ) ;
2323 process . exit ( 1 ) ;
2424}
2525
2626// Ensure list of endpoints is a file
27- if ( ! fs . statSync ( endpoints ) . isFile ( ) ) {
28- console . error ( '--endpoints should refer to a file (list of endpoints)' ) ;
27+ if ( ! fs . statSync ( endpoints ) . isFile ( ) ) {
28+ logger . error ( '--endpoints should refer to a file (list of endpoints)' ) ;
2929 process . exit ( 2 ) ;
3030}
3131
3232// Ensure IFTTT configuration is valid
33- if ( ifttt ) {
34- if ( ! fs . statSync ( ifttt ) . isFile ( ) ) {
35- console . error ( '--ifttt should refer to a JSON file configuration' ) ;
33+ if ( ifttt ) {
34+ if ( ! fs . statSync ( ifttt ) . isFile ( ) ) {
35+ logger . error ( '--ifttt should refer to a JSON file configuration' ) ;
3636 process . exit ( 5 ) ;
3737 }
3838
3939 const { key, eventName, bodyKey } = IFTTTParams = jsonfile . readFileSync ( ifttt ) ;
4040
41- if ( ! key || ! eventName || ! bodyKey || ! _ . isString ( key ) || ! _ . isString ( eventName ) || ! _ . isString ( bodyKey ) ) {
42- console . error ( '--ifttt file is missing required data' ) ;
41+ if ( ! _ . isString ( key ) || ! _ . isString ( eventName ) || ! _ . isString ( bodyKey ) ) {
42+ logger . error ( '--ifttt file is missing required data' ) ;
4343 process . exit ( 6 ) ;
4444 }
4545}
4646
4747// Ensure random argument is valid
48- if ( random ) {
49- if ( _ . isBoolean ( random ) ) {
48+ if ( random ) {
49+ if ( _ . isBoolean ( random ) ) {
5050 multiplier = options . defaultRandom ;
51- } else if ( ( _ . isInteger ( random ) && ( random < 0 || random > 99 ) || ! _ . isInteger ( random ) ) ) {
52- console . error ( '--random must be an integer from 0 to 99' ) ;
51+ } else if ( ( _ . isInteger ( random ) && ( random < 0 || random > 99 ) || ! _ . isInteger ( random ) ) ) {
52+ logger . error ( '--random must be an integer from 0 to 99' ) ;
5353 process . exit ( 7 ) ;
5454 } else {
5555 multiplier = random ;
@@ -58,20 +58,20 @@ if(random) {
5858
5959// Make requests to endpoints
6060const makeRequests = ( urls , callback ) => {
61- let responses = { } ;
61+ const responses = { } ;
6262 let complete = 0 ;
6363
6464 urls . forEach ( ( url ) => {
6565 jsdom . env ( {
66- url : url ,
66+ url,
6767 scripts : [ options . jQuerySrc ] ,
68- done : ( err , window ) => {
69- if ( ! window || ! window . $ || err ) {
70- console . error ( `Resource data located at ${ url } failed to load` ) ;
68+ done ( err , window ) {
69+ if ( ! window || ! window . $ || err ) {
70+ logger . error ( `Resource data located at ${ url } failed to load` ) ;
7171 } else {
7272 const $ = window . $ ;
7373
74- $ ( options . selector ) . each ( function ( ) {
74+ $ ( options . selector ) . each ( ( ) => {
7575 const responseText = $ ( this ) . text ( ) . replace ( / \W + / g, '' ) ;
7676
7777 responses [ url ] = responseText ;
@@ -80,7 +80,7 @@ const makeRequests = (urls, callback) => {
8080
8181 complete ++ ;
8282
83- if ( complete === urls . length ) {
83+ if ( complete === urls . length ) {
8484 callback ( responses ) ;
8585 }
8686 }
@@ -94,72 +94,73 @@ const postIFTTT = (data) => {
9494 const timeout = ( _ . isInteger ( IFTTTParams . timeout ) ? IFTTTParams . timeout : options . defaultTimeout ) ;
9595
9696 // Ensure enough time has passed since last time an event was dispatched
97- if ( ! IFTTTTimers [ data ] || now - IFTTTTimers [ data ] > timeout ) {
98- let postData = { } ;
97+ if ( ! IFTTTTimers [ data ] || now - IFTTTTimers [ data ] > timeout ) {
98+ const postData = { } ;
9999
100100 postData [ IFTTTParams . bodyKey ] = data ;
101101 IFTTTTimers [ data ] = now ;
102102
103103 request . post ( {
104104 url : `https://maker.ifttt.com/trigger/${ IFTTTParams . eventName } /with/key/${ IFTTTParams . key } ` ,
105105 form : postData
106- } , ( err , response ) => {
107- if ( err ) {
108- console . log ( '- IFTTT event dispatch failed' ) ;
106+ } , ( err ) => {
107+ if ( err ) {
108+ logger . info ( '- IFTTT event dispatch failed' ) ;
109109 } else {
110- console . log ( '- IFTTT event dispatched' ) ;
110+ logger . info ( '- IFTTT event dispatched' ) ;
111111 }
112112 } ) ;
113113 } else {
114- console . log ( '- IFTTT event ignored due to timeout' ) ;
114+ logger . info ( '- IFTTT event ignored due to timeout' ) ;
115115 }
116116} ;
117117
118118// Read endpoints file and create list of endpoints
119119fs . readFile ( endpoints , 'utf-8' , ( err , data ) => {
120- if ( err ) {
121- console . error ( '--endpoints file could not be read' ) ;
120+ if ( err ) {
121+ logger . error ( '--endpoints file could not be read' ) ;
122122 process . exit ( 3 ) ;
123123 }
124124
125- const endpointsList = _ . remove ( data . split ( '\n' ) , ( item ) => {
126- return _ . isString ( item ) && ! _ . isEmpty ( item ) ;
127- } ) ;
125+ let endpointsList = data . split ( '\n' ) ;
126+
127+ endpointsList = _ . filter ( endpointsList , ( item ) => _ . isString ( item ) && ! _ . isEmpty ( item ) ) ;
128128
129- if ( ! endpointsList . length ) {
130- console . error ( '--endpoints file does not contain any endpoints' ) ;
129+ if ( ! endpointsList . length ) {
130+ logger . error ( '--endpoints file does not contain any endpoints' ) ;
131131 process . exit ( 4 ) ;
132132 }
133133
134134 // Cache initial endpoint responses
135- makeRequests ( endpointsList , ( responses ) => {
136- const cache = responses ;
135+ makeRequests ( endpointsList , ( initialResponses ) => {
136+ const cache = initialResponses ;
137137
138- console . log ( `${ _ . keys ( responses ) . length } of ${ endpointsList . length } responses cached` ) ;
138+ logger . info ( `${ _ . keys ( cache ) . length } of ${ endpointsList . length } responses cached` ) ;
139139
140140 ( function loop ( ) {
141- const random = ( Math . random ( ) * multiplier ) / 100 ;
141+ // Apply randomness to timing
142+ const rand = ( Math . random ( ) * multiplier ) / 100 ;
142143 const plusOrMinus = ( Math . random ( ) < 0.5 ? - 1 : 1 ) ;
143- const timer = Math . floor ( ( interval + plusOrMinus * ( random * interval ) ) * 1000 ) ;
144+ const timer = Math . floor ( ( interval + plusOrMinus * ( rand * interval ) ) * 1000 ) ;
144145
145146 setTimeout ( ( ) => {
146147 makeRequests ( endpointsList , ( responses ) => {
147148 const differences = _ . difference ( _ . values ( responses ) , _ . values ( cache ) ) ;
148149
149- if ( differences . length ) {
150+ if ( differences . length ) {
150151 differences . forEach ( ( difference ) => {
151152 const endpoint = _ . invert ( responses ) [ difference ] ;
152153
153154 cache [ endpoint ] = difference ;
154155
155- console . log ( `Difference identified within ${ endpoint } ` ) ;
156+ logger . info ( `Difference identified within ${ endpoint } ` ) ;
156157
157- if ( ifttt ) {
158+ if ( ifttt ) {
158159 postIFTTT ( endpoint ) ;
159160 }
160161 } ) ;
161162 } else {
162- console . log ( `No differences identified for ${ _ . keys ( responses ) . length } responses` )
163+ logger . info ( `No differences identified for ${ _ . keys ( responses ) . length } responses` ) ;
163164 }
164165
165166 loop ( ) ;
0 commit comments