Skip to content

Commit

Permalink
Merge pull request #1373 from DIYgod/master
Browse files Browse the repository at this point in the history
[pull] master from diygod:master
  • Loading branch information
pull[bot] authored May 3, 2024
2 parents 2cd3852 + b1ca437 commit e376a51
Show file tree
Hide file tree
Showing 28 changed files with 517 additions and 123 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"unicorn/explicit-length-check": 0,
"unicorn/filename-case": ["error", { "case": "kebabCase", "ignore": [".*\\.(yaml|yml)$", "RequestInProgress\\.js$"] }],
"unicorn/new-for-builtins": 0,
"unicorn/no-array-callback-reference": 0,
"unicorn/no-array-callback-reference": 1,
"unicorn/no-array-reduce": 1,
"unicorn/no-await-expression-member": 0,
"unicorn/no-empty-file": 1,
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/caixin/templates/article.art
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<% const video = $('script').text().match(/initPlayer\('(.*?)','(.*?)'\)/); %>
<% const videoUrl = video[1]; %>
<% const poster = video[2]; %>
<video controls preload="none" poster="{{ poster }}" src="{{ videoUrl }}"></video>
<video controls preload="metadata" poster="{{ poster }}" src="{{ videoUrl }}"></video>
<br>
{{ /if }}

Expand Down
2 changes: 1 addition & 1 deletion lib/routes/douyin/templates/embed.art
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<video controls preload="none" referrerpolicy="no-referrer"
<video controls preload="metadata" referrerpolicy="no-referrer"
style="width:50%"
{{ if img }}
poster="{{ img }}"
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/fansly/templates/media.art
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ if poster && src }}
<video controls preload="none" poster="{{ poster.location }}">
<video controls preload="metadata" poster="{{ poster.location }}">
<source src="{{ src.location }}" type="video/mp4">
</video>
{{ else if src }}
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/ifeng/templates/video.art
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ if videoInfo.mobileUrl }}
<video controls poster="{{ videoInfo.bigPosterUrl }}" preload="none">
<video controls poster="{{ videoInfo.bigPosterUrl }}" preload="metadata">
<source src="{{ videoInfo.mobileUrl }}" type="video/mp4">
</video>
{{ /if }}
2 changes: 1 addition & 1 deletion lib/routes/instagram/templates/video.art
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
<br>
{{ /if }}

<video controls preload="none" poster="{{ image }}" width="{{ video.width }}">
<video controls preload="metadata" poster="{{ image }}" width="{{ video.width }}">
<source src="{{ video.url }}" type="video/mp4">
</video>
13 changes: 13 additions & 0 deletions lib/routes/ithome/templates/description.art
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{{ if images }}
{{ each images image }}
{{ if image?.src }}
<figure>
<img
{{ if image.alt }}
alt="{{ image.alt }}"
{{ /if }}
src="{{ image.src }}">
</figure>
{{ /if }}
{{ /each }}
{{ /if }}
174 changes: 118 additions & 56 deletions lib/routes/ithome/zt.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,150 @@
import { Route } from '@/types';
import { getCurrentPath } from '@/utils/helpers';
const __dirname = getCurrentPath(import.meta.url);

import cache from '@/utils/cache';
import got from '@/utils/got';
import { load } from 'cheerio';
import timezone from '@/utils/timezone';
import { parseDate } from '@/utils/parse-date';
import { art } from '@/utils/render';
import path from 'node:path';

export const route: Route = {
path: '/zt/:id',
categories: ['new-media'],
example: '/ithome/zt/xijiayi',
parameters: { id: '专题 id' },
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['ithome.com/zt/:id'],
},
],
name: '专题',
maintainers: ['nczitzk'],
handler,
description: `所有专题请见[此处](https://www.ithome.com/zt)`,
};

async function handler(ctx) {
const id = ctx.req.param('id');
export const handler = async (ctx) => {
const { id = 'xijiayi' } = ctx.req.param();
const limit = ctx.req.query('limit') ? Number.parseInt(ctx.req.query('limit'), 10) : 50;

const rootUrl = 'https://www.ithome.com';
const currentUrl = `${rootUrl}/zt/${id}`;
const currentUrl = new URL(`zt/${id}`, rootUrl).href;

const { data: response } = await got(currentUrl);

const response = await got({
method: 'get',
url: currentUrl,
});
const $ = load(response);

const $ = load(response.data);
const author = 'IT之家';
const language = 'zh';

const list = $('.newsbody a')
.map((_, item) => {
let items = $('div.newsbody')
.slice(0, limit)
.toArray()
.map((item) => {
item = $(item);

const title = item.find('h2').text();
const image = item.find('img').prop('data-original') ?? item.find('img').prop('src');

return {
title: item.text(),
link: item.attr('href'),
title,
pubDate: timezone(
parseDate(
item
.find('span.time script')
.text()
.match(/'(.*?)'/)
),
+8
),
link: item.find('a').first().prop('href'),
author: item.find('div.editor').contents().first().text(),
image,
banner: image,
language,
};
})
.get();
});

const items = await Promise.all(
list.map((item) =>
items = await Promise.all(
items.map((item) =>
cache.tryGet(item.link, async () => {
const detailResponse = await got({
method: 'get',
url: item.link,
});
const { data: detailResponse } = await got(item.link);

const $$ = load(detailResponse);

$$('p.ad-tips, a.topic-bar').remove();

const content = load(detailResponse.data);
const post = content('.post_content');
$$('div#paragraph p img').each((_, el) => {
el = $$(el);

post.find('img[data-original]').each((_, ele) => {
ele = $(ele);
ele.attr('src', ele.attr('data-original'));
ele.removeAttr('class');
ele.removeAttr('data-original');
const src = el.prop('data-original');

if (src) {
el.replaceWith(
art(path.join(__dirname, 'templates/description.art'), {
images: [
{
src,
alt: el.prop('alt'),
},
],
})
);
}
});

item.description = post.html();
item.author = content('#author_baidu').text().replace('作者:', '');
item.pubDate = timezone(parseDate(content('#pubtime_baidu').text()), +8);
const title = $$('h1').text();
const description = $$('div#paragraph').html();
const image = $$('div#paragraph img').first().prop('src');

item.title = title;
item.description = description;
item.pubDate = timezone(parseDate($$('span#pubtime_baidu').text()), +8);
item.category = $$('div.cv a')
.toArray()
.map((c) => $$(c).text())
.slice(1);
item.author = $$('span#author_baidu').contents().last().text() || $$('span#source_baidu').contents().last().text() || $$('span#editor_baidu').contents().last().text();
item.content = {
html: description,
text: $$('div#paragraph').text(),
};
item.image = image;
item.banner = image;
item.language = language;

return item;
})
)
);

const image = new URL($('meta[property="og:image"]').prop('content'), rootUrl).href;

return {
title: `${$('title').text()} - IT之家`,
title: `${author} - ${$('title').text()}`,
description: $('meta[name="description"]').prop('content'),
link: currentUrl,
item: items,
allowEmpty: true,
image,
author,
language,
};
}
};

export const route: Route = {
path: '/zt/:id?',
name: '专题',
url: 'ithome.com',
maintainers: ['nczitzk'],
handler,
example: '/ithome/zt/xijiayi',
parameters: { category: '专题 id,默认为 xijiayi,即 [喜加一](https://www.ithome.com/zt/xijiayi),可在对应专题页 URL 中找到' },
description: `:::tip
更多专题请见 [IT之家专题](https://www.ithome.com/zt)
:::`,
categories: ['new-media'],

features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportRadar: true,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['ithome.com/zt/:id'],
target: '/zt/:id',
},
],
};
2 changes: 1 addition & 1 deletion lib/routes/kcna/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const fetchVideo = (ctx, url) =>
const js = $('script[type="text/javascript"]:not([src])').html();
let sources = js.match(/<[^>]*source[^>]+src[^>]+>/g);
sources = sources && sources.map((item) => item.replaceAll("'", '"').replaceAll(/src="([^"]+)"/g, `src="${rootUrl}$1"`));
return `<video controls preload="none">${sources.join('\n')}</video>`;
return `<video controls preload="metadata">${sources.join('\n')}</video>`;
});

export { parseJucheDate, fixDesc, fetchPhoto, fetchVideo };
10 changes: 5 additions & 5 deletions lib/routes/linkedin/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,11 @@ function parseJobSearch(data) {
const jobs = $('li')
.map((i, elem) => {
const elemHtml = $(elem);
const link = elemHtml.find('a.base-card__full-link').attr('href').split('?')[0];
const title = elemHtml.find('h3.base-search-card__title').text().trim();
const company = elemHtml.find('h4.base-search-card__subtitle').text().trim();
const location = elemHtml.find('span.job-search-card__location').text().trim();
const pubDate = elemHtml.find('time').attr('datetime');
const link = elemHtml.find('a.base-card__full-link, a.base-card--link')?.attr('href')?.split('?')[0];
const title = elemHtml.find('h3.base-search-card__title')?.text()?.trim();
const company = elemHtml.find('h4.base-search-card__subtitle')?.text()?.trim();
const location = elemHtml.find('span.job-search-card__location')?.text()?.trim();
const pubDate = elemHtml.find('time')?.attr('datetime');

return new Job(title, link, company, location, pubDate);
})
Expand Down
63 changes: 63 additions & 0 deletions lib/routes/magnumphotos/magazine.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { Route } from '@/types';
import cache from '@/utils/cache';
import parser from '@/utils/rss-parser';
import ofetch from '@/utils/ofetch';
import { load } from 'cheerio';
const host = 'https://www.magnumphotos.com';
export const route: Route = {
path: '/magazine',
categories: ['picture'],
example: '/magnumphotos/magazine',
parameters: {},
features: {
requireConfig: false,
requirePuppeteer: false,
antiCrawler: false,
supportBT: false,
supportPodcast: false,
supportScihub: false,
},
radar: [
{
source: ['magnumphotos.com/'],
},
],
name: 'Magazine',
maintainers: ['EthanWng97'],
handler,
url: 'magnumphotos.com/',
};

async function handler() {
const rssUrl = `${host}/feed/`;
const feed = await parser.parseURL(rssUrl);
const items = await Promise.all(
feed.items.map((item) =>
cache.tryGet(item.link, async () => {
if (!item.link) {
return;
}
const data = await ofetch(item.link);
const $ = load(data);
const description = $('#content');
description.find('ul.share').remove();
description.find('h1').remove();

return {
title: item.title,
pubDate: item.pubDate,
link: item.link,
category: item.categories,
description: description.html(),
};
})
)
);

return {
title: 'Magnum Photos',
link: host,
description: 'Magnum is a community of thought, a shared human quality, a curiosity about what is going on in the world, a respect for what is going on and a desire to transcribe it visually',
item: items,
};
}
6 changes: 6 additions & 0 deletions lib/routes/magnumphotos/namespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Namespace } from '@/types';

export const namespace: Namespace = {
name: 'Magnum Photos',
url: 'magnumphotos.com',
};
2 changes: 1 addition & 1 deletion lib/routes/mingpao/templates/fancybox.art
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{{ each media }}
{{ if $value.video }}
<video controls preload="none" poster="{{ $value.video.replace('.mp4', '.jpg') }}"><source src="{{ $value.video }}" type="video/mp4"></video>
<video controls preload="metadata" poster="{{ $value.video.replace('.mp4', '.jpg') }}"><source src="{{ $value.video }}" type="video/mp4"></video>
{{ else }}
<figure><img src="{{ $value.href }}" alt="{{ $value.title }}"><figcaption>{{ $value.title }}</figcaption></figure>
{{ /if }}
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/missav/templates/preview.art
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<video controls preload="none" poster="{{ poster }}">
<video controls preload="metadata" poster="{{ poster }}">
<source src="{{ video }}" type="{{ type }}">
</video>
2 changes: 1 addition & 1 deletion lib/routes/pikabu/templates/video.art
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{{ if videoId }}
<iframe id="ytplayer" type="text/html" width="640" height="360" src="https://www.youtube-nocookie.com/embed/{{ videoId }}" frameborder="0" allowfullscreen></iframe>
{{ else if webm || mp4 }}
<video controls preload="none" poster="{{ preview }}" width="{{ width }}">
<video controls preload="metadata" poster="{{ preview }}" width="{{ width }}">
{{ if webm }}<source src="{{ webm }}" type="video/webm">{{ /if }}
{{ if mp4 }}<source src="{{ mp4 }}" type="video/mp4">{{ /if }}
</video>
Expand Down
2 changes: 1 addition & 1 deletion lib/routes/pornhub/templates/description.art
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{{ if previewVideo }}
<video controls preload="none" poster="{{ poster }}">
<video controls preload="metadata" poster="{{ poster }}">
<source src="{{ previewVideo }}" type="video/webm">
</video>
{{ /if }}
Expand Down
Loading

0 comments on commit e376a51

Please sign in to comment.