Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
162 changes: 162 additions & 0 deletions lib/routes/jable/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
import { load } from 'cheerio';

import type { Route } from '@/types';
import cache from '@/utils/cache';
import got from '@/utils/got';
import { parseDate } from '@/utils/parse-date';

export const route: Route = {
path: '/search/:query',
categories: ['multimedia'],
example: '/jable/search/みなみ羽琉',
parameters: {
query: 'Search keyword',
},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
nsfw: true,
},
radar: [
{
source: ['jable.tv/search/:query'],
target: '/:query',
},
],
name: 'Jable 搜索结果',
maintainers: [],
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing maintainer GitHub id.

handler,
};

const GOT_HEADERS = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36',
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the site only work with this specific version of Chrome on Windows instead of RSSHub's default UA which is a randomised version of Chrome on mac?

'Accept-Language': 'en-US,en;q=0.9',
Referer: 'https://jable.tv/',
};

// function renderDescription(item) {
// return `
// <a href="${item.link}">
// <img src="${item.thumb}" style="max-width:100%" />
// </a>
// <p>
// ${item.duration ? `<strong>Duration:</strong> ${item.duration}` : ''}
// ${item.views ? `|<strong>Views:</strong> ${item.views}` : ''}
// ${item.favorites ? `|<strong>Favorites:</strong> ${item.favorites}` : ''}
// ${item.author ? `|<strong>Author:</strong> ${item.author}` : ''}
// </p>
// `.trim();
// }

async function handler(ctx) {
const { query } = ctx.req.param();
const params = new URLSearchParams({
mode: 'async',
function: 'get_block',
block_id: 'list_videos_videos_list_search_result',
q: query,
sort_by: 'post_date',
});
const encodedQuery = encodeURIComponent(query);
const apiUrl = `https://jable.tv/search/${encodedQuery}/?${params.toString()}`;
const response = await got(apiUrl, { headers: GOT_HEADERS });
const $ = load(response.data);

const author = $('section.content-header h2').first().text().trim() || query;

const items = await Promise.all(
$('.video-img-box')
.toArray()
.map((el) => {
const $el = $(el);

const $titleLink = $el.find('.detail h6.title a');
const title = $titleLink.text().trim();
const link = $titleLink.attr('href') ?? '';

const thumb = $el.find('img[data-src]').attr('data-src') ?? '';
const preview = $el.find('img[data-preview]').attr('data-preview') ?? '';
Comment on lines +81 to +82
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are referring to the same element. Do not use find to look for the same element more than once.


// const duration = $el.find('.label').text().trim();
// const subText = $el.find('.sub-title').text().trim();
// const nums = subText.split(/\s+/);
// const views = nums[0] ?? '';
// const favorites = nums[1] ?? '';

const videoId = $el.find('[data-fav-video-id]').attr('data-fav-video-id') ?? link;

return cache.tryGet(`jable:video:${videoId}`, async () => {
let pubDate;
let videoUrl;

try {
const { data } = await got(link, { headers: GOT_HEADERS });
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not visit link for pubDate and a potential file. None of the entries can obtain the videoUrl as you can see from #21641 (comment). This is simply not worth it.

const $page = load(data);

const dateText = $page('.video-date').text().trim();
if (dateText) {
pubDate = parseDate(dateText);
}

const script = $page('script')
.toArray()
.map((s) => $(s).html())
.find((s) => s && s.includes('sources'));

if (script) {
const match = script.match(/file:\s*"([^"]+)"/);
if (match) {
videoUrl = match[1];
}
}
} catch {
// 忽略错误
}

return {
title,
link,
guid: `jable:video:${videoId}`,
pubDate,
author,
// description: renderDescription({
// title,
// link,
// thumb,
// duration,
// views,
// favorites,
// author,
// }),
media: {
content: preview
? {
url: preview,
type: 'video/mp4',
}
: videoUrl
? {
url: videoUrl,
type: 'video/mp4',
}
: undefined,
thumbnail: {
url: thumb,
},
},
};
});
})
);

return {
title: query,
link: `https://jable.tv/search/${encodedQuery}/`,
description: `Search results for ${query}`,
item: items,
};
}
7 changes: 7 additions & 0 deletions lib/routes/jable/namaspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'jable',
url: 'jable.tv',
lang: 'zh',
};
Loading