diff --git a/src/KodiApi/LRT/LrtApiClient/Interface/search-response-item.interface.ts b/src/KodiApi/LRT/LrtApiClient/Interface/search-response-item.interface.ts new file mode 100644 index 0000000..61fcbf9 --- /dev/null +++ b/src/KodiApi/LRT/LrtApiClient/Interface/search-response-item.interface.ts @@ -0,0 +1,32 @@ +interface SearchResponseItemInterface { + photo_horizontal: number; + category_img_path_postfix: string; + age_restriction: string; + badges_html: string; + article_has_audiovideo: number; + img_w_h: string | number; + photo_horizontal_small: number; + category_title: string; + item_date: string; + category_img_path_prefix: string; + img_path_prefix: string; + is_audio: number; + category_url: string; + img_path_postfix: string; + heritage: number; + type: number; + fb_share_count: number; + id: number; + title: string; + url: string; + photo_id: number; + main_category_title: string; + is_video: number; + channel: number; + curr_category_title: string; + is_videoteka: number; + read_count: number; + n18: number; + subtitle: string; + photo_count: number; +} diff --git a/src/KodiApi/LRT/LrtApiClient/Interface/search-response.interface.ts b/src/KodiApi/LRT/LrtApiClient/Interface/search-response.interface.ts new file mode 100644 index 0000000..e731778 --- /dev/null +++ b/src/KodiApi/LRT/LrtApiClient/Interface/search-response.interface.ts @@ -0,0 +1,10 @@ +interface SearchResponseInterface { + q: string; + meta: { + time: number; + total: number; + total_found: number; + }; + total_found: string | number; + items: Array; +} diff --git a/src/KodiApi/LRT/lrt-api.client.ts b/src/KodiApi/LRT/lrt-api.client.ts new file mode 100644 index 0000000..3402d7e --- /dev/null +++ b/src/KodiApi/LRT/lrt-api.client.ts @@ -0,0 +1,62 @@ +import { Injectable } from '@nestjs/common'; +import { HttpService } from '@nestjs/axios'; +import { firstValueFrom } from 'rxjs'; + +@Injectable() +export class LrtApiClient { + constructor(private readonly httpService: HttpService) {} + + async getSearch(topic: string): Promise { + //todo return api response + topic = topic === 'viskas' ? topic : ''; + const url = 'https://www.lrt.lt/api/search?type=3&tema=' + topic; + const resp = await firstValueFrom(this.httpService.get(url)); + const data: SearchResponseInterface = resp.data; + const items: Array = data.items; + + const cats = []; + const returnItems = []; + + if (items) { + for (const i in items) { + const item = items[i]; + if (cats.indexOf(item.category_title) > -1) { + continue; + } + //todo refactor to api response item + const newItem = { + label: item.category_title, + isFolder: true, + thumb: + 'https://www.lrt.lt' + + item.img_path_prefix + + '282x158' + + item.img_path_postfix, + categoryId: await this.findCategoryId(item.category_url), + }; + cats.push(item.category_title); + returnItems.push(newItem); + } + } + + return returnItems; + } + + private async findCategoryId(categoryUrl: string): Promise { + const url = 'https://www.lrt.lt' + categoryUrl; + const resp = await firstValueFrom(this.httpService.get(url)); + const body: string = resp.data; + const start = body.indexOf('data.category_id'); + if (start > -1) { + const interim = body.indexOf('"', start); + const end = body.indexOf('"', interim + 1); + if (end > interim) { + return body.substring(interim + 1, end); + } else { + throw 'unexpected response body 1'; + } + } else { + throw 'unexpected response body 2'; + } + } +} diff --git a/src/KodiApi/LRT/lrt.controller.ts b/src/KodiApi/LRT/lrt.controller.ts index 7bba53d..a307c3b 100644 --- a/src/KodiApi/LRT/lrt.controller.ts +++ b/src/KodiApi/LRT/lrt.controller.ts @@ -1,14 +1,13 @@ import { Controller, Get, Param, Query, Req } from '@nestjs/common'; import { Request } from 'express'; import { LrtService } from './lrt.service'; -import { HttpService } from '@nestjs/axios'; -import { firstValueFrom } from 'rxjs'; +import { LrtApiClient } from './lrt-api.client'; @Controller('api/lrt') export class LrtController { constructor( private readonly lrtService: LrtService, - private readonly httpService: HttpService, + private readonly lrtApiClient: LrtApiClient, ) {} @Get() @@ -27,19 +26,15 @@ export class LrtController { @Get('recent') async getRecent(@Req() request: Request) { - const resp = await firstValueFrom( - this.httpService.get('http://localhost:3000/api'), - ); return { mod: 'lrt', path: request.url, recent: true, - resp: resp.data, }; } @Get('cat/:id') - getCategory(@Req() request: Request, @Param('id') id: number) { + async getCategory(@Req() request: Request, @Param('id') id: number) { return { mod: 'lrt - category', path: request.url, @@ -47,12 +42,13 @@ export class LrtController { }; } - @Get('tema/:tema') - getTema(@Req() request: Request, @Param('tema') tema: string) { + @Get('tema/:topic') + async getTema(@Req() request: Request, @Param('topic') topic: string) { + const resp = await this.lrtApiClient.getSearch(topic); return { mod: 'lrt - category', path: request.url, - tema: tema, + topic: resp, }; } diff --git a/src/KodiApi/LRT/lrt.module.ts b/src/KodiApi/LRT/lrt.module.ts index 6c95f71..86d65e1 100644 --- a/src/KodiApi/LRT/lrt.module.ts +++ b/src/KodiApi/LRT/lrt.module.ts @@ -3,10 +3,15 @@ import { LrtController } from './lrt.controller'; import { LrtService } from './lrt.service'; import { KodiApiResponseFactory } from '../kodi-api-response.factory'; import { HttpModule } from '@nestjs/axios'; +import { LrtApiClient } from './lrt-api.client'; @Module({ - imports: [HttpModule], + imports: [ + HttpModule.register({ + withCredentials: true, + }), + ], controllers: [LrtController], - providers: [KodiApiResponseFactory, LrtService], + providers: [KodiApiResponseFactory, LrtService, LrtApiClient], }) export class LrtModule {}