Skip to content

Commit 87dfd18

Browse files
committed
Add listen posts on hashtags and location
1 parent be6869c commit 87dfd18

File tree

3 files changed

+138
-37
lines changed

3 files changed

+138
-37
lines changed

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ Inst.follow(3, 1) //unfollow "kevin"
7474
Insta.getUserMedia(3, '0', 12).then(f => console.log(f))
7575
````
7676

77+
### Get media by hashtags and locations
78+
````js
79+
Insta.commonSearch('Kyiv').then(r =>
80+
{
81+
//get location id for Kyiv
82+
let locationId = r.places[0].place.location['pk']
83+
//search posts from Kyiv
84+
Insta.searchBy('location', locationId, '0', 12).then(r => console.log(r))
85+
//search posts by hashtag "Eurovision"
86+
})
87+
Insta.searchBy('hashtag', 'Eurovision').then(r => console.log(r))
88+
````
89+
7790
When you pass items counter param instagram create pagination tokens on all iterations and gives on every response end_cursor, which the need to pass on next feed request
7891

7992

instagram.js

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module.exports = class Instagram
2222
this.timeoutForCounter = 300
2323
this.timeoutForCounterValue = 30000
2424
this.receivePromises = {}
25+
this.searchTypes = ['location', 'hashtag']
2526
}
2627

2728
/**
@@ -50,6 +51,7 @@ module.exports = class Instagram
5051

5152
/**
5253
* User followers list
54+
* Bench - 1k followers/1 min
5355
* @param {Int} userId
5456
* @param {String} command
5557
* @param {String} Params
@@ -410,6 +412,7 @@ module.exports = class Instagram
410412
*/
411413
getUserMedia(userId, cursor, mediaCounter)
412414
{
415+
cursor = cursor ? cursor : '0'
413416
mediaCounter = mediaCounter ? mediaCounter : 12
414417
let form = new formData()
415418
form.append('q', 'ig_user('+userId+') { media.after('+cursor+', '+mediaCounter+') {\
@@ -452,4 +455,87 @@ module.exports = class Instagram
452455
body : form
453456
}).then(r => r.text().then(t => t))
454457
}
458+
459+
/**
460+
* End cursor - t.entry_data.TagPage[0].tag.media.page_info['end_cursor']
461+
* Media(nodes) - t.entry_data.TagPage[0].tag.media['nodes']
462+
* @param {String} searchBy - location, hashtag
463+
* @param {String} q - location id, or hashtag
464+
* @param {String} cursor pagination cursor
465+
* @param {Int} mediaCounter
466+
* @return {Object} Promise
467+
*/
468+
searchBy(searchBy, q, cursor, mediaCounter)
469+
{
470+
if(this.searchTypes.indexOf(searchBy) === false)
471+
throw 'search type '+ searchBy + ' is not found'
472+
473+
//exclusion for hashtag if not cursor
474+
if(searchBy == 'hashtag' && !cursor)
475+
{
476+
return fetch('https://www.instagram.com/explore/tags/'+q+'/',
477+
{
478+
headers: this.getHeaders(),
479+
}).then( t => t.text().then(r => JSON.parse(r.match(/\<script type=\"text\/javascript\">window\._sharedData \=(.*)\;<\//)[1])))
480+
}
481+
482+
let form = new formData()
483+
mediaCounter = mediaCounter ? mediaCounter : 12
484+
form.append('q', 'ig_'+searchBy+'('+q+') { media.after('+cursor+', '+mediaCounter+') {\
485+
count,\
486+
nodes {\
487+
__typename,\
488+
caption,\
489+
code,\
490+
comments {\
491+
count\
492+
},\
493+
comments_disabled,\
494+
date,\
495+
dimensions {\
496+
height,\
497+
width\
498+
},\
499+
display_src,\
500+
id,\
501+
is_video,\
502+
likes {\
503+
count\
504+
},\
505+
owner {\
506+
id\
507+
},\
508+
thumbnail_src,\
509+
video_views\
510+
},\
511+
page_info\
512+
}\
513+
}')
514+
515+
form.append('ref', 'locations::show')
516+
form.append('query_id', '') //empty
517+
518+
519+
return fetch('https://www.instagram.com/query/',
520+
{
521+
headers: this.getHeaders(),
522+
method: 'post',
523+
body: form
524+
}).then(t => t.json().then(r => r))
525+
}
526+
527+
/**
528+
* Place id path - r.places[0].place.location['pk'], r.places[1].place.location['pk'], ...
529+
* Common search returned locations, hashtags and users
530+
* @param {String} q
531+
* @return {Object} Promise
532+
*/
533+
commonSearch(q, rankToken)
534+
{
535+
rankToken = rankToken ? rankToken : ''
536+
return fetch('https://www.instagram.com/web/search/topsearch/?context=blended&query='+q+'&rank_token='+ rankToken,
537+
{
538+
headers: this.getHeaders() // no required
539+
}).then(t => t.json().then(r => r))
540+
}
455541
}

package.json

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
{
2-
"dependencies": {
3-
"fetch-cookie": "^0.6.0",
4-
"form-data": "^2.1.2",
5-
"node-fetch": "^1.6.3"
6-
},
7-
"name": "instagram-nodejs-without-api",
8-
"description": "Auth and get followers on instagram with nodejs",
9-
"version": "0.2.5",
10-
"main": "instagram.js",
11-
"scripts": {
12-
"test": "echo \"Error: no test specified\" && exit 1"
13-
},
14-
"repository": {
15-
"type": "git",
16-
"url": "git+https://github.com/yatsenkolesh/instagram-nodejs.git"
17-
},
18-
"keywords": [
19-
"Instagram",
20-
"Instagram",
21-
"without",
22-
"API",
23-
"get",
24-
"instagram",
25-
"followers",
26-
"nodejs",
27-
"mass-following",
28-
"mass-liking",
29-
"instagram-feed"
30-
],
31-
"author": "Alex Yatsenko <webfay1@gmail.com> (http://webfay.net)",
32-
"license": "BSD-3-Clause",
33-
"bugs": {
34-
"url": "https://github.com/yatsenkolesh/instagram-nodejs/issues"
35-
},
36-
"homepage": "https://github.com/yatsenkolesh/instagram-nodejs#readme"
37-
}
1+
{
2+
"dependencies": {
3+
"fetch-cookie": "^0.6.0",
4+
"form-data": "^2.1.2",
5+
"node-fetch": "^1.6.3"
6+
},
7+
"name": "instagram-nodejs-without-api",
8+
"description": "Auth and get followers on instagram with nodejs",
9+
"version": "0.2.0",
10+
"main": "instagram.js",
11+
"scripts": {
12+
"test": "echo \"Error: no test specified\" && exit 1"
13+
},
14+
"repository": {
15+
"type": "git",
16+
"url": "git+https://github.com/yatsenkolesh/instagram-nodejs.git"
17+
},
18+
"keywords": [
19+
"Instagram",
20+
"Instagram",
21+
"without",
22+
"API",
23+
"get",
24+
"instagram",
25+
"followers",
26+
"nodejs",
27+
"mass-following",
28+
"mass-liking",
29+
"instagram-feed",
30+
"instagram-search",
31+
"instagram-full-search"
32+
],
33+
"author": "Alex Yatsenko <webfay1@gmail.com> (http://webfay.net)",
34+
"license": "BSD-3-Clause",
35+
"bugs": {
36+
"url": "https://github.com/yatsenkolesh/instagram-nodejs/issues"
37+
},
38+
"homepage": "https://github.com/yatsenkolesh/instagram-nodejs#readme"
39+
}

0 commit comments

Comments
 (0)