种子提取器
读取 RSS/ATOM/JSON 的输入数据.
安装
德诺
deno add jsr:@extractus/feed-extractor
节点.js / Bun
pnpm add jsr:@extractus/feed-extractor
# or
npx jsr add @extractus/feed-extractor
# or
bunx jsr add @extractus/feed-extractor
或者从 npm 安装 :
npm install @extractus/feed-extractor
# or
bun add @extractus/feed-extractor
使用量
import { extract } from "@extractus/feed-extractor";
const data = await extract("https://news.google.com/atom");
console.log(data);
APIs 密码
extract()
从给定的RSS/ATOM/JSON源装入并取出饲料数据.
语法
extract(url: string): Promise<FeedData>
extract(url: string, options?: ParserOptions): Promise<FeedData>
extract(url: string, options?: ParserOptions, fetcher?: Fetcher): Promise<FeedData>
示例
import { extract } from "@extractus/feed-extractor";
const result = await extract("https://news.google.com/atom");
console.log(result);
没有任何选择,结果应具有以下结构:
{
title: string;
link: string;
description: string;
generator: string;
language: string;
published: string; // ISO datetime
entries: Array<{
id: string;
title: string;
link: string;
description: string;
published: string; // ISO datetime
}>;
}
参数
url 所需经费
有效种子源的 URL .
饲料内容必须易于取用并符合下列标准之一: 1
options 选项
具有下列全部或数个属性的对象:
normalization:布尔、正常的种子数据或保持原始。 默认true。 。 。 .useISODateFormat: boolean,将日期时间转换为ISO格式. 默认true。 。 。 .descriptionMaxLen:数字,用于截取描述. 默认250字符键。 设定为0=没有截断.xmlParserOptions:对象,选项传递给 XML 解析器.getExtraFeedFields:函数,从种子数据中获取更多字段.getExtraEntryFields:函数,从种子条目数据中获取更多字段.baseUrl: URL 字符串,用于解决 feed 内容中的链接.
例如:
import { extract } from "@extractus/feed-extractor";
await extract("https://news.google.com/atom", {
useISODateFormat: false,
});
await extract("https://news.google.com/rss", {
useISODateFormat: false,
getExtraFeedFields: (feedData) => {
return {
subtitle: feedData.subtitle || "",
};
},
getExtraEntryFields: (feedEntry) => {
const { enclosure, category } = feedEntry;
return {
enclosure: {
url: enclosure["@_url"],
type: enclosure["@_type"],
length: enclosure["@_length"],
},
};
},
});
fetcher 选项
带有签名的自定义获取函数 (url: string) => Promise<Response>。 。 。 .
使用此功能来自定义 HTTP 行为: 代理,信头, TLS, 认证, 超时等.
默认为 globalThis.fetch。 。 。 .
德诺 (带有代理) :
import { extract } from "@extractus/feed-extractor";
const client = Deno.createHttpClient({
proxy: { url: "http://proxy.example.com:8080" },
});
const myFetcher = (url: string) => fetch(url, { client });
const result = await extract("https://news.google.com/rss", {}, myFetcher);
节点.js (通过undici使用代理) :
import { extract } from "@extractus/feed-extractor";
import { fetch, ProxyAgent } from "undici";
const dispatcher = new ProxyAgent("http://proxy.example.com:8080");
const myFetcher = (url: string) => fetch(url, { dispatcher });
const result = await extract("https://news.google.com/rss", {}, myFetcher);
阿宝 (带有代理) :
import { extract } from "@extractus/feed-extractor";
const myFetcher = (url: string) =>
fetch(url, {
proxy: "http://proxy.example.com:8080",
});
const result = await extract("https://news.google.com/rss", {}, myFetcher);
自定义信头数字 :
const myFetcher = (url: string) =>
fetch(url, {
headers: {
"user-agent": "MyBot/1.0",
authorization: "Bearer token123",
},
});
const result = await extract(url, {}, myFetcher);
请求超时数字 :
const myFetcher = (url: string) =>
fetch(url, {
signal: AbortSignal.timeout(5000),
});
const result = await extract(url, {}, myFetcher);
extractFromJson()
从 JSON 对象或字符串中提取种子数据 .
语法
extractFromJson(json: Record<string, unknown> | string): FeedData
extractFromJson(json: Record<string, unknown> | string, options?: ParserOptions): FeedData
示例
import { extractFromJson } from "@extractus/feed-extractor";
const url = "https://www.jsonfeed.org/feed.json";
const res = await fetch(url);
const json = await res.json();
const feed = extractFromJson(json);
console.log(feed);
参数
json 所需经费
JSON 对象或字符串来自 JSON Feed 资源.
options 选项
见 选项 见上文.
extractFromXml()
从 XML 字符串中提取种子数据 .
语法
extractFromXml(xml: string): FeedData
extractFromXml(xml: string, options?: ParserOptions): FeedData
示例
import { extractFromXml } from "@extractus/feed-extractor";
const url = "https://news.google.com/atom";
const res = await fetch(url);
const xml = await res.text();
const feed = extractFromXml(xml);
console.log(feed);
参数
xml 所需经费
来自 RSS/ATOM 种子资源的 XML 字符串 .
options 选项
见 选项 见上文.
发展
git clone https://github.com/extractus/feed-extractor.git
cd feed-extractor
# Run tests
deno test --allow-all
# Lint
deno lint
# Build npm package
deno task build
许可证
《麻省理工学院许可证》
支持项目
这个项目是我空余时间维持的 如果你认为这有帮助, 有一些简单的方式支持它的持续发展:
- 来帮助更多的人发现它.
- 给我买杯咖啡: https://paypal.me/ndaidong (英语)
- 订阅 种子阅读器服务 在快速API上.
每一点支持都有助于保持该项目的积极维持。 谢谢