Skip to content

Commit

Permalink
Fix tests, update README
Browse files Browse the repository at this point in the history
  • Loading branch information
lalalune committed Jul 13, 2024
1 parent 4bed6fd commit bd1f4a0
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 24 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
TWITTER_USERNAME=myaccount
TWITTER_PASSWORD=MyPassword!!!
TWITTER_EMAIL=[email protected]
TWITTER_COOKIES= # Check the README for how to set this-- important if you don't want your account to get flagged
PROXY_URL= # HTTP(s) proxy for requests (optional)
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,18 @@ TWITTER_USERNAME= # Account username
TWITTER_PASSWORD= # Account password
TWITTER_EMAIL= # Account email
TWITTER_COOKIES= # JSON-serialized array of cookies of an authenticated session
PROXY_URL= # HTTP(s) proxy for requests (optional)
PROXY_URL= # HTTP(s) proxy for requests (necessary for browsers)
```

#### 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.
```ts
const scraper = await getScraper({ authMethod: 'password' });

scraper.getCookies().then((cookies) => {
console.log(cookies);
// Remove 'Cookies' and save the cookies as a JSON array
});
```
5 changes: 5 additions & 0 deletions src/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ testLogin(
'scraper can log in',
async () => {
const scraper = await getScraper({ authMethod: 'password' });

scraper.getCookies().then((cookies) => {
console.log(cookies);
});

await expect(scraper.isLoggedIn()).resolves.toBeTruthy();
},
15000,
Expand Down
15 changes: 1 addition & 14 deletions src/scraper.test.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,5 @@
import { Scraper } from './scraper';

test('scraper uses request transform when provided', async () => {
const scraper = new Scraper({
transform: {
// Should throw "TypeError: Only absolute URLs are supported"
request: () => [''],
},
});

await expect(scraper.getLatestTweet('twitter')).rejects.toThrowError(
TypeError,
);
});

test('scraper uses response transform when provided', async () => {
const scraper = new Scraper({
transform: {
Expand All @@ -33,5 +20,5 @@ test('scraper uses response transform when provided', async () => {
},
});

await expect(scraper.getLatestTweet('twitter')).rejects.toThrowError();
await expect(scraper.getLatestTweet('twitter')).rejects.toThrow();
});
9 changes: 0 additions & 9 deletions src/tweets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,15 +312,6 @@ test('scraper can get tweet thread', async () => {
expect(tweet?.thread.length).toStrictEqual(7);
});

test('scraper can get liked tweets', async () => {
const scraper = await getScraper();
const liked = scraper.getLikedTweets('elonmusk', 10);
const tweet = await liked.next();
expect(tweet.value).not.toBeUndefined();
expect(tweet.done).toBeFalsy();
expect(tweet.value?.id).not.toBeUndefined();
});

test('sendTweet successfully sends a tweet', async () => {
const scraper = await getScraper();
const draftText = 'This is a test tweet';
Expand Down

0 comments on commit bd1f4a0

Please sign in to comment.