Commit 599f70ca authored by nanahira's avatar nanahira

remake for latest biu

parent fdfe4b01
Pipeline #3387 passed with stages
in 18 minutes
This diff is collapsed.
import { Controller, Get } from '@nestjs/common'; import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service'; import { AppService } from './app.service';
import { ApiTags } from '@nestjs/swagger';
@Controller() @Controller()
export class AppController { export class AppController {
constructor(private readonly appService: AppService) {} constructor(private readonly appService: AppService) {}
@Get('tips.json') @Get('tips.json')
@ApiTags('tips')
async getTips(): Promise<string[]> { async getTips(): Promise<string[]> {
return await this.appService.getTips(); return await this.appService.getTips();
} }
......
...@@ -4,18 +4,32 @@ import moment from 'moment'; ...@@ -4,18 +4,32 @@ import moment from 'moment';
import PQueue from 'p-queue'; import PQueue from 'p-queue';
import axios from 'axios'; import axios from 'axios';
import _ from 'lodash'; import _ from 'lodash';
import qs from 'qs';
import cookie from 'cookie';
const blacklistedWords = ['', '', '', '', '[Vv]ocal', ':', '']; const blacklistedWords = ['', '', '', '', '[Vv]ocal', ':', ''];
const blacklistedRegex = blacklistedWords.map((word) => new RegExp(word)); const blacklistedRegex = blacklistedWords.map((word) => new RegExp(word));
interface LoginInfo {
email: string;
password: string;
}
@Injectable() @Injectable()
export class AppService { export class AppService {
cache: string[]; cache: string[];
lastFetchTime: moment.Moment; lastFetchTime: moment.Moment;
queue: PQueue; queue: PQueue;
loginInfo: LoginInfo;
constructor(private log: AppLogger) { constructor(private log: AppLogger) {
this.log.setContext('ygopro-tips-biu-generator'); this.log.setContext('ygopro-tips-biu-generator');
this.queue = new PQueue({ concurrency: 1 }); this.queue = new PQueue({ concurrency: 1 });
if (process.env.BIU_EMAIL && process.env.BIU_PASSWORD) {
this.loginInfo = {
email: process.env.BIU_EMAIL,
password: process.env.BIU_PASSWORD,
};
}
} }
async getTips(): Promise<string[]> { async getTips(): Promise<string[]> {
...@@ -68,11 +82,49 @@ export class AppService { ...@@ -68,11 +82,49 @@ export class AppService {
} }
} }
async getLoginCookie() {
if (!this.loginInfo) {
return undefined;
}
try {
this.log.log(`Logging in.`);
const ret = await axios.post(
'https://web.biu.moe/User/doLogin',
qs.stringify(this.loginInfo),
{
responseType: 'json',
timeout: 30000,
},
);
if (!ret.data.status) {
this.log.error(`Login failed: ${ret.data.message}`);
return undefined;
}
const setCookieContent = ret.headers['set-cookie'] as string[];
if (!setCookieContent) {
return undefined;
}
const contentHits = setCookieContent.find((c) => c.includes('biuInfo'));
if (!contentHits) {
return undefined;
}
const token = cookie.parse(contentHits).biuInfo as string;
this.log.log(`Login success: ${token}`);
return cookie.serialize('biuInfo', token);
} catch (e) {
this.log.error(`Login error: ${e.toString()}`);
return undefined;
}
}
async fetchTipsTask(): Promise<string[]> { async fetchTipsTask(): Promise<string[]> {
try { try {
this.log.log(`Fetching song list.`); this.log.log(`Fetching song list.`);
const { data } = await axios.get('https://web.biu.moe/Index/home', { const { data } = await axios.get('https://web.biu.moe/Index/home', {
responseType: 'document', responseType: 'document',
headers: {
Cookie: (await this.getLoginCookie()) || '',
},
timeout: 30000, timeout: 30000,
}); });
const urls = (data as string).match(/href="\/s(\d+)"/g); const urls = (data as string).match(/href="\/s(\d+)"/g);
......
import { NestFactory } from '@nestjs/core'; import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
async function bootstrap() { async function bootstrap() {
const app = await NestFactory.create(AppModule); const app = await NestFactory.create(AppModule);
app.enableCors(); app.enableCors();
const documentConfig = new DocumentBuilder()
.setTitle('ygopro-tips-biu-generator')
.setDescription('YGOPro Tips 生成工具')
.setVersion('1.0')
.addTag('tips')
.build();
const document = SwaggerModule.createDocument(app, documentConfig);
SwaggerModule.setup('docs', app, document);
await app.listen(3000); await app.listen(3000);
} }
bootstrap(); bootstrap();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment