-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlike-comunity.js
107 lines (81 loc) · 3.32 KB
/
like-comunity.js
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
const puppeteer = require('puppeteer');
const COMMUNITY_URL = '';
const USERNAME = '';
const PASSWORD = '';
(async () => {
const browser = await puppeteer.launch({ headless: false });
const page = await browser.newPage();
try {
console.log('Connexion à Twitter/X...');
await page.goto('https://x.com/login', { waitUntil: 'networkidle2' });
await page.waitForSelector('input[name="text"]', { visible: true });
await page.type('input[name="text"]', USERNAME);
await page.evaluate(() => {
const buttons = Array.from(document.querySelectorAll('button[role="button"]'));
const nextButton = buttons.find(button =>
button.innerText.includes('Next') || button.innerText.includes('Suivant')
);
if (nextButton) nextButton.click();
});
await page.waitForSelector('input[name="password"]', { visible: true });
await page.type('input[name="password"]', PASSWORD);
await page.evaluate(() => {
const buttons = Array.from(document.querySelectorAll('button[role="button"]'));
const loginButton = buttons.find(button =>
button.innerText.includes('Login') || button.innerText.includes('Se connecter')
);
if (loginButton) loginButton.click();
});
await page.waitForNavigation({ waitUntil: 'networkidle2' });
console.log('Navigation vers la communauté...');
await page.goto(COMMUNITY_URL, { waitUntil: 'networkidle2' });
console.log('Recherche de l’onglet "Latest"...');
await page.waitForSelector('a[role="tab"]', { visible: true });
const latestTab = await page.$('::-p-xpath(//a[@role="tab"]//span[contains(text(), "Latest") or contains(text(), "Les plus récents")])');
if (latestTab) {
await latestTab.click();
console.log('Onglet "Latest" cliqué avec succès.');
} else {
console.error('Impossible de trouver l’onglet "Latest".');
}
let lastTweetTimestamp = null;
while (true) {
console.log('Recherche des tweets récents...');
await page.waitForSelector('article', { visible: true });
const tweets = await page.$$('article');
const tweetData = [];
for (const tweet of tweets) {
const isPinned = await tweet.evaluate(el =>
el.innerText.includes('Pinned by Community mods')
);
if (isPinned) continue;
const timestamp = await tweet.evaluate(el =>
el.querySelector('time') ? el.querySelector('time').getAttribute('datetime') : null
);
tweetData.push({
tweet,
timestamp
});
}
tweetData.sort((a, b) => new Date(b.timestamp) - new Date(a.timestamp));
for (const { tweet, timestamp } of tweetData) {
if (timestamp && timestamp !== lastTweetTimestamp) {
const likeButton = await tweet.$('button[data-testid="like"]');
if (likeButton) {
await likeButton.click();
console.log('Tweet récent liké:', timestamp);
lastTweetTimestamp = timestamp;
break;
}
}
}
console.log('Attente avant la prochaine vérification...');
await new Promise(resolve => setTimeout(resolve, 10000));
await page.reload({ waitUntil: 'networkidle2' });
}
} catch (error) {
console.error('Erreur:', error);
} finally {
await browser.close();
}
})();