forked from KaKi87/scraper-instagram-v1
parent
e60df18858
commit
8e3028d1ee
|
@ -248,3 +248,4 @@ Insta.subscribeHashtagPosts('selfie', posts => {
|
|||
- Added support for hashtags, mentions and tags in posts and comments
|
||||
- Added posts subscriptions feature from users (untested) and hashtags
|
||||
* `1.0.2` (2019-03-27) • Added support for videos
|
||||
* `1.0.3` (2019-03-27) • Using promises & observable
|
||||
|
|
117
index.js
117
index.js
|
@ -3,15 +3,16 @@
|
|||
const
|
||||
request = require('requestretry'),
|
||||
JSDOM = require('jsdom').JSDOM,
|
||||
Observable = require('zen-observable'),
|
||||
parse = document => new JSDOM(document, { runScripts: "dangerously" }).window.document,
|
||||
insta = 'https://instagram.com';
|
||||
|
||||
module.exports = {
|
||||
getProfile(username, callback){
|
||||
const link = `${insta}/${username}`;
|
||||
request(link,(err, res, body) => {
|
||||
getProfile: username => new Promise((resolve, reject) => {
|
||||
const link = `${ insta }/${ username }`;
|
||||
request(link, (err, res, body) => {
|
||||
if(res.statusCode === 404)
|
||||
throw new Error('Instagram user not found');
|
||||
return reject('Instagram user not found');
|
||||
body += `<script>document.querySelector('html').innerHTML = JSON.stringify(_sharedData.entry_data.ProfilePage[0].graphql.user)</script>`;
|
||||
let u;
|
||||
try {
|
||||
|
@ -31,15 +32,18 @@ module.exports = {
|
|||
.map(post => post['node']['shortcode']),
|
||||
link
|
||||
};
|
||||
} catch(e){ throw new Error('Instagram parsing error'); }
|
||||
callback(u);
|
||||
} catch(_) {
|
||||
}
|
||||
if(!u)
|
||||
return reject('Instagram parsing error');
|
||||
resolve(u);
|
||||
});
|
||||
},
|
||||
getHashtag(hashtag, callback){
|
||||
const link = `${insta}/explore/tags/${hashtag}/`;
|
||||
}),
|
||||
getHashtag: hashtag => new Promise((resolve, reject) => {
|
||||
const link = `${ insta }/explore/tags/${ hashtag }/`;
|
||||
request(link, (err, res, body) => {
|
||||
if(res.statusCode === 404)
|
||||
throw new Error('Instagram hashtag not found');
|
||||
return reject('Instagram hashtag not found');
|
||||
body += `<script>document.querySelector('html').innerHTML = JSON.stringify(_sharedData.entry_data.TagPage[0].graphql.hashtag)</script>`;
|
||||
let h;
|
||||
try {
|
||||
|
@ -54,15 +58,18 @@ module.exports = {
|
|||
.map(post => post['node']['shortcode']),
|
||||
link
|
||||
}
|
||||
} catch(e){ throw new Error('Instagram parsing error'); }
|
||||
callback(h);
|
||||
} catch(_) {
|
||||
}
|
||||
if(!h)
|
||||
return reject('Instagram parsing error');
|
||||
resolve(h);
|
||||
});
|
||||
},
|
||||
getPost(shortcode, callback){
|
||||
const link = `${insta}/p/${shortcode}`;
|
||||
}),
|
||||
getPost: shortcode => new Promise((resolve, reject) => {
|
||||
const link = `${ insta }/p/${ shortcode }`;
|
||||
request(link, (err, res, body) => {
|
||||
if(res.statusCode === 404)
|
||||
throw new Error('Instagram post not found');
|
||||
return reject('Instagram post not found');
|
||||
body += `<script>document.querySelector('html').innerHTML = JSON.stringify(_sharedData.entry_data.PostPage[0].graphql.shortcode_media)</script>`;
|
||||
let p;
|
||||
try {
|
||||
|
@ -92,7 +99,7 @@ module.exports = {
|
|||
name: post['owner']['full_name'],
|
||||
pic: post['owner']['profile_pic_url'],
|
||||
verified: post['owner']['is_verified'],
|
||||
link: `${insta}/${username}`
|
||||
link: `${ insta }/${ username }`
|
||||
},
|
||||
comments: post['comments_disabled'] ? null : post['edge_media_to_comment']['edges']
|
||||
.map(c => ({
|
||||
|
@ -135,65 +142,49 @@ module.exports = {
|
|||
});
|
||||
break;
|
||||
}
|
||||
} catch(e){ throw new Error('Instagram parsing error'); }
|
||||
callback(p);
|
||||
} catch(_) {
|
||||
}
|
||||
if(!p)
|
||||
return reject('Instagram parsing error');
|
||||
resolve(p);
|
||||
});
|
||||
},
|
||||
subscribeUserPosts(username, onPosts, options){
|
||||
const interval = options['interval'] || 30;
|
||||
let lastPost = options['lastPost'] || null;
|
||||
}),
|
||||
subscribeUserPosts: (username, interval, lastPost) => new Observable(observer => {
|
||||
const checkNewPosts = () => {
|
||||
module.exports.getProfile(username, profile => {
|
||||
const _lastPost = profile.lastPosts[0];
|
||||
if(_lastPost !== lastPost){
|
||||
lastPost = _lastPost;
|
||||
if(!_lastPost){
|
||||
onPosts(lastPost);
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
else {
|
||||
const posts = [];
|
||||
module.exports.getProfile(username)
|
||||
.then(profile => {
|
||||
const _lastPost = profile.lastPosts[0];
|
||||
if(lastPost && _lastPost !== lastPost){
|
||||
for(let i = 0; i < profile.lastPosts.indexOf(lastPost); i++){
|
||||
posts.push(profile.lastPosts[i]);
|
||||
observer.next(profile.lastPosts[i]);
|
||||
}
|
||||
onPosts(posts);
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
setTimeout(checkNewPosts, interval || 30);
|
||||
lastPost = _lastPost;
|
||||
}
|
||||
else {
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
observer.error(err);
|
||||
});
|
||||
};
|
||||
checkNewPosts();
|
||||
},
|
||||
subscribeHashtagPosts(hashtag, onPosts, options){
|
||||
const interval = options['interval'] || 30;
|
||||
let lastPost = options['lastPost'] || null;
|
||||
}),
|
||||
subscribeHashtagPosts: (hashtag, interval, lastPost) => new Observable(observer => {
|
||||
const checkNewPosts = () => {
|
||||
module.exports.getHashtag(hashtag, hashtag => {
|
||||
const _lastPost = hashtag.lastPosts[0];
|
||||
if(_lastPost !== lastPost){
|
||||
if(!_lastPost){
|
||||
onPosts(lastPost);
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
else {
|
||||
const posts = [];
|
||||
module.exports.getHashtag(hashtag)
|
||||
.then(hashtag => {
|
||||
const _lastPost = hashtag.lastPosts[0];
|
||||
if(lastPost && _lastPost !== lastPost){
|
||||
for(let i = 0; i < hashtag.lastPosts.indexOf(lastPost); i++){
|
||||
posts.push(hashtag.lastPosts[i]);
|
||||
observer.next(hashtag.lastPosts[i]);
|
||||
}
|
||||
onPosts(posts);
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
setTimeout(checkNewPosts, interval || 30);
|
||||
lastPost = _lastPost;
|
||||
}
|
||||
else {
|
||||
setTimeout(checkNewPosts, interval);
|
||||
}
|
||||
});
|
||||
})
|
||||
.catch(err => {
|
||||
observer.error(err);
|
||||
});
|
||||
};
|
||||
checkNewPosts();
|
||||
}
|
||||
})
|
||||
};
|
||||
|
|
16
package.json
16
package.json
|
@ -1,8 +1,17 @@
|
|||
{
|
||||
"name": "@kaki87/ig-scraper",
|
||||
"version": "1.0.2",
|
||||
"version": "1.0.3",
|
||||
"description": "Instagram scraper without authenticated API",
|
||||
"keywords": ["insta", "instagram", "ig", "scraper", "ig-scraper", "instagrap-scraper", "api", "scraper"],
|
||||
"keywords": [
|
||||
"insta",
|
||||
"instagram",
|
||||
"ig",
|
||||
"scraper",
|
||||
"ig-scraper",
|
||||
"instagrap-scraper",
|
||||
"api",
|
||||
"scraper"
|
||||
],
|
||||
"homepage": "https://git.kaki87.net/KaKi87/ig-scraper",
|
||||
"license": "MIT",
|
||||
"author": {
|
||||
|
@ -17,7 +26,8 @@
|
|||
},
|
||||
"dependencies": {
|
||||
"jsdom": "^14.0.0",
|
||||
"requestretry": "^4.0.0"
|
||||
"requestretry": "^4.0.0",
|
||||
"zen-observable": "^0.8.13"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
|
|
|
@ -661,3 +661,8 @@ xmlchars@^1.3.1:
|
|||
version "1.3.1"
|
||||
resolved "https://registry.yarnpkg.com/xmlchars/-/xmlchars-1.3.1.tgz#1dda035f833dbb4f86a0c28eaa6ca769214793cf"
|
||||
integrity sha512-tGkGJkN8XqCod7OT+EvGYK5Z4SfDQGD30zAa58OcnAa0RRWgzUEK72tkXhsX1FZd+rgnhRxFtmO+ihkp8LHSkw==
|
||||
|
||||
zen-observable@^0.8.13:
|
||||
version "0.8.13"
|
||||
resolved "https://registry.yarnpkg.com/zen-observable/-/zen-observable-0.8.13.tgz#a9f1b9dbdfd2d60a08761ceac6a861427d44ae2e"
|
||||
integrity sha512-fa+6aDUVvavYsefZw0zaZ/v3ckEtMgCFi30sn91SEZea4y/6jQp05E3omjkX91zV6RVdn15fqnFZ6RKjRGbp2g==
|
||||
|
|
Loading…
Reference in New Issue