@@ -9,7 +9,7 @@ module.exports = class IPGeolocationAPI {
99 }
1010
1111 getGeolocation ( callback , geolocationParams = null ) {
12- if ( geolocationParams && geolocationParams . getIPAddresses ( ) ) {
12+ if ( geolocationParams && geolocationParams . getIPAddresses ( ) ) {
1313 var jsonData = JSON . stringify ( {
1414 'ips' : geolocationParams . getIPAddresses ( )
1515 } ) ;
@@ -19,11 +19,31 @@ module.exports = class IPGeolocationAPI {
1919 getRequest ( 'ipgeo' , buildGeolocationUrlParams ( this . apiKey , geolocationParams ) , callback ) ;
2020 }
2121 }
22-
22+
2323 getTimezone ( callback , timezoneParams = null ) {
2424 return getRequest ( 'timezone' , buildTimezoneUrlParams ( this . apiKey , timezoneParams ) , callback ) ;
2525 }
2626
27+ getUserAgent ( callback , uaString = "" ) {
28+ var jsonData = "{\"uaString\":\"" + uaString + "\"}" ;
29+ postRequest ( 'user-agent' , "apiKey=" + this . apiKey , jsonData , callback ) ;
30+ }
31+
32+ getBulkUserAgent ( callback , uaStrings = [ ] ) {
33+ var jsonData = "{\"uaStrings\":[" ;
34+ for ( let i = 0 ; i < uaStrings . length ; i ++ ) {
35+ if ( i === 0 ) {
36+ jsonData = jsonData . concat ( "\"" + uaStrings [ i ] + "\"" ) ;
37+ } else {
38+ jsonData = jsonData . concat ( "," ) ;
39+ jsonData = jsonData . concat ( "\"" + uaStrings [ i ] + "\"" ) ;
40+ }
41+ }
42+ jsonData = jsonData . concat ( "]}" ) ;
43+
44+ postRequest ( 'user-agent-bulk' , "apiKey=" + this . apiKey , jsonData , callback ) ;
45+ }
46+
2747 getApiKey ( ) {
2848 return this . apiKey ;
2949 }
@@ -36,80 +56,124 @@ module.exports = class IPGeolocationAPI {
3656function buildGeolocationUrlParams ( apiKey = '' , geolocationParams = null ) {
3757 var urlParams = '' ;
3858
39- if ( apiKey ) {
59+ if ( apiKey ) {
4060 urlParams = urlParams . concat ( 'apiKey=' , apiKey ) ;
4161 }
4262
43- if ( geolocationParams ) {
44- if ( geolocationParams . getIPAddress ( ) ) {
45- if ( urlParams ) {
63+ if ( geolocationParams ) {
64+ if ( geolocationParams . getIPAddress ( ) ) {
65+ if ( urlParams ) {
4666 urlParams = urlParams . concat ( '&' ) ;
4767 }
4868
4969 urlParams = urlParams . concat ( 'ip=' , geolocationParams . getIPAddress ( ) ) ;
5070 }
51-
52- if ( geolocationParams . getFields ( ) ) {
53- if ( urlParams ) {
71+
72+ if ( geolocationParams . getFields ( ) ) {
73+ if ( urlParams ) {
5474 urlParams = urlParams . concat ( '&' ) ;
5575 }
5676
5777 urlParams = urlParams . concat ( 'fields=' , geolocationParams . getFields ( ) ) ;
5878 }
59-
60- if ( geolocationParams . getExcludes ( ) ) {
61- if ( urlParams ) {
79+
80+ if ( geolocationParams . getExcludes ( ) ) {
81+ if ( urlParams ) {
6282 urlParams = urlParams . concat ( '&' ) ;
6383 }
6484
6585 urlParams = urlParams . concat ( 'excludes=' , geolocationParams . getExcludes ( ) ) ;
6686 }
67-
68- if ( geolocationParams . getLang ( ) ) {
69- if ( urlParams ) {
87+
88+ if ( geolocationParams . getLang ( ) ) {
89+ if ( urlParams ) {
7090 urlParams = urlParams . concat ( '&' ) ;
7191 }
7292
7393 urlParams = urlParams . concat ( 'lang=' , geolocationParams . getLang ( ) ) ;
7494 }
95+
96+ if ( geolocationParams . isIncludeHostname ( ) || geolocationParams . isIncludeHostnameFallbackLive ( ) || geolocationParams . isIncludeLiveHostname ( ) || geolocationParams . isIncludeSecurity ( ) || geolocationParams . isIncludeUserAgent ( ) ) {
97+ var val = "" ;
98+ var includeHost = false ;
99+ if ( geolocationParams . isIncludeHostname ( ) ) {
100+ val = "hostname" ;
101+ includeHost = true ;
102+ } else if ( geolocationParams . isIncludeHostnameFallbackLive ( ) ) {
103+ val = "hostnameFallbackLive" ;
104+ includeHost = true ;
105+ } else if ( geolocationParams . isIncludeLiveHostname ( ) ) {
106+ val = "liveHostname" ;
107+ includeHost = true ;
108+ }
109+ if ( geolocationParams . isIncludeSecurity ( ) ) {
110+ if ( includeHost ) {
111+ val = val + ",security" ;
112+ } else {
113+ val = "security" ;
114+ }
115+ }
116+ if ( geolocationParams . isIncludeUserAgent ( ) ) {
117+ if ( includeHost || geolocationParams . isIncludeSecurity ( ) ) {
118+ val = val + ",useragent" ;
119+ } else {
120+ val = "useragent" ;
121+ }
122+ }
123+ if ( urlParams ) {
124+ urlParams = urlParams . concat ( '&' ) ;
125+ }
126+ urlParams = urlParams . concat ( 'include=' , val ) ;
127+
128+ }
129+
75130 }
76131 return urlParams ;
77132}
78133
79134function buildTimezoneUrlParams ( apiKey = '' , timezoneParams = null ) {
80135 var urlParams = '' ;
81136
82- if ( apiKey ) {
137+ if ( apiKey ) {
83138 urlParams = urlParams . concat ( 'apiKey=' , apiKey ) ;
84139 }
85140
86- if ( timezoneParams ) {
87- if ( timezoneParams . getIPAddress ( ) ) {
88- if ( urlParams ) {
141+ if ( timezoneParams ) {
142+ if ( timezoneParams . getIPAddress ( ) ) {
143+ if ( urlParams ) {
89144 urlParams = urlParams . concat ( '&' ) ;
90145 }
91146
92147 urlParams = urlParams . concat ( 'ip=' , timezoneParams . getIPAddress ( ) ) ;
93148 }
94149
95- if ( timezoneParams . getTimezone ( ) ) {
96- if ( urlParams ) {
150+ if ( timezoneParams . getTimezone ( ) ) {
151+ if ( urlParams ) {
97152 urlParams = urlParams . concat ( '&' ) ;
98153 }
99154
100155 urlParams = urlParams . concat ( 'tz=' , timezoneParams . getTimezone ( ) ) ;
101156 }
102157
103- if ( timezoneParams . getLatitude ( ) !== '1000.0' && timezoneParams . getLongitude ( ) !== '1000.0' ) {
104- if ( urlParams ) {
158+ if ( timezoneParams . getLocation ( ) ) {
159+ if ( urlParams ) {
105160 urlParams = urlParams . concat ( '&' ) ;
106161 }
107162
108- urlParams = urlParams . concat ( 'lat=' , timezoneParams . getLatitude ( ) , '&long=' , timezoneParams . getLongitude ( ) ) ;
163+ urlParams = urlParams . concat ( 'location=' , timezoneParams . getLocation ( ) ) ;
164+ }
165+
166+ if ( timezoneParams . getLatitude ( ) !== '1000.0' && timezoneParams . getLongitude ( ) !== '1000.0' ) {
167+ if ( ( timezoneParams . getLatitude ( ) >= - 90 && timezoneParams . getLatitude ( ) <= 90 ) && ( timezoneParams . getLongitude ( ) >= - 180 && timezoneParams . getLongitude ( ) <= 180 ) ) {
168+ if ( urlParams ) {
169+ urlParams = urlParams . concat ( '&' ) ;
170+ }
171+ urlParams = urlParams . concat ( 'lat=' , timezoneParams . getLatitude ( ) , '&long=' , timezoneParams . getLongitude ( ) ) ;
172+ }
109173 }
110-
111- if ( timezoneParams . getLang ( ) ) {
112- if ( urlParams ) {
174+
175+ if ( timezoneParams . getLang ( ) ) {
176+ if ( urlParams ) {
113177 urlParams = urlParams . concat ( '&' ) ;
114178 }
115179
@@ -124,17 +188,17 @@ function getRequest(subUrl, urlParams = '', callback) {
124188 var XMLHttpRequest = require ( 'xmlhttprequest' ) . XMLHttpRequest ;
125189 var xhr = new XMLHttpRequest ( ) ;
126190
127- xhr . onreadystatechange = function ( ) {
191+ xhr . onreadystatechange = function ( ) {
128192 if ( this . readyState === 4 ) {
129- if ( this . status == 0 ) {
193+ if ( this . status == 0 ) {
130194 jsonData = {
131195 'message' : 'Internet is not connected!'
132196 } ;
133197 } else {
134198 jsonData = JSON . parse ( this . responseText ) ;
135199 }
136200
137- if ( callback && typeof ( callback ) === typeof ( Function ) ) {
201+ if ( callback && typeof ( callback ) === typeof ( Function ) ) {
138202 callback ( jsonData ) ;
139203 } else {
140204 console . error ( `Passed callback '${ callback } ' is not a valid Function.` )
@@ -148,21 +212,21 @@ function getRequest(subUrl, urlParams = '', callback) {
148212}
149213
150214function postRequest ( subUrl , urlParams = '' , requestData = { } , callback ) {
151- var jsonData = { } ;
215+ var jsonData = { } ;
152216 var XMLHttpRequest = require ( 'xmlhttprequest' ) . XMLHttpRequest ;
153217 var xhr = new XMLHttpRequest ( ) ;
154218
155- xhr . onreadystatechange = function ( ) {
219+ xhr . onreadystatechange = function ( ) {
156220 if ( this . readyState === 4 ) {
157221 if ( this . status == 0 ) {
158222 jsonData = {
159223 'message' : 'Internet is not connected!'
160224 } ;
161225 } else {
162- jsonData = JSON . parse ( this . responseText ) ;
226+ jsonData = JSON . parse ( this . responseText ) ;
163227 }
164-
165- if ( callback && typeof ( callback ) === typeof ( Function ) ) {
228+
229+ if ( callback && typeof ( callback ) === typeof ( Function ) ) {
166230 callback ( jsonData ) ;
167231 } else {
168232 console . error ( `Passed callback '${ callback } ' is not a valid Function.` )
0 commit comments