Skip to content

Commit

Permalink
fix(twitter): make twitter watcher to use rettiwt-api package instead…
Browse files Browse the repository at this point in the history
… of official one (#4)
  • Loading branch information
async3619 authored Oct 31, 2024
1 parent 0447655 commit 18e6374
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 53 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ jobs:
- name: Install and Build
uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: "22.x"

- name: Install yarn
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
- name: Install and Build
uses: actions/setup-node@v3
with:
node-version: "18.x"
node-version: "22.x"

- name: Install yarn
run: |
Expand Down
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:18-alpine as builder
FROM node:22-alpine as builder

RUN apk add --update --no-cache curl git openssh openssl

Expand All @@ -18,7 +18,7 @@ ENV NODE_ENV ${NODE_ENV}

RUN ["yarn", "build"]

FROM node:18-alpine as prod-deps
FROM node:22-alpine as prod-deps

USER node
WORKDIR /home/node
Expand All @@ -35,7 +35,7 @@ ENV NODE_ENV ${NODE_ENV}

CMD [ "node", "dist/src/index" ]

FROM node:18-alpine as production
FROM node:22-alpine as production

USER node
WORKDIR /home/node
Expand Down
4 changes: 2 additions & 2 deletions config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@
"type": "string",
"const": "twitter"
},
"bearerToken": {
"apiKey": {
"type": "string"
},
"username": {
"type": "string"
}
},
"required": [
"bearerToken",
"apiKey",
"type",
"username"
],
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@
"@atproto/api": "^0.3.13",
"@octokit/rest": "^19.0.5",
"@slack/webhook": "^6.1.0",
"@twitter-api-v2/plugin-rate-limit": "^1.1.0",
"@urql/core": "^3.0.5",
"ajv": "^8.11.2",
"better-ajv-errors": "^1.2.0",
Expand All @@ -97,10 +96,10 @@
"pluralize": "^8.0.0",
"pretty-ms": "^7.0.1",
"reflect-metadata": "^0.1.13",
"rettiwt-api": "^4.1.4",
"set-cookie-parser": "^2.5.1",
"sqlite3": "^5.1.2",
"strip-ansi": "^6.0.1",
"twitter-api-v2": "^1.12.9",
"typeorm": "^0.3.10",
"update-notifier": "^5.1.0"
}
Expand Down
40 changes: 22 additions & 18 deletions src/watchers/twitter/index.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import { TwitterApi, TwitterApiReadOnly } from "twitter-api-v2";
import { TwitterApiRateLimitPlugin } from "@twitter-api-v2/plugin-rate-limit";
import { Cursor, Rettiwt, User } from "rettiwt-api";

import { BaseWatcher, BaseWatcherOptions, PartialUser } from "@watchers/base";

export interface TwitterWatcherOptions extends BaseWatcherOptions<TwitterWatcher> {
bearerToken: string;
apiKey: string;
username: string;
}

export class TwitterWatcher extends BaseWatcher<"Twitter"> {
private readonly twitterClient: TwitterApiReadOnly;
private readonly twitterClient: Rettiwt;
private readonly currentUserName: string;

private currentUserId: string | null = null;

public constructor({ bearerToken, username }: TwitterWatcherOptions) {
public constructor({ apiKey, username }: TwitterWatcherOptions) {
super("Twitter");
this.twitterClient = new TwitterApi(bearerToken, { plugins: [new TwitterApiRateLimitPlugin()] }).readOnly;
this.twitterClient = new Rettiwt({ apiKey });
this.currentUserName = username;
}

public async initialize() {
const { data } = await this.twitterClient.v2.userByUsername(this.currentUserName, {
"user.fields": ["id"],
});

const data = await this.twitterClient.user.details(this.currentUserName);
if (!data) {
throw new Error("Failed to get user id");
}
Expand All @@ -37,16 +34,23 @@ export class TwitterWatcher extends BaseWatcher<"Twitter"> {
throw new Error("Watcher is not initialized");
}

const followers = await this.twitterClient.v2.followers(this.currentUserId, {
max_results: 1000,
"user.fields": ["id", "name", "username", "profile_image_url"],
});
const users: User[] = [];
let cursor: Cursor | null = null;
while (true) {
const followers = await this.twitterClient.user.followers(this.currentUserId, 100, cursor?.value);
if (!followers.next.value || followers.list.length === 0) {
break;
}

users.push(...followers.list);
cursor = followers.next;
}

return followers.data.map(user => ({
return users.map(user => ({
uniqueId: user.id,
displayName: user.name,
userId: user.username,
profileUrl: `https://twitter.com/${user.username}`,
displayName: user.fullName,
userId: user.userName,
profileUrl: `https://twitter.com/${user.userName}`,
}));
}
}
Loading

0 comments on commit 18e6374

Please sign in to comment.