mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
cache lrt category id to db
This commit is contained in:
@@ -37,3 +37,4 @@ lerna-debug.log*
|
|||||||
|
|
||||||
# other
|
# other
|
||||||
.env
|
.env
|
||||||
|
/data
|
||||||
|
|||||||
Generated
+2098
-117
File diff suppressed because it is too large
Load Diff
+5
-1
@@ -27,9 +27,13 @@
|
|||||||
"@nestjs/core": "^9.0.0",
|
"@nestjs/core": "^9.0.0",
|
||||||
"@nestjs/platform-express": "^9.0.0",
|
"@nestjs/platform-express": "^9.0.0",
|
||||||
"@nestjs/serve-static": "^3.0.0",
|
"@nestjs/serve-static": "^3.0.0",
|
||||||
|
"@nestjs/typeorm": "^9.0.1",
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
"rimraf": "^3.0.2",
|
"rimraf": "^3.0.2",
|
||||||
"rxjs": "^7.2.0"
|
"rxjs": "^7.2.0",
|
||||||
|
"sqlite": "^4.1.2",
|
||||||
|
"sqlite3": "^5.1.2",
|
||||||
|
"typeorm": "^0.3.11"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@nestjs/cli": "^9.0.0",
|
"@nestjs/cli": "^9.0.0",
|
||||||
|
|||||||
@@ -4,10 +4,17 @@ import { firstValueFrom } from 'rxjs';
|
|||||||
import { SearchResponseInterface } from '../Interface/search-response.interface';
|
import { SearchResponseInterface } from '../Interface/search-response.interface';
|
||||||
import { SearchResponseDto } from '../Dto/search-response.dto';
|
import { SearchResponseDto } from '../Dto/search-response.dto';
|
||||||
import { SearchResponseItemDto } from '../Dto/search-response-item.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()
|
@Injectable()
|
||||||
export class LrtApiSearchClient {
|
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> {
|
async searchCategories(query: string): Promise<SearchResponseDto> {
|
||||||
query = query === 'viskas' ? '' : query;
|
query = query === 'viskas' ? '' : query;
|
||||||
@@ -46,6 +53,36 @@ export class LrtApiSearchClient {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private async findCategoryId(categoryUrl: string): Promise<string> {
|
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
|
//todo - cache to database
|
||||||
const url = 'https://www.lrt.lt' + categoryUrl;
|
const url = 'https://www.lrt.lt' + categoryUrl;
|
||||||
const resp = await firstValueFrom(this.httpService.get(url));
|
const resp = await firstValueFrom(this.httpService.get(url));
|
||||||
@@ -55,7 +92,9 @@ export class LrtApiSearchClient {
|
|||||||
const interim = body.indexOf('"', start);
|
const interim = body.indexOf('"', start);
|
||||||
const end = body.indexOf('"', interim + 1);
|
const end = body.indexOf('"', interim + 1);
|
||||||
if (end > interim) {
|
if (end > interim) {
|
||||||
return body.substring(interim + 1, end);
|
const catId = body.substring(interim + 1, end);
|
||||||
|
this.saveCategory(categoryUrl, catId);
|
||||||
|
return catId;
|
||||||
} else {
|
} else {
|
||||||
throw 'unexpected response body 1';
|
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 { LrtApiClient } from './lrt-api.client';
|
||||||
import { LrtApiCategoryClient } from './Client/lrt-api-category.client';
|
import { LrtApiCategoryClient } from './Client/lrt-api-category.client';
|
||||||
import { LrtApiPlaylistClient } from './Client/lrt-api-playlist.client';
|
import { LrtApiPlaylistClient } from './Client/lrt-api-playlist.client';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { LrtCategory } from './Entity/lrt-category.entity';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
HttpModule.register({
|
HttpModule.register({
|
||||||
withCredentials: true,
|
withCredentials: true,
|
||||||
}),
|
}),
|
||||||
|
TypeOrmModule.forFeature([LrtCategory])
|
||||||
],
|
],
|
||||||
providers: [
|
providers: [
|
||||||
LrtApiSearchClient,
|
LrtApiSearchClient,
|
||||||
|
|||||||
@@ -9,11 +9,15 @@ import { join } from 'path';
|
|||||||
import { StaticController } from './static.controller';
|
import { StaticController } from './static.controller';
|
||||||
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
|
import { RemoveQueryUrlRewriteMiddleware } from './UrlRewrite/url-rewrite.middleware';
|
||||||
import { StaticService } from './static.service';
|
import { StaticService } from './static.service';
|
||||||
|
import { ConfigModule } from '@nestjs/config';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [
|
imports: [
|
||||||
|
ConfigModule.forRoot({
|
||||||
|
isGlobal: true,
|
||||||
|
}),
|
||||||
ServeStaticModule.forRoot({
|
ServeStaticModule.forRoot({
|
||||||
rootPath: join(__dirname, '../../../static'),
|
rootPath: join(__dirname, process.env.STATIC_SERVE_FOLDER),
|
||||||
}),
|
}),
|
||||||
],
|
],
|
||||||
controllers: [StaticController],
|
controllers: [StaticController],
|
||||||
|
|||||||
+12
-1
@@ -2,9 +2,20 @@ import { Module } from '@nestjs/common';
|
|||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { KodiApiModule } from './KodiApi/kodi-api.module';
|
import { KodiApiModule } from './KodiApi/kodi-api.module';
|
||||||
import { StaticModule } from './Static/static.module';
|
import { StaticModule } from './Static/static.module';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
import { LrtCategory } from './KodiApi/LRT/LrtApiClient/Entity/lrt-category.entity';
|
||||||
|
|
||||||
@Module({
|
@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: [],
|
controllers: [],
|
||||||
providers: [],
|
providers: [],
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { INestApplication } from '@nestjs/common';
|
import { INestApplication } from '@nestjs/common';
|
||||||
import * as request from 'supertest';
|
import * as request from 'supertest';
|
||||||
import { AppModule } from './../src/app.module';
|
import { AppModule } from '../src/app.module';
|
||||||
|
|
||||||
describe('AppController (e2e)', () => {
|
describe('AppController (e2e)', () => {
|
||||||
let app: INestApplication;
|
let app: INestApplication;
|
||||||
|
|||||||
Reference in New Issue
Block a user