cache lrt category id to db

This commit is contained in:
Jurgis Sakalauskas
2022-12-08 15:34:49 +02:00
parent e1fe99c72f
commit a7b53752e6
9 changed files with 2177 additions and 123 deletions
@@ -4,10 +4,17 @@ import { firstValueFrom } from 'rxjs';
import { SearchResponseInterface } from '../Interface/search-response.interface';
import { SearchResponseDto } from '../Dto/search-response.dto';
import { SearchResponseItemDto } from '../Dto/search-response-item.dto';
import { InjectRepository } from '@nestjs/typeorm';
import { LrtCategory } from '../Entity/lrt-category.entity';
import { FindOptionsUtils, Repository } from 'typeorm';
@Injectable()
export class LrtApiSearchClient {
constructor(private readonly httpService: HttpService) {}
constructor(
private readonly httpService: HttpService,
@InjectRepository(LrtCategory)
private lrtCategoriesRepository: Repository<LrtCategory>,
) {}
async searchCategories(query: string): Promise<SearchResponseDto> {
query = query === 'viskas' ? '' : query;
@@ -46,6 +53,36 @@ export class LrtApiSearchClient {
}
private async findCategoryId(categoryUrl: string): Promise<string> {
const catId = await this.findCategoryIdInDb(categoryUrl);
if (catId !== null) {
return catId;
}
return this.findCategoryIdInLrt(categoryUrl);
}
private saveCategory(categoryUrl: string, catId: string) {
const cat = new LrtCategory();
cat.categoryUrl = categoryUrl;
cat.categoryId = catId;
this.lrtCategoriesRepository.save(cat);
}
private async findCategoryIdInDb(
categoryUrl: string,
): Promise<string | null> {
const entity: LrtCategory | null = await this.lrtCategoriesRepository
.findOneBy({
categoryUrl: categoryUrl,
})
.catch(() => null);
if (entity !== null) {
return entity.categoryId;
}
return null;
}
private async findCategoryIdInLrt(categoryUrl: string): Promise<string> {
//todo - cache to database
const url = 'https://www.lrt.lt' + categoryUrl;
const resp = await firstValueFrom(this.httpService.get(url));
@@ -55,7 +92,9 @@ export class LrtApiSearchClient {
const interim = body.indexOf('"', start);
const end = body.indexOf('"', interim + 1);
if (end > interim) {
return body.substring(interim + 1, end);
const catId = body.substring(interim + 1, end);
this.saveCategory(categoryUrl, catId);
return catId;
} else {
throw 'unexpected response body 1';
}
@@ -0,0 +1,11 @@
import { Column, Entity, PrimaryColumn } from 'typeorm';
@Entity()
export class LrtCategory {
@PrimaryColumn({ name: 'category_url' })
categoryUrl: string;
@Column({ name: 'category_id' })
categoryId: string;
}
@@ -4,12 +4,15 @@ import { LrtApiSearchClient } from './Client/lrt-api-search.client';
import { LrtApiClient } from './lrt-api.client';
import { LrtApiCategoryClient } from './Client/lrt-api-category.client';
import { LrtApiPlaylistClient } from './Client/lrt-api-playlist.client';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LrtCategory } from './Entity/lrt-category.entity';
@Module({
imports: [
HttpModule.register({
withCredentials: true,
}),
TypeOrmModule.forFeature([LrtCategory])
],
providers: [
LrtApiSearchClient,
+5 -1
View File
@@ -9,11 +9,15 @@ import { join } from 'path';
import { StaticController } from './static.controller';
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
import { StaticService } from './static.service';
import { ConfigModule } from '@nestjs/config';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
}),
ServeStaticModule.forRoot({
rootPath: join(__dirname, '../../../static'),
rootPath: join(__dirname, process.env.STATIC_SERVE_FOLDER),
}),
],
controllers: [StaticController],
+12 -1
View File
@@ -2,9 +2,20 @@ import { Module } from '@nestjs/common';
import { ConfigModule } from '@nestjs/config';
import { KodiApiModule } from './KodiApi/kodi-api.module';
import { StaticModule } from './Static/static.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { LrtCategory } from './KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity';
@Module({
imports: [ConfigModule.forRoot(), KodiApiModule, StaticModule],
imports: [
ConfigModule.forRoot({ isGlobal: true }),
KodiApiModule,
StaticModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: process.env.DB_PATH,
entities: [LrtCategory],
}),
],
controllers: [],
providers: [],
})