Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
kakkokari-gtyih committed Nov 11, 2024
1 parent 7a63aa1 commit 4530a01
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 22 deletions.
13 changes: 6 additions & 7 deletions src/plugins/bluesky.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import * as cheerio from 'cheerio';
import type Summary from '@/summary.js';
import { getResponse } from '@/utils/got.js';
import { parseGeneral } from '@/general.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): Promise<Summary | null> {
export async function summarize(url: URL, opts?: GeneralScrapingOptions): Promise<Summary | null> {
const args = getGotOptions(url.href, opts);

// HEADで取ると404が返るためGETのみで取得
const res = await getResponse({
url: url.href,
...args,
method: 'GET',
headers: {
'accept': '*/*',
},
});
const body = res.body;
const $ = cheerio.load(body);
Expand Down
28 changes: 13 additions & 15 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 { StatusError } from '@/utils/status-error.js';
import { detectEncoding, toUtf8 } from '@/utils/encoding.js';
import type { GeneralScrapingOptions } from '@/general.js';

Check warning on line 9 in src/utils/got.ts

View workflow job for this annotation

GitHub Actions / lint (20.10.0)

`@/general.js` type import should occur before import of `@/utils/status-error.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

0 comments on commit 4530a01

Please sign in to comment.