Skip to content

Commit

Permalink
feat: add baseUrl
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Jan 24, 2024
1 parent 09ed96d commit bc43e55
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 22 deletions.
24 changes: 11 additions & 13 deletions packages/get-bonus/src/melonbooks/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { Provider } from '../scraper';

export class Melonbooks extends Provider {
constructor() {
super('melonbooks');
super('melonbooks', 'https://www.melonbooks.co.jp');
}

async search(text: string, options: SearchOptions): Promise<SearchResult[]> {
const html: string = await ofetch('https://www.melonbooks.co.jp/search/search.php', {
const html: string = await ofetch(this.baseUrl + '/search/search.php', {
query: {
name: text,
'additional[]': 'pr'
Expand All @@ -22,15 +22,14 @@ export class Melonbooks extends Provider {
const doc = dom.window.document;

const resultItems = doc.querySelectorAll('.item-list li[class^=product]');
return [...resultItems].reduce((res: SearchResult[], item) => {
return [...resultItems].map((item) => {
const a = item.querySelector('.item-image > a') as HTMLAnchorElement;
res.push({
return {
provider: this.id,
title: a.title,
url: 'https://www.melonbooks.co.jp' + a.href
});
return res;
}, []);
url: this.baseUrl + a.href
};
});
}

async detail(url: string): Promise<Detail | undefined> {
Expand All @@ -40,15 +39,14 @@ export class Melonbooks extends Provider {

const title = doc.querySelector('.page-header')?.textContent || '';
const privItems = doc.querySelectorAll('.priv-item');
const items = [...privItems].reduce((res: DetailItem[], item) => {
const items = [...privItems].map((item) => {
const img = item.querySelector('.priv_img') as HTMLImageElement;
const info = item.querySelector('.priv-item_info');
res.push({
return {
image: enforceHTTPS(img.src),
description: info?.textContent?.trim?.() || ''
});
return res;
}, []);
};
});

return {
provider: this.id,
Expand Down
4 changes: 3 additions & 1 deletion packages/get-bonus/src/scraper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ export class Scraper {

export abstract class Provider {
public readonly id: string;
public readonly baseUrl: string;

constructor(name: string) {
constructor(name: string, baseUrl: string) {
this.id = name;
this.baseUrl = baseUrl;
}

abstract search(text: string, options: Partial<SearchOptions>): Promise<SearchResult[]>;
Expand Down
15 changes: 7 additions & 8 deletions packages/get-bonus/src/toranoana/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import { Provider } from '../scraper';

export class Toranoana extends Provider {
constructor() {
super('toranoana');
super('toranoana', 'https://ecs.toranoana.jp');
}

async search(text: string, options: SearchOptions): Promise<SearchResult[]> {
const html: string = await ofetch('https://ecs.toranoana.jp/tora/ec/app/catalog/list', {
const html: string = await ofetch(this.baseUrl + '/tora/ec/app/catalog/list', {
query: {
searchWord: text,
stock_status: '○,△',
Expand All @@ -23,15 +23,14 @@ export class Toranoana extends Provider {
const doc = dom.window.document;

const resultItems = doc.querySelectorAll('.product-list-item');
return [...resultItems].reduce((res: SearchResult[], item) => {
return [...resultItems].map((item) => {
const a = item.querySelector('.product-list-title > a') as HTMLAnchorElement;
res.push({
return {
provider: this.id,
title: a.textContent?.trim?.() || '',
url: 'https://ecs.toranoana.jp' + a.href
});
return res;
}, []);
url: this.baseUrl + a.href
};
});
}

async detail(url: string): Promise<Detail | undefined> {
Expand Down

0 comments on commit bc43e55

Please sign in to comment.