Skip to content

Commit

Permalink
Update readme and remove user likes
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 13, 2024
1 parent f9c65ea commit e992f4c
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 31 deletions.
113 changes: 111 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ This is a modified version of [@the-convocation/twitter-scraper](https://github.
npm install agent-twitter-client
```

### Setup
## Setup
Configure environment variables for authentication.

```
Expand All @@ -18,7 +18,7 @@ TWITTER_COOKIES= # JSON-serialized array of cookies of an authenticated sess
PROXY_URL= # HTTP(s) proxy for requests (necessary for browsers)
```

#### Getting Twitter Cookies
### Getting Twitter Cookies
It is important that you use Twitter cookies so that you don't send a new login request to twitter every time you want to do something.

In your application, you will probably want to have a check for cookies. If you don't have cookies, log in with user auth credentials. Then, cache the cookies for future use.
Expand All @@ -29,4 +29,113 @@ In your application, you will probably want to have a check for cookies. If you
console.log(cookies);
// Remove 'Cookies' and save the cookies as a JSON array
});
```

## Getting Started
```ts
const scraper = new Scraper();
await scraper.login('username', 'password');
const tweets = await scraper.getTweets('elonmusk', 10);
const tweetsAndReplies = scraper.getTweetsAndReplies('elonmusk');
const latestTweet = await scraper.getLatestTweet('elonmusk');
const tweet = await scraper.getTweet('1234567890123456789');
await scraper.sendTweet('Hello world!');
```

## API

### Authentication
```ts
// Log in
await scraper.login('username', 'password');

// Log out
await scraper.logout();

// Check if logged in
const isLoggedIn = await scraper.isLoggedIn();

// Get current session cookies
const cookies = await scraper.getCookies();

// Set current session cookies
await scraper.setCookies(cookies);

// Clear current cookies
await scraper.clearCookies();
```

### Profile
```ts
// Get a user's profile
const profile = await scraper.getProfile('TwitterDev');

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

### Search
```ts
import { SearchMode } from 'agent-twitter-client';

// Search for recent tweets
const tweets = scraper.searchTweets('#nodejs', 20, SearchMode.Latest);

// Search for profiles
const profiles = scraper.searchProfiles('John', 10);

// Fetch a page of tweet results
const results = await scraper.fetchSearchTweets('#nodejs', 20, SearchMode.Top);

// Fetch a page of profile results
const profileResults = await scraper.fetchSearchProfiles('John', 10);
```

### Relationships
```ts
// Get a user's followers
const followers = scraper.getFollowers('12345', 100);

// Get who a user is following
const following = scraper.getFollowing('12345', 100);

// Fetch a page of a user's followers
const followerResults = await scraper.fetchProfileFollowers('12345', 100);

// Fetch a page of who a user is following
const followingResults = await scraper.fetchProfileFollowing('12345', 100);
```

### Trends
```ts
// Get current trends
const trends = await scraper.getTrends();

// Fetch tweets from a list
const listTweets = await scraper.fetchListTweets('1234567890', 50);
```

### Tweets
```ts
// Get a user's tweets
const tweets = scraper.getTweets('TwitterDev');

// Get a user's liked tweets
const likedTweets = scraper.getLikedTweets('TwitterDev');

// Get a user's tweets and replies
const tweetsAndReplies = scraper.getTweetsAndReplies('TwitterDev');

// Get tweets matching specific criteria
const timeline = scraper.getTweets('TwitterDev', 100);
const retweets = await scraper.getTweetsWhere(
timeline,
(tweet) => tweet.isRetweet
);

// Get a user's latest tweet
const latestTweet = await scraper.getLatestTweet('TwitterDev');

// Get a specific tweet by ID
const tweet = await scraper.getTweet('1234567890123456789');
```
11 changes: 0 additions & 11 deletions src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import {
getTweetAnonymous,
getTweets,
getLatestTweet,
getLikedTweets,
getTweetWhere,
getTweetsWhere,
getTweetsByUserId,
Expand Down Expand Up @@ -251,16 +250,6 @@ export class Scraper {
return getTweets(user, maxTweets, this.auth);
}

/**
* Fetches liked tweets from a Twitter user. Requires authentication.
* @param user The user whose likes should be returned.
* @param maxTweets The maximum number of tweets to return. Defaults to `200`.
* @returns An {@link AsyncGenerator} of liked tweets from the provided user.
*/
public getLikedTweets(user: string, maxTweets = 200): AsyncGenerator<Tweet> {
return getLikedTweets(user, maxTweets, this.auth);
}

/**
* Fetches tweets from a Twitter user using their ID.
* @param userId The user whose tweets should be returned.
Expand Down
18 changes: 0 additions & 18 deletions src/tweets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,24 +374,6 @@ export async function fetchLikedTweets(
return parseTimelineTweetsV2(res.value);
}

export function getLikedTweets(
user: string,
maxTweets: number,
auth: TwitterAuth,
): AsyncGenerator<Tweet, void> {
return getTweetTimeline(user, maxTweets, async (q, mt, c) => {
const userIdRes = await getUserIdByScreenName(q, auth);

if (!userIdRes.success) {
throw userIdRes.err;
}

const { value: userId } = userIdRes;

return fetchLikedTweets(userId, mt, c, auth);
});
}

export async function getTweetWhere(
tweets: AsyncIterable<Tweet>,
query: TweetQuery,
Expand Down

0 comments on commit e992f4c

Please sign in to comment.