@@ -2,20 +2,90 @@ import axios from "axios";
22import * as cheerio from "cheerio" ;
33import { DEFAULT_HEADERS } from "../configs/header.config.js" ;
44import { v1_base_url } from "../utils/base_v1.js" ;
5+ import {
6+ FILTER_LANGUAGE_MAP ,
7+ GENRE_MAP ,
8+ FILTER_TYPES ,
9+ FILTER_STATUS ,
10+ FILTER_RATED ,
11+ FILTER_SCORE ,
12+ FILTER_SEASON ,
13+ FILTER_SORT ,
14+ } from "../routes/filter.maping.js" ;
515
6- async function extractSearchResults ( search , page ) {
16+ async function extractSearchResults ( params = { } ) {
717 try {
8- const resp = await axios . get (
9- `https://${ v1_base_url } /search?keyword=${ search } &page=${ page } ` ,
10- {
11- headers : {
12- Accept :
13- "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ,
14- "Accept-Encoding" : "gzip, deflate, br" ,
15- "User-Agent" : DEFAULT_HEADERS ,
16- } ,
18+ const normalizeParam = ( param , mapping ) => {
19+ if ( ! param ) return undefined ;
20+
21+ if ( typeof param === "string" ) {
22+ const isAlreadyId = Object . values ( mapping ) . includes ( param ) ;
23+ if ( isAlreadyId ) {
24+ return param ;
25+ }
26+
27+ const key = param . trim ( ) . toUpperCase ( ) ;
28+ return mapping . hasOwnProperty ( key ) ? mapping [ key ] : undefined ;
1729 }
18- ) ;
30+ return param ;
31+ } ;
32+
33+ const typeParam = normalizeParam ( params . type , FILTER_TYPES ) ;
34+ const statusParam = normalizeParam ( params . status , FILTER_STATUS ) ;
35+ const ratedParam = normalizeParam ( params . rated , FILTER_RATED ) ;
36+ const scoreParam = normalizeParam ( params . score , FILTER_SCORE ) ;
37+ const seasonParam = normalizeParam ( params . season , FILTER_SEASON ) ;
38+ const sortParam = normalizeParam ( params . sort , FILTER_SORT ) ;
39+
40+ let languageParam = params . language ;
41+ if ( typeof languageParam === "string" ) {
42+ languageParam = languageParam . trim ( ) . toUpperCase ( ) ;
43+ languageParam = FILTER_LANGUAGE_MAP [ languageParam ] || undefined ;
44+ }
45+
46+ let genresParam = params . genres ;
47+ if ( typeof genresParam === "string" ) {
48+ genresParam = genresParam
49+ . split ( "," )
50+ . map ( ( genre ) => GENRE_MAP [ genre . trim ( ) . toUpperCase ( ) ] || genre . trim ( ) )
51+ . join ( "," ) ;
52+ }
53+
54+ const filteredParams = {
55+ type : typeParam ,
56+ status : statusParam ,
57+ rated : ratedParam ,
58+ score : scoreParam ,
59+ season : seasonParam ,
60+ language : languageParam ,
61+ genres : genresParam ,
62+ sort : sortParam ,
63+ page : params . page || 1 ,
64+ sy : params . sy || undefined ,
65+ sm : params . sm || undefined ,
66+ sd : params . sd || undefined ,
67+ ey : params . ey || undefined ,
68+ em : params . em || undefined ,
69+ ed : params . ed || undefined ,
70+ keyword : params . keyword || undefined ,
71+ } ;
72+
73+ Object . keys ( filteredParams ) . forEach ( ( key ) => {
74+ if ( filteredParams [ key ] === undefined ) {
75+ delete filteredParams [ key ] ;
76+ }
77+ } ) ;
78+
79+ const queryParams = new URLSearchParams ( filteredParams ) . toString ( ) ;
80+
81+ const resp = await axios . get ( `https://${ v1_base_url } /search?${ queryParams } ` , {
82+ headers : {
83+ Accept :
84+ "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9" ,
85+ "Accept-Encoding" : "gzip, deflate, br" ,
86+ "User-Agent" : DEFAULT_HEADERS ,
87+ } ,
88+ } ) ;
1989
2090 const $ = cheerio . load ( resp . data ) ;
2191 const elements = "#main-content .film_list-wrap .flw-item" ;
@@ -59,10 +129,11 @@ async function extractSearchResults(search, page) {
59129 . find ( ".film-poster .film-poster-img" )
60130 ?. attr ( "data-src" )
61131 ?. trim ( ) || null ,
62- duration : $ ( el )
63- . find ( ".film-detail .fd-infor .fdi-item.fdi-duration" )
64- ?. text ( )
65- ?. trim ( ) ,
132+ duration :
133+ $ ( el )
134+ . find ( ".film-detail .fd-infor .fdi-item.fdi-duration" )
135+ ?. text ( )
136+ ?. trim ( ) ,
66137 tvInfo : {
67138 showType :
68139 $ ( el )
0 commit comments