start lrt api client

This commit is contained in:
Jurgis Sakalauskas
2022-12-07 21:21:01 +02:00
parent 059904d745
commit 9d00d549b6
5 changed files with 118 additions and 13 deletions
@@ -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;
}
@@ -0,0 +1,10 @@
interface SearchResponseInterface {
q: string;
meta: {
time: number;
total: number;
total_found: number;
};
total_found: string | number;
items: Array<SearchResponseItemInterface>;
}
+62
View File
@@ -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<any> {
//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<any> = 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<string> {
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';
}
}
}
+7 -11
View File
@@ -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,
};
}
+7 -2
View File
@@ -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 {}