Skip to content

Commit

Permalink
updated examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Nerixyz committed Jan 1, 2020
1 parent 7144fd2 commit 3b6cf26
Show file tree
Hide file tree
Showing 11 changed files with 57 additions and 32 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ From github
```
npm install github:dilame/instagram-private-api
```

# Support us

If you find this library useful for you, you can support it by donating any amount
Expand All @@ -47,6 +46,13 @@ BTC: 1Dqnz9QuswAvD3t7Jsw7LhwprR6HAWprW6

You can find usage examples [here](examples)

*Note for JavaScript users:*
As of Node v.13.5.0, there isn't support for ESModules and the 'import'-syntax.
So you have to read the imports in the examples like this:

`import { A } from 'b'``const { A } = require('b')`


```typescript
import { IgApiClient } from './src';
import { sample } from 'lodash';
Expand Down
23 changes: 11 additions & 12 deletions examples/2fa-sms-login.example.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* tslint:disable:no-console */
import { IgApiClient, IgLoginTwoFactorRequiredError } from '../src';
import { get } from 'lodash';
import * as Bluebird from 'bluebird';
import inquirer = require('inquirer');

Expand All @@ -15,27 +15,26 @@ import inquirer = require('inquirer');
return Bluebird.try(() => ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD)).catch(
IgLoginTwoFactorRequiredError,
async err => {
const twoFactorIdentifier = get(err, 'response.body.two_factor_info.two_factor_identifier');
if (!twoFactorIdentifier) {
throw new Error('Unable to login, no 2fa identifier found');
}
// At this point a code should have been received via SMS
// Get SMS code from stdin
const {username, totp_two_factor_on, two_factor_identifier} = err.response.body.two_factor_info;
// decide which method to use
const verificationMethod = totp_two_factor_on ? '0' : '1'; // default to 1 for SMS
// At this point a code should have been sent
// Get the code
const { code } = await inquirer.prompt([
{
type: 'input',
name: 'code',
message: 'Enter code',
message: `Enter code received via ${verificationMethod === '1' ? 'SMS' : 'TOTP'}`,
},
]);
// Use the code to finish the login process
return ig.account.twoFactorLogin({
username: process.env.IG_USERNAME,
username,
verificationCode: code,
twoFactorIdentifier,
verificationMethod: '1', // '1' = SMS (default), '0' = OTP
twoFactorIdentifier: two_factor_identifier,
verificationMethod, // '1' = SMS (default), '0' = TOTP (google auth for example)
trustThisDevice: '1', // Can be omitted as '1' is used by default
});
},
);
).catch(e => console.error('An error occurred while processing two factor auth', e, e.stack));
})();
1 change: 1 addition & 0 deletions examples/account-followers.feed.example.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* tslint:disable:no-console */
import 'dotenv/config';
import { IgApiClient } from '../src';

Expand Down
8 changes: 7 additions & 1 deletion examples/checkpoint.example.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
/* tslint:disable:no-console */
import 'dotenv/config';
import { IgApiClient, IgCheckpointError } from '../src';
import Bluebird = require('bluebird');
import inquirer = require('inquirer');

/**
* This method won't catch all checkpoint errors
* There's currently a new checkpoint used by instagram which requires 'web-support'
*/

(async () => {
const ig = new IgApiClient();
ig.state.generateDevice(process.env.IG_USERNAME);
Expand All @@ -22,5 +28,5 @@ import inquirer = require('inquirer');
},
]);
console.log(await ig.challenge.sendSecurityCode(code));
});
}).catch(e => console.log('Could not resolve checkpoint:', e, e.stack));
})();
1 change: 1 addition & 0 deletions examples/session.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ function fakeLoad() {
}
// This call will provoke request.end$ stream
await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
// Most of the time you don't have to login after loading the state
})();
2 changes: 1 addition & 1 deletion examples/socks5-proxy.example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const shttps = require('socks-proxy-agent'); // you should install SOCKS5 client
// @ts-ignore
hostname: '127.0.0.1', // proxy hostname
port: 8000, // proxy port
protocol: 'socks:' // supported: 'socks:' , 'socks4:' , 'socks4a:' , 'socks5:' , 'socks5h:'
protocol: 'socks:' // supported: 'socks:' , 'socks4:' , 'socks4a:' , 'socks5:' , 'socks5h:'
};
// Now we can perform authorization using our SOCKS5 proxy.
const auth = await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
Expand Down
11 changes: 4 additions & 7 deletions examples/upload-photo-from-web.example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* tslint:disable:no-console */
import 'dotenv/config';
import { IgApiClient } from '../src';
import { get } from 'request-promise'; // request is already declared as an dependency of the library
import { get } from 'request-promise'; // request is already declared as a dependency of the library

(async () => {
const ig = new IgApiClient();
Expand All @@ -10,15 +10,12 @@ import { get } from 'request-promise'; // request is already declared as an depe
const auth = await ig.account.login(process.env.IG_USERNAME, process.env.IG_PASSWORD);
console.log(JSON.stringify(auth));

// getting random square image from internet
const imageRequest = await get({
// getting random square image from internet as a Buffer
const imageBuffer = await get({
url: 'https://picsum.photos/800/800', // random picture with 800x800 size
encoding: null, // this is required, we could convert body to buffer only with null encoding
encoding: null, // this is required, only this way a Buffer is returned
});

// converting image body to buffer
const imageBuffer = Buffer.from(imageRequest.body, 'binary');

const publishResult = await ig.publish.photo({
file: imageBuffer, // image buffer, you also can specify image from your disk using fs
caption: 'Really nice photo from the internet! 💖', // nice caption (optional)
Expand Down
10 changes: 7 additions & 3 deletions examples/upload-photo.example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* tslint:disable:no-console */
import { IgApiClient } from '../src';
import * as Bluebird from 'bluebird';
import { readFile } from 'fs';
import { promisify } from 'util';
const readFileAsync = promisify(readFile);

const ig = new IgApiClient();

Expand Down Expand Up @@ -38,9 +39,12 @@ async function login() {

const publishResult = await ig.publish.photo({
// read the file into a Buffer
file: await Bluebird.fromCallback(cb => readFile(path, cb)),
location: mediaLocation,
file: await readFileAsync(path),
// optional, default ''
caption: 'my caption',
// optional
location: mediaLocation,
// optional
usertags: {
in: [
// tag the user 'instagram' @ (0.5 | 0.5)
Expand Down
13 changes: 11 additions & 2 deletions examples/upload-story.example.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { IgApiClient } from '../src';
import Bluebird = require('bluebird');
import { readFile } from 'fs';
import { DateTime, Duration } from 'luxon';

import { StickerBuilder} from '../src/sticker-builder';
import { promisify } from 'util';
const readFileAsync = promisify(readFile);

const ig = new IgApiClient();

Expand All @@ -16,7 +17,7 @@ async function login() {
(async () => {
await login();
const path = './myStory.jpg';
const file = await Bluebird.fromCallback<Buffer>(cb => readFile(path, cb));
const file = await readFileAsync(path);

/**
* You can move and rotate stickers by using one of these methods:
Expand All @@ -32,6 +33,14 @@ async function login() {
*
* All of these are chainable e.g.:
* StickerBuilder.hashtag({ tagName: 'tag' }).scale(0.5).rotateDeg(90).center().left()
* You can also set the position and size like this:
* StickerBuilder.hashtag({
* tagName: 'insta',
* width: 0.5,
* height: 0.5,
* x: 0.5,
* y: 0.5,
* })
*/

// these stickers are 'invisible' and not 're-rendered' in the app
Expand Down
7 changes: 4 additions & 3 deletions examples/upload-video.example.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
/* tslint:disable:no-console */
import { IgApiClient } from '../src';
import * as Bluebird from 'bluebird';
import { readFile } from 'fs';
import { promisify } from 'util';
const readFileAsync = promisify(readFile);

const ig = new IgApiClient();

Expand All @@ -20,8 +21,8 @@ async function login() {

const publishResult = await ig.publish.video({
// read the file into a Buffer
video: await Bluebird.fromCallback(cb => readFile(videoPath, cb)),
coverImage: await Bluebird.fromCallback(cb => readFile(coverPath, cb)),
video: await readFileAsync(videoPath),
coverImage: await readFileAsync(coverPath),
/*
this does also support:
caption (string), ----+
Expand Down
5 changes: 3 additions & 2 deletions src/sticker-builder/stickers/countdown.sticker.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { InstaSticker } from './insta-sticker';
import { DateTime } from 'luxon';

export interface CountdownStickerOptions {
endTs: number;
endTs: DateTime | number;
text: string;
textColor?: string;
startBackgroundColor?: string;
Expand All @@ -11,7 +12,7 @@ export interface CountdownStickerOptions {
}

export class CountdownSticker extends InstaSticker implements CountdownStickerOptions {
endTs: number;
endTs: number | DateTime;
text: string;
textColor: string = '#ffffff';
startBackgroundColor: string = '#ca2ee1';
Expand Down

0 comments on commit 3b6cf26

Please sign in to comment.