mirror of
https://github.com/sakaljurgis/kodi-api-server.git
synced 2026-07-08 20:37:41 +00:00
start lrt api client
This commit is contained in:
@@ -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>;
|
||||||
|
}
|
||||||
@@ -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';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,14 +1,13 @@
|
|||||||
import { Controller, Get, Param, Query, Req } from '@nestjs/common';
|
import { Controller, Get, Param, Query, Req } from '@nestjs/common';
|
||||||
import { Request } from 'express';
|
import { Request } from 'express';
|
||||||
import { LrtService } from './lrt.service';
|
import { LrtService } from './lrt.service';
|
||||||
import { HttpService } from '@nestjs/axios';
|
import { LrtApiClient } from './lrt-api.client';
|
||||||
import { firstValueFrom } from 'rxjs';
|
|
||||||
|
|
||||||
@Controller('api/lrt')
|
@Controller('api/lrt')
|
||||||
export class LrtController {
|
export class LrtController {
|
||||||
constructor(
|
constructor(
|
||||||
private readonly lrtService: LrtService,
|
private readonly lrtService: LrtService,
|
||||||
private readonly httpService: HttpService,
|
private readonly lrtApiClient: LrtApiClient,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
@Get()
|
@Get()
|
||||||
@@ -27,19 +26,15 @@ export class LrtController {
|
|||||||
|
|
||||||
@Get('recent')
|
@Get('recent')
|
||||||
async getRecent(@Req() request: Request) {
|
async getRecent(@Req() request: Request) {
|
||||||
const resp = await firstValueFrom(
|
|
||||||
this.httpService.get('http://localhost:3000/api'),
|
|
||||||
);
|
|
||||||
return {
|
return {
|
||||||
mod: 'lrt',
|
mod: 'lrt',
|
||||||
path: request.url,
|
path: request.url,
|
||||||
recent: true,
|
recent: true,
|
||||||
resp: resp.data,
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('cat/:id')
|
@Get('cat/:id')
|
||||||
getCategory(@Req() request: Request, @Param('id') id: number) {
|
async getCategory(@Req() request: Request, @Param('id') id: number) {
|
||||||
return {
|
return {
|
||||||
mod: 'lrt - category',
|
mod: 'lrt - category',
|
||||||
path: request.url,
|
path: request.url,
|
||||||
@@ -47,12 +42,13 @@ export class LrtController {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@Get('tema/:tema')
|
@Get('tema/:topic')
|
||||||
getTema(@Req() request: Request, @Param('tema') tema: string) {
|
async getTema(@Req() request: Request, @Param('topic') topic: string) {
|
||||||
|
const resp = await this.lrtApiClient.getSearch(topic);
|
||||||
return {
|
return {
|
||||||
mod: 'lrt - category',
|
mod: 'lrt - category',
|
||||||
path: request.url,
|
path: request.url,
|
||||||
tema: tema,
|
topic: resp,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,10 +3,15 @@ import { LrtController } from './lrt.controller';
|
|||||||
import { LrtService } from './lrt.service';
|
import { LrtService } from './lrt.service';
|
||||||
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
|
import { KodiApiResponseFactory } from '../kodi-api-response.factory';
|
||||||
import { HttpModule } from '@nestjs/axios';
|
import { HttpModule } from '@nestjs/axios';
|
||||||
|
import { LrtApiClient } from './lrt-api.client';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
imports: [HttpModule],
|
imports: [
|
||||||
|
HttpModule.register({
|
||||||
|
withCredentials: true,
|
||||||
|
}),
|
||||||
|
],
|
||||||
controllers: [LrtController],
|
controllers: [LrtController],
|
||||||
providers: [KodiApiResponseFactory, LrtService],
|
providers: [KodiApiResponseFactory, LrtService, LrtApiClient],
|
||||||
})
|
})
|
||||||
export class LrtModule {}
|
export class LrtModule {}
|
||||||
|
|||||||
Reference in New Issue
Block a user