-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(instagram): implement instagram watcher (#7)
- Loading branch information
Showing
7 changed files
with
564 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { BaseWatcher, BaseWatcherOptions, PartialUser } from "@watchers/base"; | ||
import { IgApiClient } from "instagram-private-api"; | ||
import { Resolve, sleep } from "@utils"; | ||
|
||
export interface InstagramWatcherOptions extends BaseWatcherOptions<InstagramWatcher> { | ||
username: string; | ||
password: string; | ||
targetUserName: string; | ||
requestDelay?: number; | ||
} | ||
|
||
export class InstagramWatcher extends BaseWatcher<"Instagram"> { | ||
private readonly client = new IgApiClient(); | ||
|
||
private readonly username: string; | ||
private readonly password: string; | ||
private readonly targetUserName: string; | ||
private readonly requestDelay: number; | ||
|
||
private loggedInUser: Resolve<ReturnType<typeof this.client.account.login>> | null = null; | ||
|
||
public constructor({ username, password, targetUserName, requestDelay = 1000 }: InstagramWatcherOptions) { | ||
super("Instagram"); | ||
|
||
this.username = username; | ||
this.password = password; | ||
this.targetUserName = targetUserName; | ||
this.requestDelay = requestDelay; | ||
} | ||
|
||
public async initialize() { | ||
this.client.state.generateDevice(this.username); | ||
await this.client.simulate.preLoginFlow(); | ||
|
||
this.loggedInUser = await this.client.account.login(this.username, this.password); | ||
|
||
this.logger.verbose("Successfully initialized with user name {}", [this.loggedInUser.username]); | ||
} | ||
|
||
protected async getFollowers() { | ||
// get followers | ||
const id = await this.client.user.getIdByUsername(this.targetUserName); | ||
if (!id) { | ||
throw new Error("Failed to get user id"); | ||
} | ||
|
||
const followersFeed = this.client.feed.accountFollowers(id); | ||
const followers: PartialUser[] = []; | ||
while (true) { | ||
const items = await followersFeed.items(); | ||
followers.push( | ||
...items.map(user => ({ | ||
uniqueId: user.pk.toString(), | ||
displayName: user.full_name, | ||
userId: user.username, | ||
profileUrl: `https://instagram.com/${user.username}`, | ||
})), | ||
); | ||
|
||
if (!followersFeed.isMoreAvailable()) { | ||
break; | ||
} | ||
|
||
await sleep(this.requestDelay); | ||
} | ||
|
||
return followers; | ||
} | ||
} |
Oops, something went wrong.