Files
RSSHub/lib/routes/techflowpost/utils.ts
T
dependabot[bot]andTony 417ab04f06 chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0 (#22310)
* chore(deps-dev): bump eslint-plugin-unicorn from 66.0.0 to 68.0.0

Bumps [eslint-plugin-unicorn](https://github.com/sindresorhus/eslint-plugin-unicorn) from 66.0.0 to 68.0.0.
- [Release notes](https://github.com/sindresorhus/eslint-plugin-unicorn/releases)
- [Commits](https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v66.0.0...v68.0.0)

---
updated-dependencies:
- dependency-name: eslint-plugin-unicorn
  dependency-version: 68.0.0
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>

* style: fix no-useless-coercion

* style: fix prefer-boolean-return

* style: fix no-useless-logical-operand

* style: fix prefer-single-replace

* style: fix consistent-conditional-object-spread

* style: fix no-useless-else

* style: fix prefer-unicode-code-point-escapes

* style: fix operator-assignment

* style: fix prefer-continue

* style: fix prefer-math-constants

* style: fix prefer-hoisting-branch-code

* style: fix prefer-array-slice

* style: fix consistent-class-member-order

* style: fix no-return-array-push

* style: fix prefer-promise-with-resolvers

* style: fix no-array-concat-in-loop

* style: fix no-loop-iterable-mutation

* style: fix no-invalid-argument-count

* style: fix no-duplicate-if-branches

* style: fix prefer-else-if

* style: fix no-unreadable-for-of-expression

* style: fix logical-assignment-operators

* style: fix default-export-style

* style: revert false positve in prefer-type-literal-last in #22274

* style: fix arrow-body-style

* style: add v68 recommended preset

* style: refactor GraphQL queries to use template literals for better readability

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Tony <TonyRL@users.noreply.github.com>
2026-06-20 23:11:27 +08:00

193 lines
5.5 KiB
TypeScript

import { config } from '@/config';
import type { DataItem } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';
import { getAcwScV2ByArg1 } from '../5eplay/utils';
const rootUrl = 'https://www.techflowpost.com';
const apiRootUrl = `${rootUrl}/api`;
const uploadRootUrl = 'https://upload.techflowpost.com';
const locale = 'zh-CN';
let acwScV2Cookie = '';
type ApiListResponse<T> = {
data?: T[];
};
type Label = {
label?: string;
};
type Article = {
id: number;
title: string;
abstract?: string;
picture?: string;
author?: {
name?: string;
};
category?: {
name?: string;
};
labels?: Label[];
created_at?: string;
updated_at?: string;
};
type ArticleDetailResponse = {
article?: {
content?: string;
};
};
type Newsflash = {
id: number;
title: string;
abstract?: string;
url?: string;
created_at?: string;
updated_at?: string;
};
type NewsflashDetailResponse = {
content?: string;
};
function getHeaders(referer: string, cookie = acwScV2Cookie) {
return {
accept: 'application/json, text/plain, */*',
'accept-language': locale,
referer,
'user-agent': config.trueUA,
...(cookie && { cookie }),
};
}
function getAcwScV2Cookie(response: string) {
const arg1 = response.match(/var arg1='(.*?)';/)?.[1];
if (!arg1) {
return '';
}
return `acw_sc__v2=${getAcwScV2ByArg1(arg1)}`;
}
async function requestApi<T>(endpoint: string, referer: string, searchParams?: Record<string, string | number | boolean>) {
const request = () =>
got.get(`${apiRootUrl}${endpoint}`, {
searchParams,
headers: getHeaders(referer),
});
const { data } = await request();
if (typeof data !== 'string') {
return data as T;
}
const cookie = getAcwScV2Cookie(data);
if (!cookie) {
throw new Error('TechFlow API returned an unexpected non-JSON response.');
}
acwScV2Cookie = cookie;
const retryResponse = await request();
if (typeof retryResponse.data === 'string') {
throw new TypeError('TechFlow API still returned an anti-crawler challenge after retry.');
}
return retryResponse.data as T;
}
function getPictureUrl(picture?: string) {
if (!picture) {
return;
}
return new URL(picture, uploadRootUrl).href;
}
function getCategories(article: Article) {
return [...new Set([article.category?.name, ...(article.labels?.map((label) => label.label) ?? [])].filter(Boolean))] as string[];
}
function getArticleItem(article: Article, content?: string): DataItem {
const link = `${rootUrl}/${locale}/article/${article.id}`;
const categories = getCategories(article);
const image = getPictureUrl(article.picture);
return {
title: article.title,
author: article.author?.name,
link,
category: categories.length ? categories : undefined,
pubDate: article.created_at ? parseDate(article.created_at) : undefined,
updated: article.updated_at ? parseDate(article.updated_at) : undefined,
description: content || article.abstract,
image,
};
}
function getNewsflashItem(newsflash: Newsflash, content?: string): DataItem {
return {
title: newsflash.title,
link: `${rootUrl}/${locale}/newsletter/${newsflash.id}`,
pubDate: newsflash.created_at ? parseDate(newsflash.created_at) : undefined,
updated: newsflash.updated_at ? parseDate(newsflash.updated_at) : undefined,
description: content || newsflash.abstract,
};
}
async function getArticleItems({ category, limit }: { category?: string; limit: string | number }) {
const link = `${rootUrl}/${locale}/article`;
const searchParams: Record<string, string | number> = {
page: 1,
page_size: limit,
};
if (category) {
searchParams.category_id = category;
}
const response = await requestApi<ApiListResponse<Article>>('/client/articles', link, searchParams);
if (!Array.isArray(response.data)) {
throw new TypeError('TechFlow API returned an invalid article list.');
}
return await Promise.all(
response.data.map((article) =>
cache.tryGet(`techflowpost:article:${article.id}`, async () => {
const itemLink = `${rootUrl}/${locale}/article/${article.id}`;
const detail = await requestApi<ArticleDetailResponse>(`/client/articles/${article.id}`, itemLink);
return getArticleItem(article, detail.article?.content);
})
)
);
}
async function getNewsflashItems(limit: string | number) {
const link = `${rootUrl}/${locale}/newsletter`;
const response = await requestApi<ApiListResponse<Newsflash>>('/client/newsflashes', link, {
page: 1,
page_size: limit,
});
if (!Array.isArray(response.data)) {
throw new TypeError('TechFlow API returned an invalid newsflash list.');
}
return await Promise.all(
response.data.map((newsflash) =>
cache.tryGet(`techflowpost:newsflash:${newsflash.id}`, async () => {
const itemLink = `${rootUrl}/${locale}/newsletter/${newsflash.id}`;
const detail = await requestApi<NewsflashDetailResponse>(`/client/newsflashes/${newsflash.id}`, itemLink);
return getNewsflashItem(newsflash, detail.content);
})
)
);
}
export { getArticleItems, getNewsflashItems, rootUrl };