-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(route): yenpress * fix: duplicate category
- Loading branch information
Showing
3 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import type { Namespace } from '@/types'; | ||
|
||
export const namespace: Namespace = { | ||
name: 'Yen Press', | ||
url: 'yenpress.com', | ||
categories: ['reading'], | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
import { DataItem, Route } from '@/types'; | ||
import type { Context } from 'hono'; | ||
|
||
import ofetch from '@/utils/ofetch'; | ||
import cache from '@/utils/cache'; | ||
import * as cheerio from 'cheerio'; | ||
import timezone from '@/utils/timezone'; | ||
import { parseDate } from '@/utils/parse-date'; | ||
import { art } from '@/utils/render'; | ||
import path from 'node:path'; | ||
import { getCurrentPath } from '@/utils/helpers'; | ||
|
||
const __dirname = getCurrentPath(import.meta.url); | ||
|
||
const render = (data) => art(path.join(__dirname, 'templates', 'series.art'), data); | ||
|
||
export const route: Route = { | ||
path: '/series/:name', | ||
example: '/yenpress/series/alya-sometimes-hides-her-feelings-in-russian', | ||
parameters: { name: 'Series name' }, | ||
name: 'Series', | ||
maintainers: ['TonyRL'], | ||
handler, | ||
radar: [ | ||
{ | ||
source: ['yenpress.com/series/:name'], | ||
target: '/series/:name', | ||
}, | ||
], | ||
}; | ||
|
||
async function handler(ctx: Context) { | ||
const { name: series } = ctx.req.param(); | ||
const baseUrl = 'https://yenpress.com'; | ||
const link = `https://yenpress.com/series/${series}`; | ||
|
||
const response = await ofetch(link); | ||
const $ = cheerio.load(response); | ||
|
||
const list = $('.show-more-container .inline_block') | ||
.toArray() | ||
.map((item) => { | ||
const $item = $(item); | ||
return { | ||
title: $item.find('span').text().trim(), | ||
link: new URL($item.find('a').attr('href')!, baseUrl).href, | ||
}; | ||
}) as DataItem[]; | ||
|
||
const items = await Promise.all( | ||
list.map((item) => | ||
cache.tryGet(item.link!, async () => { | ||
const response = await ofetch(item.link!); | ||
const $ = cheerio.load(response); | ||
|
||
item.category = $('.detail-labels.mobile-only') | ||
.eq(0) | ||
.find('a') | ||
.toArray() | ||
.map((a) => $(a).text().trim()); | ||
|
||
$('svg, .social-share, .desktop-only, .detail-labels').remove(); | ||
|
||
const cover = $('.book-info').find('.book-cover-img').html(); | ||
|
||
const bookInfo = $('.buy-info .deliver') | ||
.toArray() | ||
.map((item, i) => ({ | ||
deliver: $(item).text().trim(), | ||
price: $('.book-price').eq(i).text().trim(), | ||
from: $('.services') | ||
.eq(i) | ||
.find('.service') | ||
.toArray() | ||
.map((service) => { | ||
const a = $(service).find('a'); | ||
return { | ||
name: a.text().trim(), | ||
link: a.attr('href'), | ||
}; | ||
}), | ||
detail: $('.detail-info') | ||
.eq(i) | ||
.find('div span') | ||
.toArray() | ||
.map((span) => { | ||
const $span = $(span); | ||
return { | ||
key: $span.text().trim(), | ||
value: $span.next().text().trim(), | ||
}; | ||
}), | ||
})); | ||
|
||
const info = $('.book-info'); | ||
info.find('.buy-info, .series-cover').remove(); | ||
|
||
item.description = render({ | ||
cover: cover! + info.html(), | ||
bookInfo, | ||
}); | ||
item.pubDate = timezone(parseDate(bookInfo[0].detail.find((d) => d.key === 'Release Date')!.value), 0); | ||
|
||
return item; | ||
}) | ||
) | ||
); | ||
|
||
return { | ||
title: $('head title').text().trim(), | ||
description: $('.social-share p').text().trim(), | ||
link, | ||
item: items, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{{ if cover }} | ||
{{@ cover }}<br> | ||
{{ /if }} | ||
|
||
{{ if bookInfo }} | ||
{{ each bookInfo info }} | ||
<p>{{ info.deliver }}: {{ info.price }}</p> | ||
Buy from: | ||
<ul> | ||
{{ each info.from f }} | ||
<li><a href="{{ f.link }}">{{ f.name }}</a></li> | ||
{{ /each }} | ||
</ul> | ||
<b>FULL DETAILS</b> | ||
<table> | ||
{{ each info.detail d }} | ||
<tr> | ||
<td>{{ d.key }}</td> | ||
<td>{{ d.value }}</td> | ||
</tr> | ||
{{ /each }} | ||
</table> | ||
<br> | ||
{{ /each }} | ||
{{ /if }} |