Skip to content

Commit

Permalink
Merge pull request #25 from abcfy2/main
Browse files Browse the repository at this point in the history
feat: add me() api to get logged in user's profile
  • Loading branch information
lalalune authored Dec 20, 2024
2 parents 0920a6d + 0443a7b commit e56e268
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ const profile = await scraper.getProfile('TwitterDev');

// Get a user ID from their screen name
const userId = await scraper.getUserIdByScreenName('TwitterDev');

// Get logged-in user's profile
const me = await scraper.me();
```

### Search
Expand Down
15 changes: 15 additions & 0 deletions src/auth-user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { TwitterApiErrorRaw } from './errors';
import { Type, type Static } from '@sinclair/typebox';
import { Check } from '@sinclair/typebox/value';
import * as OTPAuth from 'otpauth';
import { LegacyUserRaw, parseProfile, type Profile } from './profile';

interface TwitterUserAuthFlowInitRequest {
flow_name: string;
Expand Down Expand Up @@ -53,6 +54,8 @@ type FlowTokenResult = FlowTokenResultSuccess | { status: 'error'; err: Error };
* A user authentication token manager.
*/
export class TwitterUserAuth extends TwitterGuestAuth {
private userProfile: Profile | undefined;

constructor(bearerToken: string, options?: Partial<TwitterAuthOptions>) {
super(bearerToken, options);
}
Expand All @@ -67,9 +70,21 @@ export class TwitterUserAuth extends TwitterGuestAuth {
}

const { value: verify } = res;
this.userProfile = parseProfile(
verify as LegacyUserRaw,
(verify as unknown as { verified: boolean }).verified,
);
return verify && !verify.errors?.length;
}

async me(): Promise<Profile | undefined> {
if (this.userProfile) {
return this.userProfile;
}
await this.isLoggedIn();
return this.userProfile;
}

async login(
username: string,
password: string,
Expand Down
10 changes: 10 additions & 0 deletions src/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { updateCookieJar } from './requests';
import { Headers } from 'headers-polyfill';
import { FetchTransformOptions } from './api';
import { TwitterApi } from 'twitter-api-v2';
import { Profile } from './profile';

export interface TwitterAuthOptions {
fetch: typeof fetch;
Expand Down Expand Up @@ -38,6 +39,11 @@ export interface TwitterAuth {
*/
isLoggedIn(): Promise<boolean>;

/**
* Fetches the current user's profile.
*/
me(): Promise<Profile | undefined>;

/**
* Logs into a Twitter account.
* @param username The username to log in with.
Expand Down Expand Up @@ -151,6 +157,10 @@ export class TwitterGuestAuth implements TwitterAuth {
return Promise.resolve(false);
}

async me(): Promise<Profile | undefined> {
return undefined;
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
login(_username: string, _password: string, _email?: string): Promise<void> {
return this.updateGuestToken();
Expand Down
8 changes: 8 additions & 0 deletions src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -692,6 +692,14 @@ export class Scraper {
);
}

/**
* Returns the currently logged in user
* @returns The currently logged in user
*/
public async me(): Promise<Profile | undefined> {
return this.auth.me();
}

/**
* Login to Twitter as a real Twitter account. This enables running
* searches.
Expand Down

0 comments on commit e56e268

Please sign in to comment.