-
Notifications
You must be signed in to change notification settings - Fork 9.5k
Expand file tree
/
Copy pathsearch.ts
More file actions
227 lines (197 loc) · 8.08 KB
/
search.ts
File metadata and controls
227 lines (197 loc) · 8.08 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import { load } from 'cheerio';
import type { DataItem, Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import ofetch from '@/utils/ofetch';
import { parseDate } from '@/utils/parse-date';
const baseUrl = 'https://www.archdaily.com';
const archdailyArticlePrefix = 'https://www.archdaily.com/';
const allowedCategories = new Set(['all', 'projects', 'products', 'folders', 'articles', 'competitions', 'events']);
export const route: Route = {
path: '/search/:category/:search',
categories: ['journal'],
example: '/archdaily/search/projects/Urban Design',
parameters: {
category: 'The category to search in, including "all", "projects", "products", "folders", "articles", "competitions" and "events"',
search: 'The search query',
},
features: {
requireConfig: false,
},
radar: [
{
source: ['www.archdaily.com/search/:category'],
target: '/search/:category/:search',
},
],
name: 'Search',
maintainers: ['Friday_Anubis'],
handler,
};
async function handler(ctx) {
const { category, search } = ctx.req.param();
const finalCategory = allowedCategories.has(category) ? category : 'all';
if (finalCategory === 'competitions' || finalCategory === 'events') {
const pageCategory = finalCategory;
const listingUrl = `${baseUrl}/search/${pageCategory}/text/${encodeURIComponent(search)}`;
const { data: response } = await got(listingUrl);
const $ = load(response);
const seen = new Set<string>();
const list = $('li.afd-search-list__item')
.toArray()
.flatMap((item) => {
const element = $(item);
const linkElement = element.find('a.afd-search-list__link').first();
const href = linkElement.attr('href');
const titleElement = element.find('h2.afd-search-list__title').first();
const imageElement = element.find('img.afd-search-list__img').first();
const title = titleElement.text().trim() || imageElement.attr('alt')?.trim();
if (!href || !title) {
return [];
}
const link = normalizeArchdailyLink(href, baseUrl);
if (!link || !link.startsWith(archdailyArticlePrefix) || link.includes('/search/') || seen.has(link)) {
return [];
}
seen.add(link);
const articleId = getArchdailyArticleId(link);
return [
{
title,
link,
image: normalizeImageUrl(imageElement.attr('src')),
guid: articleId ? `archdaily-${pageCategory}-${articleId}` : `${pageCategory}:${link}`,
},
];
});
const items: DataItem[] = await Promise.all(
list.map((item) =>
cache.tryGet(item.guid, async () => {
const detail = await getCompetitionMeta(item.link);
return {
title: item.title,
link: item.link,
guid: item.guid,
description: `${item.image ? `<img src="${item.image}"><br>` : ''}${detail.description ?? ''}`,
pubDate: detail.pubDate,
};
})
)
);
return {
title: `ArchDaily - ${search} in ${pageCategory}`,
link: listingUrl,
description: `Search results for "${search}" in ${pageCategory} on ArchDaily`,
item: items,
};
}
const response = await ofetch<{ results?: any[] }>(`${baseUrl}/search/api/v1/us/${finalCategory}?q=${encodeURIComponent(search)}`);
const items: DataItem[] = (response?.results ?? []).flatMap((item) => {
const title = item?.title || item?.name;
const link = item?.url;
if (!title || !link) {
return [];
}
if (finalCategory === 'folders') {
const images = (item?.images ?? []).map((image) => normalizeImageUrl(image)).filter(Boolean);
const author = item?.user?.slug_name;
const profileUrl = item?.user?.profile_url;
const folderTitle = author ? `${title} by ${author}` : title;
return [
{
title: folderTitle,
link,
guid: item?.id ? `archdaily-folder-${item.id}` : undefined,
description: [
`<p><strong>${folderTitle}</strong></p>`,
item?.last_update ? `<p>Updated: ${item.last_update}</p>` : undefined,
profileUrl ? `<p><a href="${profileUrl}">Uploader Profile</a></p>` : undefined,
images.map((image) => `<img src="${image}">`).join('<br>'),
]
.filter(Boolean)
.join(''),
pubDate: item?.last_update ? parseDate(item.last_update) : undefined,
author,
category: ['folders', title],
},
];
}
const image = normalizeImageUrl(item?.featured_images?.url_large || item?.featured_images?.url_medium || item?.featured_images?.url_small);
const itemCategory = (item?.tags ?? []).map((tag) => tag?.name).filter(Boolean);
return [
{
title,
link,
description: `${image ? `<img src="${image}"><br>` : ''}${item?.meta_description ?? ''}`,
pubDate: item?.publication_date ? parseDate(item.publication_date) : undefined,
author: item?.author?.name,
category: itemCategory,
},
];
});
const seenOutput = new Set<string>();
const dedupedItems = items.filter((item) => {
const key = item.guid || item.link;
if (!key || seenOutput.has(key)) {
return false;
}
seenOutput.add(key);
return true;
});
return {
title: `ArchDaily - ${search}${finalCategory === 'all' ? '' : ` in ${finalCategory}`}`,
link: `${baseUrl}/search/${finalCategory}?q=${encodeURIComponent(search)}`,
description: `Search results for "${search}" on ArchDaily`,
item: dedupedItems,
};
}
function normalizeImageUrl(url?: string) {
if (!url) {
return;
}
if (url.startsWith('//')) {
return `https:${url}`;
}
return url;
}
function normalizeArchdailyLink(url: string, baseUrl: string) {
if (url.startsWith('/')) {
return normalizeUrlWithoutSearchAndHash(`${baseUrl}${url}`);
}
if (url.startsWith(archdailyArticlePrefix)) {
return normalizeUrlWithoutSearchAndHash(url);
}
}
function normalizeUrlWithoutSearchAndHash(input: string) {
const parsed = new URL(input);
parsed.search = '';
parsed.hash = '';
return parsed.toString();
}
function getArchdailyArticleId(link: string) {
const matched = link.match(/archdaily\.com\/(\d+)\//);
return matched ? matched[1] : undefined;
}
async function getCompetitionMeta(link: string) {
const { data } = await got(link);
const $ = load(data);
const publishedTime =
$('meta[property="article:published_time"]').attr('content') ??
$('script[type="application/ld+json"]')
.toArray()
.map((script) => $(script).text())
.map((jsonText) => {
try {
return JSON.parse(jsonText);
} catch {
return;
}
})
.flatMap((entry) => (Array.isArray(entry) ? entry : [entry]))
.find((entry) => entry && typeof entry === 'object' && entry.datePublished)?.datePublished;
const description = $('meta[property="og:description"]').attr('content') || $('meta[name="description"]').attr('content');
return {
pubDate: publishedTime ? parseDate(publishedTime) : undefined,
description,
};
}