From 31129e23f6ca1082002fe6da5823c6f56bd97268 Mon Sep 17 00:00:00 2001 From: NAME Date: Wed, 13 May 2026 11:49:16 +0000 Subject: [PATCH] Udate --- src/sqs-module/sqs.module.ts | 7 +- src/sqs-module/sqs.poster.worker.ts | 13 ++-- src/x-poster/x-browser.service.ts | 94 +++++++++++++++---------- src/x-poster/x-poster.router.service.ts | 2 +- 4 files changed, 70 insertions(+), 46 deletions(-) diff --git a/src/sqs-module/sqs.module.ts b/src/sqs-module/sqs.module.ts index bfed78e..f5b822d 100644 --- a/src/sqs-module/sqs.module.ts +++ b/src/sqs-module/sqs.module.ts @@ -1,12 +1,12 @@ // sqs.module.ts -import { Module, Global } from '@nestjs/common'; -import { SqsService } from './sqs.service'; +import {Global, Module} from '@nestjs/common'; +import {SqsService} from './sqs.service'; import {SqsPostService} from "./sqs.post.service"; import {SqsPosterWorker} from "./sqs.poster.worker"; -import {XPosterRouterService} from "../x-poster/x-poster.router.service"; import {XPosterModule} from "../x-poster/x-poster.module"; import {FacebookApi} from "../x-poster/facebook.api"; import {NotifyService} from "../notify.service"; +import {XCacheService} from "../x-cache/x-cache.service"; @Global() @Module({ @@ -17,6 +17,7 @@ import {NotifyService} from "../notify.service"; SqsPosterWorker, FacebookApi, NotifyService, + XCacheService, ], exports: [SqsService], }) diff --git a/src/sqs-module/sqs.poster.worker.ts b/src/sqs-module/sqs.poster.worker.ts index 7815533..c3cc997 100644 --- a/src/sqs-module/sqs.poster.worker.ts +++ b/src/sqs-module/sqs.poster.worker.ts @@ -5,6 +5,7 @@ import {SUPPORT_SOCIAL_PROVIDERS, XPosterRouterService, XStrategy} from "../x-po import {rand} from "../helper"; import {FacebookApi} from "../x-poster/facebook.api"; import {NotifyService} from "../notify.service"; +import {XCacheService} from "../x-cache/x-cache.service"; @Injectable() export class SqsPosterWorker { @@ -15,6 +16,7 @@ export class SqsPosterWorker { private readonly xRouterService: XPosterRouterService, private readonly facebookApi: FacebookApi, private readonly notifyService: NotifyService, + private readonly xCacheService: XCacheService, ) { } @@ -23,10 +25,13 @@ export class SqsPosterWorker { await this.notifyService.sendMessageToTele(`🚀 Worker started for ${await this.sqs.getQueueName()}`) //check cookie - this.xRouterService.verifyCookie().catch((err) => { - console.error(`SqsPosterWorker_verifyCookie`); - console.error(err); - }); + if (!await this.xCacheService.isXCookiesAlive()) { + this.xRouterService.verifyCookie().catch((err) => { + console.error(`SqsPosterWorker_verifyCookie`); + console.error(err); + }); + + } let ReceiptHandle = ''; while (true) { diff --git a/src/x-poster/x-browser.service.ts b/src/x-poster/x-browser.service.ts index 157a469..9a62405 100644 --- a/src/x-poster/x-browser.service.ts +++ b/src/x-poster/x-browser.service.ts @@ -3,6 +3,8 @@ import {HttpException, Injectable, Logger, OnModuleDestroy, OnModuleInit,} from import {Browser, BrowserContext, chromium, Page} from 'playwright'; import {rand} from "../helper"; import {getAccount} from "./utils/x-headers.util"; +import {XCacheService} from "../x-cache/x-cache.service"; +import {NotifyService} from "../notify.service"; export interface BrowserAccount { accountId: string; @@ -35,6 +37,12 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { private readonly MAX_CONTEXTS = 5; private readonly CONTEXT_TTL_MS = 15 * 60 * 1000; // 15 phút + constructor( + private readonly xCacheService: XCacheService, + private readonly notifyService: NotifyService, + ) { + } + async onModuleInit() { // Lazy launch – chỉ mở khi cần setInterval(() => this.cleanupStaleContexts(), 60_000); @@ -141,7 +149,7 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { return ctx.newPage(); } - async verifyCookie() { + async verifyCookie(sendNotiWhenAlive = false): Promise { const page = await this.newPage(); try { @@ -151,25 +159,8 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { }); await page.waitForTimeout(2000 + (Math.random() + Math.random()) * 3000); await page.mouse.wheel(0, rand(300, 500)); - // Detect login/challenge screen - if (page.url().includes('/home')) { - this.logger.log('Cookies live'); - return true; - // return { - // success: false, - // error: 'Redirected to login', - // needsRelogin: true, - // }; - } - const isLoggedIn = await page - .locator('[data-testid="SideNav_AccountSwitcher_Button"], [data-testid="AppTabBar_Home_Link"]') - .first() - .isVisible() - .catch(() => false); + return await this.isCookieLive(page, sendNotiWhenAlive); - this.logger.log(`🔐 Session restore: ${isLoggedIn ? 'LOGGED IN' : 'GUEST (cookie có thể expired)'}`); - // await page.close(); - return isLoggedIn; } catch (er) { this.logger.error(`Browser verify cookie fail: ${er.message}`); return false; @@ -178,6 +169,49 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { } } + async isCookieLive(page: Page, sendNotiWhenAlive = false): Promise { + this.logger.debug('isCookieLive?'); + if (page.url().includes('/home')) { + this.logger.log('Cookies live'); + await this.xCacheService.setStateXCookiesIsSillALive(); + if (sendNotiWhenAlive) { + await this.notifyService.sendMessageToTele('✅Verify cookie pass') + } + return true; + } + const isLoggedIn = await page + .locator('[data-testid="SideNav_AccountSwitcher_Button"], [data-testid="AppTabBar_Home_Link"]') + .first() + .isVisible() + .catch(() => false); + + this.logger.log(`🔐 Session restore: ${isLoggedIn ? 'LOGGED IN' : 'GUEST (cookie có thể expired)'}`); + if (isLoggedIn) { + await this.xCacheService.setStateXCookiesIsSillALive(); + if (sendNotiWhenAlive) { + await this.notifyService.sendMessageToTele('✅Verify cookie pass') + } + } else { + await this.xCacheService.setStateXCookiesIsDie(); + await this.notifyService.sendUrgentMessageToTele('❌Cookie đã hết hạn vui lòng cập nhập để sử dụng.') + } + return isLoggedIn; + } + + // async actVerifyCookie(): Promise { + // this.logger.debug('==> actVerifyCookie'); + // // const isAlive = await this.cookieSvc.verifyCookie(); + // const isAlive = await this.verifyCookie(); + // if (!isAlive) { + // await this.xCacheService.setStateXCookiesIsDie(); + // await this.notifyService.sendUrgentMessageToTele('❌Cookie đã hết hạn vui lòng cập nhập để sử dụng.') + // } + // await this.xCacheService.setStateXCookiesIsSillALive(); + // await this.notifyService.sendMessageToTele('✅Verify cookie pass') + // + // return isAlive; + // } + async likeTweet(tweetUrl: string) { let page: Page | null = null; @@ -285,12 +319,7 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { }; } - const isLoggedIn = await page - .locator('[data-testid="SideNav_AccountSwitcher_Button"], [data-testid="AppTabBar_Home_Link"]') - .first() - .isVisible() - .catch(() => false); - this.logger.debug(`postTweet: ${isLoggedIn ? 'LOGGED IN' : 'LOGGED OUT'}`); + await this.isCookieLive(page, false); await page.mouse.wheel(200, rand(300, 800)); await page.waitForTimeout(rand(2000, 5000)); @@ -364,13 +393,7 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { await page.waitForTimeout(rand(2000, 4000)); - const isLoggedIn = await page - .locator('[data-testid="SideNav_AccountSwitcher_Button"], [data-testid="AppTabBar_Home_Link"]') - .first() - .isVisible() - .catch(() => false); - this.logger.debug(`postQuote: ${isLoggedIn ? 'LOGGED IN' : 'LOGGED OUT'}`); - + await this.isCookieLive(page, false); // ===== CHECK LOGIN ===== if (await page.locator('input[name="text"]').count()) { @@ -520,12 +543,7 @@ export class XBrowserService implements OnModuleInit, OnModuleDestroy { this.logger.debug(`đợi UI ổn...`) await page.waitForSelector('article', {timeout: 7000}); - const isLoggedIn = await page - .locator('[data-testid="SideNav_AccountSwitcher_Button"], [data-testid="AppTabBar_Home_Link"]') - .first() - .isVisible() - .catch(() => false); - this.logger.debug(`postReply: ${isLoggedIn ? 'LOGGED IN' : 'LOGGED OUT'}`); + await this.isCookieLive(page, false); // scroll nhẹ this.logger.debug(`scroll nhẹ ...`) diff --git a/src/x-poster/x-poster.router.service.ts b/src/x-poster/x-poster.router.service.ts index fa6acb1..5315548 100644 --- a/src/x-poster/x-poster.router.service.ts +++ b/src/x-poster/x-poster.router.service.ts @@ -93,8 +93,8 @@ export class XPosterRouterService { if (['cookie', 'browser'].includes(method)) { if (!canUseCookie) { - await this.notifyService.sendUrgentMessageToTele('❌ Vui lòng cập nhập cookie để sử dụng '); this.logger.error('Cookie đã hết hạn, vui lòng cập nhập'); + await this.notifyService.sendUrgentMessageToTele('❌ Vui lòng cập nhập cookie để sử dụng '); continue; } }