@@ -15,12 +15,25 @@ module.exports = class Instagram {
1515 constructor ( csrfToken , sessionId ) {
1616 this . csrfToken = csrfToken
1717 this . sessionId = sessionId
18- this . userAgent = 'Mozilla/5.0 (Windows NT 6.1 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55 .0.2900.1 Iron Safari/537.36'
18+ this . userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4 ) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68 .0.3440.106 Safari/537.36'
1919 this . userIdFollowers = { } ;
2020 this . timeoutForCounter = 300
2121 this . timeoutForCounterValue = 30000
2222 this . receivePromises = { }
2323 this . searchTypes = [ 'location' , 'hashtag' ]
24+
25+ this . essentialValues = {
26+ sessionid : undefined ,
27+ ds_user_id : undefined ,
28+ csrftoken : undefined ,
29+ shbid : undefined ,
30+ rur : undefined ,
31+ mid : undefined ,
32+ shbts : undefined ,
33+ mcd : undefined ,
34+ ig_cb : undefined ,
35+ //urlgen : undefined //this needs to be filled in according to my RE
36+ } ;
2437 }
2538
2639 /**
@@ -29,7 +42,35 @@ module.exports = class Instagram {
2942 * @return {Object } Promise
3043 */
3144 getUserDataByUsername ( username ) {
32- return fetch ( 'https://www.instagram.com/' + username ) . then ( res => res . text ( ) . then ( function ( data ) {
45+
46+ var cookie = ''
47+
48+ var keys = Object . keys ( this . essentialValues )
49+ for ( var i = 0 ; i < keys . length ; i ++ ) {
50+ var key = keys [ i ] ;
51+ cookie += key + '=' + this . essentialValues [ key ] + ( i < keys . length - 1 ? '; ' : '' )
52+ }
53+
54+
55+ var fetch_data = {
56+ 'method' : 'get' ,
57+ 'headers' :
58+ {
59+ 'accept' : 'text/html,application/xhtml+xml,application/xml;q0.9,image/webp,image/apng,*.*;q=0.8' ,
60+ 'accept-encoding' : 'gzip, deflate, br' ,
61+ 'accept-langauge' : 'en-US;q=0.9,en;q=0.8,es;q=0.7' ,
62+ 'cookie' : cookie ,
63+
64+ 'origin' : 'https://www.instagram.com' ,
65+
66+ 'referer' : 'https://www.instagram.com/' ,
67+ 'upgrade-insecure-requests' : '1' ,
68+
69+ 'user-agent' : this . userAgent ,
70+ }
71+ }
72+
73+ return fetch ( 'https://www.instagram.com/' + username , fetch_data ) . then ( res => res . text ( ) . then ( function ( data ) {
3374 const regex = / w i n d o w \. _ s h a r e d D a t a = ( .* ) ; < \/ s c r i p t > / ;
3475 const match = regex . exec ( data ) ;
3576 if ( typeof match [ 1 ] === 'undefined' ) {
@@ -166,20 +207,50 @@ module.exports = class Instagram {
166207 'method' : 'get' ,
167208 'headers' :
168209 {
169- 'referer' : 'https://www.instagram.com/' ,
210+ 'accept' : 'text/html,application/xhtml+xml,application/xml;q0.9,image/webp,image/apng,*.*;q=0.8' ,
211+ 'accept-langauge' : 'en-US;q=0.9,en;q=0.8,es;q=0.7' ,
212+
170213 'origin' : 'https://www.instagram.com' ,
214+
215+ 'referer' : 'https://www.instagram.com/' ,
216+ 'upgrade-insecure-requests' : '1' ,
217+
171218 'user-agent' : this . userAgent ,
219+
220+ 'cookie' : 'ig_cb=1'
172221 }
173- } ) . then (
174- t => {
175- let cookies = t . headers . _headers [ 'set-cookie' ]
176- for ( let i in cookies ) {
177- if ( ! cookies [ i ] . includes ( 'csrftoken="";' ) && cookies [ i ] . includes ( 'csrftoken=' ) ) {
178- return cookies [ i ] . split ( ';' ) [ 0 ] . replace ( 'csrftoken=' , '' )
179- }
180- }
222+ } ) . then ( t => {
223+ let cookies = t . headers . _headers [ 'set-cookie' ]
224+
225+ var keys = Object . keys ( this . essentialValues )
226+
227+ for ( var i = 0 ; i < keys . length ; i ++ ) {
228+ var key = keys [ i ] ;
229+ if ( ! this . essentialValues [ key ] )
230+ for ( let c in cookies )
231+ if ( cookies [ c ] . includes ( key ) && ! cookies [ c ] . includes ( key + '=""' ) ) {
232+ var cookieValue = cookies [ c ] . split ( ';' ) [ 0 ] . replace ( key + '=' , '' )
233+ this . essentialValues [ key ] = cookieValue
234+ break ;
235+ }
181236 }
182- ) . catch ( ( ) =>
237+
238+ return t . text ( ) ;
239+ } ) . then ( html => {
240+ var subStr = html ;
241+
242+ var startStr = '<script type="text/javascript">window._sharedData = ' ;
243+ var start = subStr . indexOf ( startStr ) + startStr . length ;
244+ subStr = subStr . substr ( start , subStr . length ) ;
245+
246+ subStr = subStr . substr ( 0 , subStr . indexOf ( '</script>' ) - 1 ) ;
247+
248+ var json = JSON . parse ( subStr ) ;
249+
250+ this . rollout_hash = json . rollout_hash ;
251+
252+ return json . config . csrf_token ;
253+ } ) . catch ( ( ) =>
183254 console . log ( 'Failed to get instagram csrf token' )
184255 )
185256 }
@@ -190,38 +261,53 @@ module.exports = class Instagram {
190261 * @param {String } password
191262 * @return {Object } Promise
192263 */
193- auth ( username , password ) {
194- let form = new formData ( ) ;
195- form . append ( 'username' , username )
196- form . append ( 'password' , password )
197- return fetch ( 'https://www.instagram.com/accounts/login/ajax/' ,
198- {
199- 'method' : 'post' ,
200- 'body' : form ,
201- 'headers' :
202- {
203- 'referer' : 'https://www.instagram.com/' ,
204- 'origin' : 'https://www.instagram.com' ,
205- 'user-agent' : this . userAgent ,
206- 'x-instagram-ajax' : '1' ,
207- 'x-requested-with' : 'XMLHttpRequest' ,
208- 'x-csrftoken' : this . csrfToken ,
209- cookie : 'csrftoken=' + this . csrfToken
210- }
211- } ) . then (
212- t => {
213- let cookies = t . headers . _headers [ 'set-cookie' ]
214- for ( let i in cookies ) {
215- if ( ! cookies [ i ] . includes ( 'sessionid=""' ) && cookies [ i ] . includes ( 'sessionid=' ) ) {
216- console . log ( cookies [ i ] )
217- return cookies [ i ] . split ( ';' ) [ 0 ] . replace ( 'sessionid=' , '' )
264+ auth ( username , password ) {
265+ var formdata = 'username=' + username + '&password=' + password + '&queryParams=%7B%7D'
266+
267+ var options = {
268+ method : 'POST' ,
269+ body : formdata ,
270+ headers :
271+ {
272+ 'accept' : '*/*' ,
273+ 'accept-encoding' : 'gzip, deflate, br' ,
274+ 'accept-language' : 'sv,en-US;q=0.9,en;q=0.8,es;q=0.7' ,
275+ 'content-length' : formdata . length ,
276+ 'content-type' : 'application/x-www-form-urlencoded' ,
277+ 'cookie' : 'ig_cb=' + this . essentialValues . ig_cb ,
278+ 'origin' : 'https://www.instagram.com' ,
279+ 'referer' : 'https://www.instagram.com/' ,
280+ 'user-agent' : this . userAgent ,
281+ 'x-csrftoken' : this . csrfToken ,
282+ 'x-instagram-ajax' : this . rollout_hash ,
283+ 'x-requested-with' : 'XMLHttpRequest' ,
284+
285+ }
286+ }
287+
288+ return fetch ( 'https://www.instagram.com/accounts/login/ajax/' , options ) . then (
289+ t => {
290+ let cookies = t . headers . _headers [ 'set-cookie' ]
291+
292+ var keys = Object . keys ( this . essentialValues )
293+
294+ for ( var i = 0 ; i < keys . length ; i ++ ) {
295+ var key = keys [ i ] ;
296+ if ( ! this . essentialValues [ key ] )
297+ for ( let c in cookies ) {
298+ if ( cookies [ c ] . includes ( key ) && ! cookies [ c ] . includes ( key + '=""' ) ) {
299+ var cookieValue = cookies [ c ] . split ( ';' ) [ 0 ] . replace ( key + '=' , '' )
300+ this . essentialValues [ key ] = cookieValue
301+ break ;
218302 }
219303 }
220- }
221- ) . catch ( ( ) =>
222- console . log ( 'Instagram authentication failed' )
223- )
224- }
304+ }
305+
306+ return this . essentialsValues . sessionId ;
307+ } ) . catch ( ( ) =>
308+ console . log ( 'Instagram authentication failed' )
309+ )
310+ }
225311
226312 /**
227313 * Registration for instagram, returning true or false
0 commit comments