diff --git a/README.md b/README.md index 55b2798..76381dc 100644 --- a/README.md +++ b/README.md @@ -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. ``` @@ -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. @@ -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'); ``` \ No newline at end of file diff --git a/src/scraper.ts b/src/scraper.ts index 116866e..6ce205e 100644 --- a/src/scraper.ts +++ b/src/scraper.ts @@ -23,7 +23,6 @@ import { getTweetAnonymous, getTweets, getLatestTweet, - getLikedTweets, getTweetWhere, getTweetsWhere, getTweetsByUserId, @@ -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 { - return getLikedTweets(user, maxTweets, this.auth); - } - /** * Fetches tweets from a Twitter user using their ID. * @param userId The user whose tweets should be returned. diff --git a/src/tweets.ts b/src/tweets.ts index f0c07bc..041ef97 100644 --- a/src/tweets.ts +++ b/src/tweets.ts @@ -374,24 +374,6 @@ export async function fetchLikedTweets( return parseTimelineTweetsV2(res.value); } -export function getLikedTweets( - user: string, - maxTweets: number, - auth: TwitterAuth, -): AsyncGenerator { - 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, query: TweetQuery,