Skip to content

Commit

Permalink
✨ feat: 新增:IT之家「喜加一」
Browse files Browse the repository at this point in the history
  • Loading branch information
imsyy committed Apr 15, 2024
1 parent abadb69 commit 067190b
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/routes/ithome-xijiayi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import type { RouterData } from "../types.js";
import { load } from "cheerio";
import { get } from "../utils/getData.js";

export const handleRoute = async (_: undefined, noCache: boolean) => {
const { fromCache, data, updateTime } = await getList(noCache);
const routeData: RouterData = {
name: "ithome",
title: "IT之家「喜加一」",
type: "最新动态",
description: "最新最全的「喜加一」游戏动态尽在这里!",
link: "https://www.ithome.com/zt/xijiayi",
total: data?.length || 0,
updateTime,
fromCache,
data,
};
return routeData;
};

// 链接处理
const replaceLink = (url: string, getId: boolean = false) => {
const match = url.match(/https:\/\/www\.ithome\.com\/0\/(\d+)\/(\d+)\.htm/);
if (match && match[1] && match[2]) {
return getId ? match[1] + match[2] : `https://m.ithome.com/html/${match[1]}${match[2]}.htm`;
}
return url;
};

const getList = async (noCache: boolean) => {
const url = `https://www.ithome.com/zt/xijiayi`;
const result = await get({ url, noCache });
const $ = load(result.data);
const listDom = $(".newslist li");
const listData = listDom.toArray().map((item) => {
const dom = $(item);
const href = dom.find("a").attr("href");
return {
id: href ? Number(replaceLink(href, true)) : 100000,
title: dom.find(".newsbody h2").text().trim(),
desc: dom.find(".newsbody p").text().trim(),
cover: dom.find("img").attr("data-original"),
hot: Number(dom.find(".comment").text().replace(/\D/g, "")),
url: href || undefined,
mobileUrl: href ? replaceLink(href) : undefined,
};
});
return {
fromCache: result.fromCache,
updateTime: result.updateTime,
data: listData,
};
};

0 comments on commit 067190b

Please sign in to comment.