Skip to content

Commit

Permalink
Allow to pass cookiePath
Browse files Browse the repository at this point in the history
  • Loading branch information
zzarcon committed Apr 3, 2019
1 parent f378dba commit c2f9f72
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,6 @@ typings/
.next
src/*.js
src/**/*.json
example/**/*.json
dist
debug.png
11 changes: 10 additions & 1 deletion __tests__/api.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ describe('Api Strategy', () => {
'some-pass'
)
expect(session).toEqual(sessionValue)
})
});

it('should use given cookiePath', async () => {
setup();
const api = new RestApi({cookiePath: 'some-folder'});
await api.login('some-user', 'some-pass');

expect(InstagramClient.V1.CookieFileStorage).toBeCalledTimes(1)
expect(InstagramClient.V1.CookieFileStorage).toBeCalledWith('some-folder/some-user.json')
});
});

describe('likeMedias()', () => {
Expand Down
5 changes: 3 additions & 2 deletions example/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const run = async () => {
if (!user || !pass) {
throw new Error('No username or password')
}

const bot = new LuckyBot(user, pass, {debug: true});

const cookiePath = `${__dirname}/cookies`;
const bot = new LuckyBot(user, pass, {debug: true, cookiePath});

console.log('LOGIN...')
await bot.login();
Expand Down
3 changes: 2 additions & 1 deletion src/luckybot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { RestApi } from "./strategies/api";

export interface LuckyBotOptions {
debug?: boolean;
cookiePath?: string;
}

export interface LikeOptions {
Expand All @@ -19,7 +20,7 @@ export class LuckyBot {
this.userName = userName;
this.password = password;
this.options = options;
this.client = new RestApi();
this.client = new RestApi(options);
}

async login(): Promise<void> {
Expand Down
10 changes: 6 additions & 4 deletions src/strategies/api.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import InstagramClient from 'instagram-private-api';
import {existsSync} from 'fs';
import { Strategy, LikeOptions, Media } from "./strategy";
import { Strategy, LikeOptions, Media, StrategyOptions } from "./strategy";
import { sleep } from '../util/sleep';

const Client = InstagramClient.V1;

// TODO: move session logic into Session class
export class RestApi implements Strategy {
session?: Object;
options: StrategyOptions;

constructor() {

constructor(options?: StrategyOptions) {
this.options = options || {cookiePath: __dirname};
}

async login(userName: string, password: string): Promise<Object> {
const device = new Client.Device(userName);
const cookiePath = `${__dirname}/${userName}.json`
const cookiePath = `${this.options.cookiePath}/${userName}.json`
console.log('login: exist cookie', {cookiePath}, existsSync(cookiePath))
const storage = new Client.CookieFileStorage(cookiePath);
const session: Object = await Client.Session.create(device, storage, userName, password)
Expand Down
4 changes: 4 additions & 0 deletions src/strategies/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ export interface LikeOptions {
maxLikes?: number;
}

export interface StrategyOptions {
cookiePath?: string;
}

export interface Strategy {
login(userName: string, password: string): Promise<Object>;
likeMedias(hashtag: string, options?: LikeOptions): Promise<Media[]>;
Expand Down

0 comments on commit c2f9f72

Please sign in to comment.