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

feat: add waitUntil and waitForSelctor to linksBuilder #2437

Merged
merged 1 commit into from
Apr 25, 2021
Merged
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
26 changes: 18 additions & 8 deletions src/store/fetch-links.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Print, logger} from '../logger';
import {Browser} from 'puppeteer';
import cheerio from 'cheerio';
import {filterSeries} from './filter';
import {usingResponse} from '../util';
import {usingPage} from '../util';

function addNewLinks(store: Store, links: Link[], series: Series) {
if (links.length === 0) {
Expand All @@ -28,14 +28,15 @@ function addNewLinks(store: Store, links: Link[], series: Series) {
}

export async function fetchLinks(store: Store, browser: Browser) {
if (!store.linksBuilder) {
const linksBuilder = store.linksBuilder;
if (!linksBuilder) {
return;
}

const promises: Array<Promise<void>> = [];

// eslint-disable-next-line prefer-const
for (let {series, url} of store.linksBuilder.urls) {
for (let {series, url} of linksBuilder.urls) {
if (!filterSeries(series)) {
continue;
}
Expand All @@ -48,16 +49,25 @@ export async function fetchLinks(store: Store, browser: Browser) {

url.map(x =>
promises.push(
usingResponse(browser, x, async response => {
const text = await response?.text();
usingPage(browser, async page => {
const waitUntil = linksBuilder.waitUntil
? linksBuilder.waitUntil
: 'domcontentloaded';
await page.goto(x, {waitUntil});

if (linksBuilder.waitForSelector) {
await page.waitForSelector(linksBuilder.waitForSelector);
}

const html = await page.content();

if (!text) {
if (!html) {
logger.error(Print.message('NO RESPONSE', series, store, true));
return;
}

const docElement = cheerio.load(text).root();
const links = store.linksBuilder!.builder(docElement, series);
const docElement = cheerio.load(html).root();
const links = linksBuilder.builder(docElement, series);

addNewLinks(store, links, series);
})
Expand Down
2 changes: 2 additions & 0 deletions src/store/model/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,8 @@ export type Store = {
linksBuilder?: {
builder: (docElement: cheerio.Cheerio, series: Series) => Link[];
ttl?: number;
waitUntil?: PuppeteerLifeCycleEvent;
waitForSelector?: string;
urls: Array<{series: Series; url: string | string[]}>;
};
labels: Labels;
Expand Down