mirror of
https://github.com/vega-org/vega-providers.git
synced 2026-04-17 23:51:44 +00:00
add provider 4khdhub
This commit is contained in:
81
providers/4khdhub/posts.ts
Normal file
81
providers/4khdhub/posts.ts
Normal file
@@ -0,0 +1,81 @@
|
||||
import { Post, ProviderContext } from "../types";
|
||||
|
||||
export const getPosts = async function ({
|
||||
filter,
|
||||
page,
|
||||
signal,
|
||||
providerContext,
|
||||
}: {
|
||||
filter: string;
|
||||
page: number;
|
||||
providerValue: string;
|
||||
signal: AbortSignal;
|
||||
providerContext: ProviderContext;
|
||||
}): Promise<Post[]> {
|
||||
const { getBaseUrl, cheerio } = providerContext;
|
||||
const baseUrl = await getBaseUrl("4khdhub");
|
||||
const url = `${baseUrl + filter}/page/${page}.html`;
|
||||
console.log("4khdhubGetPosts url", url);
|
||||
return posts({ url, signal, cheerio });
|
||||
};
|
||||
|
||||
export const getSearchPosts = async function ({
|
||||
searchQuery,
|
||||
page,
|
||||
signal,
|
||||
providerContext,
|
||||
}: {
|
||||
searchQuery: string;
|
||||
page: number;
|
||||
providerValue: string;
|
||||
signal: AbortSignal;
|
||||
providerContext: ProviderContext;
|
||||
}): Promise<Post[]> {
|
||||
const { getBaseUrl, cheerio } = providerContext;
|
||||
const baseUrl = await getBaseUrl("4khdhub");
|
||||
const url = `${baseUrl}/page/${page}.html?s=hin${searchQuery}`;
|
||||
return posts({ url, signal, cheerio });
|
||||
};
|
||||
|
||||
async function posts({
|
||||
url,
|
||||
signal,
|
||||
cheerio,
|
||||
}: {
|
||||
url: string;
|
||||
signal: AbortSignal;
|
||||
cheerio: ProviderContext["cheerio"];
|
||||
}): Promise<Post[]> {
|
||||
try {
|
||||
const res = await fetch(url, { signal });
|
||||
const data = await res.text();
|
||||
const $ = cheerio.load(data);
|
||||
const catalog: Post[] = [];
|
||||
$(".card-grid")
|
||||
.children()
|
||||
.map((i, element) => {
|
||||
const title = $(element).find(".movie-card-title").text();
|
||||
const link = $(element).attr("href");
|
||||
const image = $(element).find("img").attr("src");
|
||||
// console.log(
|
||||
// "4khdhubGetPosts title",
|
||||
// title,
|
||||
// "link",
|
||||
// link,
|
||||
// "image",
|
||||
// image
|
||||
// );
|
||||
if (title && link && image) {
|
||||
catalog.push({
|
||||
title: title,
|
||||
link: link,
|
||||
image: image,
|
||||
});
|
||||
}
|
||||
});
|
||||
return catalog;
|
||||
} catch (err) {
|
||||
console.error("4khdhubGetPosts error ", err);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user