import { Controller, Get, Injector } from "@nger/core"; import { User } from "@nger/entities"; import { Body, Post, Query } from "@nger/http"; import { BROWSER } from '@nger/puppeteer' import { TaskManager, RabbitmqStarter } from "@nger/rabbitmq"; import { Db } from "@nger/typeorm"; import { CryptoService } from "@nger/utils"; import { Page } from 'puppeteer'; import { SpilderError } from "../adapter/spilder"; import { SpilderFactory } from "../adapter/spilder-factory"; import { WbPlatform } from "../entities/platform"; import { WbTask } from "../entities/task"; @Controller('@nger/weibo') export class LoginController { get browser() { return this.injector.get(BROWSER) } get rabbitmqStarter() { return this.injector.get(RabbitmqStarter) } page!: Page; constructor(private injector: Injector) { } @Post('loginAdmin') async loginAdmin(@Body('username') username: string, @Body('password') password: string) { if (!username) { return { errno: -1, message: '用户名不能为空' } } if (!password) { return { errno: -1, message: '密码不能为空' } } const old = await this.db.manager.findOne(User, { where: { username } }); if (!old) { return { errno: -1, message: '用户不存在或已删除', data: { username } } } const crypto = this.injector.get(CryptoService) const psd = crypto.md5(password, old.salt) if (psd === old.password) { return { errno: 200, message: '恭喜您,登录成功', data: { uid: old.uid, username: old.username, email: old.email } } } return { errno: -1, message: '对不起,密码不正确' } } @Post('schedule') async schedule(@Body('eid') eid: number, @Body('time') time: string) { let task = new WbTask(); task.time = time; task.ids = [eid]; task = await this.db.manager.save(WbTask, task); const manager = this.injector.get(TaskManager); await manager.send({ topic: '@nger/weibo/schedule-task', data: task }) return { errno: 200, message: '操作成功' } } @Post('topic') async topic(@Body() where: any) { const manager = this.injector.get(TaskManager); const platforms = await this.db.manager.find(WbPlatform) Promise.all(platforms.map(async it => { await manager.send({ topic: '@nger/weibo/topic-page-links-task', data: { type: it.name, eid: where.eid, keyword: where.keyword } }).catch(e => { console.error(e.message); throw e; }) })) return { code: 200, msg: `成功创建${platforms.length}个爬取任务` } } get db() { return this.injector.get(Db) } get spilderFactory() { return this.injector.get(SpilderFactory) } @Get('start') async start() { try { await this.rabbitmqStarter.start(); return { code: 200 } } catch (e: any) { return { code: 0, message: e.message } } } @Get('stop') async stop() { this.rabbitmqStarter.stop(); return { code: 200 } } @Get('loginSuccess') async loginSuccess(@Query('type') type: string) { const spilder = await this.spilderFactory.create(type); const platform = await spilder.loginSuccess(); if (platform) { return { code: 200, data: { state: true, user: platform } } } return { code: 0, msg: '失败' } } @Get('hasScanQrcode') async hasScanQrcode(@Query('type') type: string) { try { const spilder = await this.spilderFactory.create(type); const txt = await spilder.checkScanStatus(); spilder.delay(200); if (txt) { return { code: 200, data: txt }; } } catch (e) { if (e instanceof SpilderError) { return { code: e.code, msg: e.message } } return { code: 0 } } } @Get('moreChildComment') async moreChildComment() { const data = { "type": "weibo", "eid": 5, "tid": 1291, "aid": 39654, "max_id": 185407668359, "id": 4795789932760176, "uid": 1784473157 } const spilder = await this.spilderFactory.create(data.type); const txt = await spilder.loadMoreChildComments(data.id, data.uid, data.max_id); if (txt) { return { code: 200, data: txt }; } else { return { code: 0 } } } @Get('login') async login(@Query('type') type: string) { try { const spilder = await this.spilderFactory.create(type); const qrcode = await spilder.createLoginQrCode(); if (qrcode) { return { code: 200, data: { src: qrcode, status: false } } } return { code: 200, data: { status: true } } } catch (e: any) { if (e instanceof SpilderError) { return { code: e.code, msg: e.message } } return { code: 0, msg: e.message } } } }