forked from huqi-pr/trending-in-one
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.ts
202 lines (183 loc) · 4.75 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import type { Question, SearchWord, ToutiaoWord, Word } from "./types.ts";
/** 合并两次热门话题并根据 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)) {
obj[w.url] = w.word;
}
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,
}));
}
export async function createReadme4Video(words: Question[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
/<!-- BEGIN ZHIHUVIDEO -->[\W\w]*<!-- END ZHIHUVIDEO -->/,
createVideoList(words),
);
}
export async function createReadme4Question(
words: Question[],
): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
/<!-- BEGIN ZHIHUQUESTIONS -->[\W\w]*<!-- END ZHIHUQUESTIONS -->/,
createQuestionList(words),
);
}
export async function createReadme4Search(
words: SearchWord[],
): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
/<!-- BEGIN ZHIHUSEARCH -->[\W\w]*<!-- END ZHIHUSEARCH -->/,
createSearchList(words),
);
}
export async function createReadme4Weibo(words: Word[]): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
/<!-- BEGIN WEIBO -->[\W\w]*<!-- END WEIBO -->/,
createWeiboList(words),
);
}
export async function createReadme4Toutiao(
words: ToutiaoWord[],
): Promise<string> {
const readme = await Deno.readTextFile("./README.md");
return readme.replace(
/<!-- BEGIN TOUTIAO -->[\W\w]*<!-- END TOUTIAO -->/,
createTuotiaoList(words),
);
}
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}](${x.url})`
).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 -->`;
}
export function createArchive4Question(
words: Question[],
date: string,
): string {
return `# ${date}\n
共 ${words.length} 条\n
${createQuestionList(words)}
`;
}
export function createArchive4Video(words: Question[], date: string): string {
return `# ${date}\n
共 ${words.length} 条\n
${createVideoList(words)}
`;
}
export function createArchive4Search(
words: SearchWord[],
date: string,
): string {
return `# ${date}\n
共 ${words.length} 条\n
${createSearchList(words)}
`;
}
export function createArchive4Weibo(words: Word[], date: string): string {
return `# ${date}\n
共 ${words.length} 条\n
${createWeiboList(words)}
`;
}
export function createArchive4Toutiao(
words: ToutiaoWord[],
date: string,
): string {
return `# ${date}\n
共 ${words.length} 条\n
${createTuotiaoList(words)}
`;
}