Skip to content

Commit a7cf7ba

Browse files
committed
feat: add animate provider
1 parent c7a05e7 commit a7cf7ba

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed
+44-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import type { Detail, SearchResult, SearchOptions } from '../types';
1+
import type { Detail, SearchResult, SearchOptions, DetailItem } from '../types';
22

3+
import { JSDOM } from 'jsdom';
34
import { Provider } from '../scraper';
45

56
export class Animate extends Provider {
@@ -8,10 +9,50 @@ export class Animate extends Provider {
89
}
910

1011
async search(text: string, options?: SearchOptions): Promise<SearchResult[]> {
11-
return [];
12+
const html: string = await $fetch('https://www.melonbooks.co.jp/search/search.php', {
13+
query: {
14+
name: text,
15+
'additional[]': 'pr'
16+
}
17+
});
18+
19+
const dom = new JSDOM(html);
20+
const doc = dom.window.document;
21+
22+
const resultItems = doc.querySelectorAll('.item-list li[class^=product]');
23+
return [...resultItems].reduce((res: SearchResult[], item) => {
24+
const a = item.querySelector('.item-image > a') as HTMLAnchorElement;
25+
res.push({
26+
provider: this.id,
27+
title: a.title,
28+
url: 'https://www.melonbooks.co.jp' + a.href
29+
});
30+
return res;
31+
}, []);
1232
}
1333

1434
async detail(url: string): Promise<Detail | undefined> {
15-
return undefined;
35+
const html: string = await $fetch(url);
36+
const dom = new JSDOM(html);
37+
const doc = dom.window.document;
38+
39+
const title = doc.querySelector('.page-header')?.textContent || '';
40+
const privItems = doc.querySelectorAll('.priv-item');
41+
const items = [...privItems].reduce((res: DetailItem[], item) => {
42+
const img = item.querySelector('.priv_img') as HTMLImageElement;
43+
const info = item.querySelector('.priv-item_info');
44+
res.push({
45+
image: img.src,
46+
description: info?.textContent?.trim?.() || ''
47+
});
48+
return res;
49+
}, []);
50+
51+
return {
52+
provider: this.id,
53+
title,
54+
url,
55+
items
56+
};
1657
}
1758
}

pages/index.vue

+7-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ const search = async () => {
2323
</div>
2424
<ul class="grid gap-4 mt-6">
2525
<li v-for="{ title, url, items } in results" class="p-4 b-1 rounded-4">
26-
<nuxt-link class="font-bold hover:color-blue" :to="url" target="_blank">{{ title }}</nuxt-link>
26+
<nuxt-link class="font-bold text-5 hover:color-blue" :to="url" target="_blank">{{ title }}</nuxt-link>
2727
<ul>
28-
<li v-for="{ image, description } in items" class="flex gap-2 mt-2">
28+
<li v-for="{ image, description } in items" class="flex gap-4 mt-4">
2929
<img class="max-w-36" :src="image"/>
30-
<p>{{ description }}</p>
30+
<p class="text-3.5 result-description">{{ description }}</p>
3131
</li>
3232
</ul>
3333
</li>
@@ -39,4 +39,8 @@ const search = async () => {
3939
.main {
4040
@apply: mx-auto w-5xl lt-lg:w-3xl lt-md:w-[95vw] lt-md:px-3;
4141
}
42+
43+
.result-description {
44+
white-space: pre-line;
45+
}
4246
</style>

0 commit comments

Comments
 (0)