mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-09 04:47:41 +00:00
cache lrt category id to db
This commit is contained in:
@@ -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,
|
||||
|
||||
Reference in New Issue
Block a user