From a2f873caa40ea9b2e3318e414b04a85e5f3c299d Mon Sep 17 00:00:00 2001 From: romuloalves Date: Tue, 13 Aug 2019 18:35:38 -0300 Subject: [PATCH] Splits code --- README.md | 4 ++-- now.json | 4 ++-- package-lock.json | 9 +++++++-- package.json | 5 +++-- src/get-picture.ts | 29 +++-------------------------- 5 files changed, 17 insertions(+), 34 deletions(-) diff --git a/README.md b/README.md index fa11044..3aa99a8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# Get Instagram Picture -> Get user pictures of Instagram without token or whatever +# Get Instagram Feed +> Get user's feed of Instagram without token or whatever *IMPORTANT: Unable to retrieve images from private accounts.* diff --git a/now.json b/now.json index 89f43e6..bfb7194 100644 --- a/now.json +++ b/now.json @@ -1,7 +1,7 @@ { - "name": "get-instagram-picture", + "name": "get-instagram-feed", "version": 2, - "alias": ["get-instagram-picture.now.sh"], + "alias": ["get-instagram-feed.now.sh"], "env": { "NODE_ENV": "production" }, diff --git a/package-lock.json b/package-lock.json index ee39566..c1e370a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { - "name": "get-instagram-picture", - "version": "0.1.1", + "name": "get-instagram-feed", + "version": "1.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -13,6 +13,11 @@ "@types/node": "*" } }, + "@romuloalves/get-instagram-data": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/@romuloalves/get-instagram-data/-/get-instagram-data-1.0.0.tgz", + "integrity": "sha512-3qUmGc/eVRwqMB8YenD75o+wdeB2UOG4tAp9yvQ/ueL4XxbUbJ2ZPHa3Ne8QCUlpz7IMRktw9LJVwZysm2yTwA==" + }, "@types/node": { "version": "12.7.1", "resolved": "https://registry.npmjs.org/@types/node/-/node-12.7.1.tgz", diff --git a/package.json b/package.json index 1ce6e98..d7fd87a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { - "name": "get-instagram-picture", - "version": "0.1.1", + "name": "get-instagram-feed", + "version": "1.0.0", "scripts": { "start": "micro", "dev": "micro-dev" @@ -13,6 +13,7 @@ "micro-dev": "3.0.0" }, "dependencies": { + "@romuloalves/get-instagram-data": "^1.0.0", "micro": "9.3.4" }, "license": "MIT" diff --git a/src/get-picture.ts b/src/get-picture.ts index b24cd13..7e79876 100644 --- a/src/get-picture.ts +++ b/src/get-picture.ts @@ -1,16 +1,4 @@ -import { get } from 'https'; - -const startDataString = 'window._sharedData = '; -const getInstagramUrl = (id: string) => `https://www.instagram.com/${id}/`; - -const getJsonData = (html:string): Object => { - const startIndex = html.indexOf(startDataString) + startDataString.length - const endIndex = html.indexOf(';', startIndex) - const substring = html.substring(startIndex, endIndex) - const data = JSON.parse(substring) - - return getImages(data) -}; +import getInstagramData from '@romuloalves/get-instagram-data'; const getImages = (data: any): Array => { if (data == null || data.entry_data == null || @@ -48,18 +36,7 @@ const getImages = (data: any): Array => { } export default async user => { - return new Promise((resolve, reject) => { - const userUrl = getInstagramUrl(user); - let html = ''; - - get(userUrl, res => { - res.on('data', chunk => { - html += chunk; - }); + const data = await getInstagramData(user); - res.on('end', () => { - resolve(getJsonData(html)); - }); - }).on('error', err => reject(err)); - }); + return getImages(data); }; -- 2.30.2