-
Notifications
You must be signed in to change notification settings - Fork 5k
/
Copy pathgettweets-copy.mjs
76 lines (60 loc) · 2.41 KB
/
gettweets-copy.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { Scraper } from "agent-twitter-client";
import dotenv from "dotenv";
import fs from "fs";
dotenv.config();
const TWEETS_FILE = "tweets.json";
(async () => {
try {
// Create a new instance of the Scraper
const scraper = new Scraper();
// Log in to Twitter using the configured environment variables
await scraper.login(
process.env.TWITTER_USERNAME,
process.env.TWITTER_PASSWORD
);
// Check if login was successful
if (await scraper.isLoggedIn()) {
console.log("Logged in successfully!");
// Fetch all tweets for the user "@realdonaldtrump"
const tweets = scraper.getTweets("pmarca", 2000);
// Initialize an empty array to store the fetched tweets
let fetchedTweets = [];
// Load existing tweets from the JSON file if it exists
if (fs.existsSync(TWEETS_FILE)) {
const fileContent = fs.readFileSync(TWEETS_FILE, "utf-8");
fetchedTweets = JSON.parse(fileContent);
}
// skip first 200
let count = 0;
// Fetch and process tweets
for await (const tweet of tweets) {
if (count < 1000) {
count++;
continue;
}
console.log("--------------------");
console.log("Tweet ID:", tweet.id);
console.log("Text:", tweet.text);
console.log("Created At:", tweet.createdAt);
console.log("Retweets:", tweet.retweetCount);
console.log("Likes:", tweet.likeCount);
console.log("--------------------");
// Add the new tweet to the fetched tweets array
fetchedTweets.push(tweet);
// Save the updated fetched tweets to the JSON file
fs.writeFileSync(
TWEETS_FILE,
JSON.stringify(fetchedTweets, null, 2)
);
}
console.log("All tweets fetched and saved to", TWEETS_FILE);
// Log out from Twitter
await scraper.logout();
console.log("Logged out successfully!");
} else {
console.log("Login failed. Please check your credentials.");
}
} catch (error) {
console.error("An error occurred:", error);
}
})();