Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

enhance(plugin): blueskyのURLプレビューに対応 #35

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(unreleased)
------------------
* センシティブフラグの判定を `<meta property="rating">` および `rating` ヘッダでも行うように
* Bluesky(bsky.app)のプレビューに対応
* 依存関係の更新
* eslintの設定を更新

Expand Down
32 changes: 19 additions & 13 deletions src/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,19 +138,7 @@ export type GeneralScrapingOptions = {
contentLengthRequired?: boolean;
}

function headerEqualValueContains(search: string, headerValue: string | string[] | undefined) {
if (!headerValue) {
return false;
}

if (Array.isArray(headerValue)) {
return headerValue.some(value => value.toLowerCase() === search.toLowerCase());
}

return headerValue.toLowerCase() === search.toLowerCase();
}

export async function parseGeneral(_url: URL | string, opts?: GeneralScrapingOptions): Promise<Summary | null> {
export async function general(_url: URL | string, opts?: GeneralScrapingOptions): Promise<Summary | null> {
let lang = opts?.lang;
if (lang && !lang.match(/^[\w-]+(\s*,\s*[\w-]+)*$/)) lang = null;

Expand All @@ -164,6 +152,24 @@ export async function parseGeneral(_url: URL | string, opts?: GeneralScrapingOpt
contentLengthLimit: opts?.contentLengthLimit,
contentLengthRequired: opts?.contentLengthRequired,
});

return await parseGeneral(url, res);
}

function headerEqualValueContains(search: string, headerValue: string | string[] | undefined) {
if (!headerValue) {
return false;
}

if (Array.isArray(headerValue)) {
return headerValue.some(value => value.toLowerCase() === search.toLowerCase());
}

return headerValue.toLowerCase() === search.toLowerCase();
}

export async function parseGeneral(_url: URL | string, res: Awaited<ReturnType<typeof scpaping>>): Promise<Summary | null> {
const url = typeof _url === 'string' ? new URL(_url) : _url;
const $ = res.$;
const twitterCard =
$('meta[name="twitter:card"]').attr('content') ||
Expand Down
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { got, type Agents as GotAgents } from 'got';
import type { FastifyInstance } from 'fastify';
import { SummalyResult } from '@/summary.js';
import { SummalyPlugin as _SummalyPlugin } from '@/iplugin.js';
import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';
import { general, type GeneralScrapingOptions } from '@/general.js';
import { DEFAULT_OPERATION_TIMEOUT, DEFAULT_RESPONSE_TIMEOUT, agent, setAgent } from '@/utils/got.js';
import { plugins as builtinPlugins } from '@/plugins/index.js';

Expand Down Expand Up @@ -125,7 +125,7 @@ export const summaly = async (url: string, options?: SummalyOptions): Promise<Su
};

// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
const summary = await (match ? match.summarize : parseGeneral)(_url, scrapingOptions);
const summary = await (match ? match.summarize : general)(_url, scrapingOptions);

if (summary == null) {
throw new Error('failed summarize');
Expand Down
26 changes: 26 additions & 0 deletions src/plugins/bluesky.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as cheerio from 'cheerio';
import type Summary from '@/summary.js';
import { getResponse, getGotOptions } from '@/utils/got.js';
import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';

export function test(url: URL): boolean {
return url.hostname === 'bsky.app';
}

export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promise<Summary | null> {
const args = getGotOptions(url.href, opts);

// HEADで取ると404が返るためGETのみで取得
const res = await getResponse({
...args,
method: 'GET',
});
const body = res.body;
const $ = cheerio.load(body);

return await parseGeneral(url, {
body,
$,
response: res,
});
}
4 changes: 2 additions & 2 deletions src/plugins/branchio-deeplinks.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { parseGeneral, type GeneralScrapingOptions } from '@/general.js';
import { general, type GeneralScrapingOptions } from '@/general.js';
import Summary from '@/summary.js';

export function test(url: URL): boolean {
Expand All @@ -12,5 +12,5 @@ export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promis
// Web版に強制リダイレクトすることでbranch.ioの独自ページが開くのを防ぐ
url.searchParams.append('$web_only', 'true');

return await parseGeneral(url, opts);
return await general(url, opts);
}
2 changes: 2 additions & 0 deletions src/plugins/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import * as amazon from './amazon.js';
import * as bluesky from './bluesky.js';
import * as wikipedia from './wikipedia.js';
import * as branchIoDeeplinks from './branchio-deeplinks.js';
import { SummalyPlugin } from '@/iplugin.js';

export const plugins: SummalyPlugin[] = [
amazon,
bluesky,
wikipedia,
branchIoDeeplinks,
];
30 changes: 14 additions & 16 deletions src/utils/got.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import { readFileSync } from 'node:fs';
import got, * as Got from 'got';
import * as cheerio from 'cheerio';
import PrivateIp from 'private-ip';
import { StatusError } from './status-error.js';
import { detectEncoding, toUtf8 } from './encoding.js';
import type { GeneralScrapingOptions } from '@/general.js';
import { StatusError } from '@/utils/status-error.js';
import { detectEncoding, toUtf8 } from '@/utils/encoding.js';

const _filename = fileURLToPath(import.meta.url);
const _dirname = dirname(_filename);
Expand Down Expand Up @@ -36,30 +37,27 @@ export const DEFAULT_OPERATION_TIMEOUT = 60 * 1000;
export const DEFAULT_MAX_RESPONSE_SIZE = 10 * 1024 * 1024;
export const DEFAULT_BOT_UA = `SummalyBot/${repo.version}`;

export async function scpaping(
url: string,
opts?: {
lang?: string;
userAgent?: string;
responseTimeout?: number;
operationTimeout?: number;
contentLengthLimit?: number;
contentLengthRequired?: boolean;
},
) {
const args: Omit<GotOptions, 'method'> = {
export function getGotOptions(url: string, opts?: GeneralScrapingOptions): Omit<GotOptions, 'method'> {
return {
url,
headers: {
'accept': 'text/html,application/xhtml+xml',
'user-agent': opts?.userAgent ?? DEFAULT_BOT_UA,
'accept-language': opts?.lang,
'accept-language': opts?.lang ?? undefined,
},
typeFilter: /^(text\/html|application\/xhtml\+xml)/,
responseTimeout: opts?.responseTimeout,
operationTimeout: opts?.operationTimeout,
contentLengthLimit: opts?.contentLengthLimit,
contentLengthRequired: opts?.contentLengthRequired,
};
}

export async function scpaping(
url: string,
opts?: GeneralScrapingOptions,
) {
const args = getGotOptions(url, opts);

const headResponse = await getResponse({
...args,
Expand Down Expand Up @@ -110,7 +108,7 @@ export async function head(url: string) {
});
}

async function getResponse(args: GotOptions) {
export async function getResponse(args: GotOptions) {
const timeout = args.responseTimeout ?? DEFAULT_RESPONSE_TIMEOUT;
const operationTimeout = args.operationTimeout ?? DEFAULT_OPERATION_TIMEOUT;

Expand Down
Loading