Skip to content

Commit

Permalink
Improve typings
Browse files Browse the repository at this point in the history
  • Loading branch information
zzarcon committed Apr 1, 2019
1 parent 10b9f0b commit 8a91e46
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions custom-typings/instagram-private-api.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'instagram-private-api';
4 changes: 1 addition & 3 deletions src/luckybot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Scraper } from "./strategies/scraper";
import { Strategy } from "./strategies/strategy";
import { RestApi } from "./strategies/api";

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

Expand All @@ -30,7 +28,7 @@ export class LuckyBot {
}

async likePhotos(hashtag: string, options: LikeOptions = {maxLikes: 50}) {
await this.client.likePhotos(hashtag, options);
await this.client.likeMedias(hashtag, options);
}

async close() {
Expand Down
33 changes: 29 additions & 4 deletions src/strategies/api.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import InstagramClient from 'instagram-private-api';
import { Strategy, LikeOptions } from "./strategy";
import { sleep } from '../util/sleep';

const Client = InstagramClient.V1;

export interface Media {
id: string;
webLink: string;
}

export class RestApi implements Strategy {
session?: any;

Expand All @@ -18,16 +24,35 @@ export class RestApi implements Strategy {
this.session = session;
}

async likePhotos(hashtag: string, options?: LikeOptions): Promise<void> {
async search(hashtag: string): Promise<Media[]> {
const {session} = this;
const taggedMedia = new Client.Feed.TaggedMedia(session, hashtag);
const results = await taggedMedia.get();
const firstMedia = results[0];

console.log('like', firstMedia.id);
await Client.Like.create(session, firstMedia.id)
return results.map((result: any) => ({
id: result.id,
webLink: result._params.webLink
}))
};

async likeMedias(hashtag: string, options: LikeOptions = {maxLikes: 20}): Promise<void> {
const {maxLikes} = options;
console.log('likeMedias', hashtag, maxLikes)
const results = await this.search(hashtag);
const likedPhotos = results.slice(0, maxLikes).map((media, index) => {
return this.likeMedia(media, index * 15000);
});

await Promise.all(likedPhotos);
};


async likeMedia(media: Media, delay: number) {
await sleep(delay);
console.log('likeMedia', media.id, media.webLink);
await Client.Like.create(this.session, media.id);
}

async close() {

}
Expand Down
2 changes: 1 addition & 1 deletion src/strategies/strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export interface LikeOptions {

export interface Strategy {
login(userName: string, password: string): Promise<void>;
likePhotos(hashtag: string, options?: LikeOptions): Promise<void>;
likeMedias(hashtag: string, options?: LikeOptions): Promise<void>;
close(): Promise<void>;
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,8 @@
"strict": true,
"moduleResolution": "node",
"esModuleInterop": true
}
},
"files": [
"./custom-typings/instagram-private-api.d.ts"
]
}

0 comments on commit 8a91e46

Please sign in to comment.