160 lines
4.7 KiB
JavaScript
160 lines
4.7 KiB
JavaScript
/**
|
|
* 微博自动发布 - 最终版本
|
|
*/
|
|
|
|
const puppeteer = require('puppeteer-core');
|
|
const { getLaunchOptions } = require('../config/browser');
|
|
const fs = require('fs');
|
|
|
|
const COOKIE_FILE = '/home/ubuntu/weibo_cookies.txt';
|
|
const TEST_CONTENT = '🤖 这是一条来自自动化脚本的测试微博\n时间:' + new Date().toLocaleString('zh-CN') + '\n#自动化测试 #机器人';
|
|
|
|
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
async function postWeibo() {
|
|
console.log('🌐 启动浏览器...');
|
|
|
|
const browser = await puppeteer.launch({
|
|
...getLaunchOptions(),
|
|
headless: true,
|
|
});
|
|
|
|
try {
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 900 });
|
|
|
|
// 加载 Cookie
|
|
console.log('🍪 加载 Cookie...');
|
|
const cookies = JSON.parse(fs.readFileSync(COOKIE_FILE, 'utf-8'));
|
|
await page.goto('https://weibo.com', { waitUntil: 'domcontentloaded' });
|
|
|
|
for (const c of cookies) {
|
|
try {
|
|
await page.setCookie({
|
|
name: c.name,
|
|
value: c.value,
|
|
domain: c.domain,
|
|
path: c.path || '/',
|
|
secure: c.secure || false,
|
|
httpOnly: c.httpOnly || false
|
|
});
|
|
} catch (e) {}
|
|
}
|
|
|
|
// 访问主页
|
|
console.log('🔄 访问微博主页...');
|
|
await page.goto('https://weibo.com', { waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
|
|
await page.screenshot({ path: '/tmp/weibo_login/step1_home.png' });
|
|
|
|
// 查找并点击"写微博"按钮
|
|
console.log('🖱️ 查找"写微博"按钮...');
|
|
const writeBtn = await page.$eval('button', buttons => {
|
|
const btns = Array.from(document.querySelectorAll('button'));
|
|
return btns.find(b => b.textContent?.includes('写微博'));
|
|
}).catch(() => null);
|
|
|
|
// 通过文本查找
|
|
let found = false;
|
|
const allBtns = await page.$$('button, a, div[role="button"]');
|
|
for (const btn of allBtns) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('写微博')) {
|
|
console.log(` 找到: "${text.trim()}"`);
|
|
await btn.click();
|
|
found = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!found) {
|
|
console.log('⚠️ 未找到"写微博"按钮,尝试直接访问compose页面');
|
|
await page.goto('https://weibo.com/compose', { waitUntil: 'networkidle2' });
|
|
} else {
|
|
await delay(2000);
|
|
}
|
|
|
|
await page.screenshot({ path: '/tmp/weibo_login/step2_compose.png' });
|
|
|
|
// 查找发布框
|
|
console.log('📝 查找发布框...');
|
|
const selectors = [
|
|
'textarea[placeholder*="有什么新鲜事"]',
|
|
'textarea[placeholder*="想说什么"]',
|
|
'textarea.woo-input-main',
|
|
'[class*="input"] textarea',
|
|
'textarea',
|
|
];
|
|
|
|
let postBox = null;
|
|
for (const sel of selectors) {
|
|
postBox = await page.$(sel);
|
|
if (postBox) {
|
|
console.log(` 找到: ${sel}`);
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!postBox) {
|
|
// 获取所有textarea
|
|
const textareas = await page.$$('textarea');
|
|
console.log(` 尝试 ${textareas.length} 个 textarea`);
|
|
if (textareas.length > 0) {
|
|
postBox = textareas[0];
|
|
}
|
|
}
|
|
|
|
if (postBox) {
|
|
// 输入内容
|
|
await postBox.click();
|
|
await delay(500);
|
|
await postBox.type(TEST_CONTENT, { delay: 30 });
|
|
console.log('✍️ 已输入:', TEST_CONTENT.substring(0, 50) + '...');
|
|
|
|
await delay(1000);
|
|
await page.screenshot({ path: '/tmp/weibo_login/step3_typed.png' });
|
|
|
|
// 查找发送按钮
|
|
console.log('🔍 查找发送按钮...');
|
|
let sendBtn = null;
|
|
|
|
const allBtns2 = await page.$$('button, a, div[role="button"]');
|
|
for (const btn of allBtns2) {
|
|
const text = await page.evaluate(el => el.textContent, btn);
|
|
if (text?.includes('发送') || text?.includes('发布')) {
|
|
console.log(` 找到: "${text.trim()}"`);
|
|
sendBtn = btn;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (sendBtn) {
|
|
console.log('🚀 点击发送...');
|
|
await sendBtn.click();
|
|
await delay(5000);
|
|
|
|
await page.screenshot({ path: '/tmp/weibo_login/step4_sent.png' });
|
|
console.log('✅ 发送完成!');
|
|
|
|
// 检查是否成功
|
|
const currentUrl = page.url();
|
|
if (currentUrl.includes('weibo.com')) {
|
|
console.log('🎉 微博发布流程执行完毕!');
|
|
}
|
|
} else {
|
|
console.log('⚠️ 未找到发送按钮');
|
|
}
|
|
} else {
|
|
console.log('❌ 未找到发布框');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 错误:', error.message);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
postWeibo().catch(console.error);
|