trending-in-one/utils.ts

198 lines
4.8 KiB
TypeScript
Raw Normal View History

2020-11-29 17:39:59 +08:00
import type { Question, SearchWord, Word, ToutiaoWord } from "./types.ts";
2020-11-29 03:11:35 +08:00
/** 合并两次热门话题并根据 id 去重 */
export function mergeQuestions(
words: Question[],
another: Question[],
): Question[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
obj[w.url] = w.title;
}
return Object.entries(obj).map(([url, title]) => ({
url,
title,
}));
}
/** 合并两次关键词并根据 display_query 去重 */
export function mergeWords(
words: SearchWord[],
another: SearchWord[],
): SearchWord[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
obj[w.display_query] = w.query;
}
return Object.entries(obj).map(([display_query, query]) => ({
query,
display_query,
}));
}
/** 合并两次热门话题并根据 id 去重 */
export function mergeWords4Toutiao(
words: ToutiaoWord[],
another: ToutiaoWord[],
): ToutiaoWord[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
2020-11-29 13:03:19 +08:00
obj[w.url] = w.word;
2020-11-29 03:11:35 +08:00
}
return Object.entries(obj).map(([url, word]) => ({
url,
word,
}));
}
/** 合并两次热门话题并根据 id 去重 */
export function mergeWords4Weibo(
words: Word[],
another: Word[],
): Word[] {
const obj: Record<string, string> = {};
for (const w of words.concat(another)) {
obj[w.url] = w.title;
}
return Object.entries(obj).map(([url, title]) => ({
url,
title,
}));
}
2020-11-29 17:39:59 +08:00
export async function createReadme4Video(words: Question[]): Promise<string> {
2020-11-29 03:11:35 +08:00
const readme = await Deno.readTextFile("./README.md");
2020-11-29 13:03:19 +08:00
return readme.replace(
/<!-- BEGIN ZHIHUVIDEO -->[\W\w]*<!-- END ZHIHUVIDEO -->/,
createVideoList(words),
2020-11-29 17:39:59 +08:00
);
}
export async function createReadme4Question(words: Question[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
2020-11-29 13:03:19 +08:00
/<!-- BEGIN ZHIHUQUESTIONS -->[\W\w]*<!-- END ZHIHUQUESTIONS -->/,
createQuestionList(words),
);
2020-11-29 03:11:35 +08:00
}
2020-11-29 13:03:19 +08:00
export async function createReadme4Search(
words: SearchWord[],
): Promise<string> {
2020-11-29 03:11:35 +08:00
const readme = await Deno.readTextFile("./README.md");
2020-11-29 13:03:19 +08:00
return readme.replace(
/<!-- BEGIN ZHIHUSEARCH -->[\W\w]*<!-- END ZHIHUSEARCH -->/,
createSearchList(words),
);
2020-11-29 03:11:35 +08:00
}
export async function createReadme4Weibo(words: Word[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
2020-11-29 13:03:19 +08:00
return readme.replace(
/<!-- BEGIN WEIBO -->[\W\w]*<!-- END WEIBO -->/,
createWeiboList(words),
);
2020-11-29 03:11:35 +08:00
}
2020-11-29 13:03:19 +08:00
export async function createReadme4Toutiao(
words: ToutiaoWord[],
): Promise<string> {
2020-11-29 03:11:35 +08:00
const readme = await Deno.readTextFile("./README.md");
2020-11-29 13:03:19 +08:00
return readme.replace(
/<!-- BEGIN TOUTIAO -->[\W\w]*<!-- END TOUTIAO -->/,
createTuotiaoList(words),
);
2020-11-29 03:11:35 +08:00
}
export function createVideoList(words: Question[]): string {
return `<!-- BEGIN ZHIHUVIDEO -->
<!-- ${Date()} -->
${
words.map((x) => `1. [${x.title}](${x.url})`)
.join("\n")
}
<!-- END ZHIHUVIDEO -->`;
}
export function createQuestionList(words: Question[]): string {
return `<!-- BEGIN ZHIHUQUESTIONS -->
<!-- ${Date()} -->
${
words.map((x) => `1. [${x.title}](${x.url})`)
.join("\n")
}
<!-- END ZHIHUQUESTIONS -->`;
}
export function createSearchList(words: SearchWord[]): string {
return `<!-- BEGIN ZHIHUSEARCH -->
<!-- ${Date()} -->
${
words.map((x) =>
`1. [${x.display_query}](https://www.zhihu.com/search?q=${x.query})`
).join("\n")
}
<!-- END ZHIHUSEARCH -->`;
}
export function createWeiboList(words: Word[]): string {
return `<!-- BEGIN WEIBO -->
<!-- ${Date()} -->
${
words.map((x) => `1. [${x.title}](https://s.weibo.com/${x.url})`)
.join("\n")
}
<!-- END WEIBO -->`;
}
export function createTuotiaoList(words: ToutiaoWord[]): string {
return `<!-- BEGIN TOUTIAO -->
<!-- ${Date()} -->
${
words.map((x) => `1. [${x.word}](${x.url})`)
.join("\n")
}
<!-- END TOUTIAO -->`;
}
2020-11-29 17:39:59 +08:00
export function createArchive4Question(words: Question[], date: string): string {
return `# ${date}\n
${words.length} \n
${createQuestionList(words)}
`;
}
export function createArchive4Video(words: Question[], date: string): string {
2020-11-29 03:11:35 +08:00
return `# ${date}\n
${words.length} \n
${createVideoList(words)}
`;
}
2020-11-29 13:03:19 +08:00
export function createArchive4Search(
words: SearchWord[],
date: string,
): string {
2020-11-29 03:11:35 +08:00
return `# ${date}\n
${words.length} \n
${createSearchList(words)}
`;
}
export function createArchive4Weibo(words: Word[], date: string): string {
return `# ${date}\n
${words.length} \n
${createWeiboList(words)}
`;
}
2020-11-29 13:03:19 +08:00
export function createArchive4Toutiao(
words: ToutiaoWord[],
date: string,
): string {
2020-11-29 03:11:35 +08:00
return `# ${date}\n
${words.length} \n
${createTuotiaoList(words)}
`;
}