From 86191e388a6e1d0013ad8ef723609fb669b3c953 Mon Sep 17 00:00:00 2001 From: Wyatt Johnson Date: Wed, 5 Feb 2020 16:35:57 +0000 Subject: [PATCH] feat: added support for customUserAgent (#2825) --- .../sections/Advanced/StoryCreationConfig.tsx | 34 +++++++++++++ .../__snapshots__/advanced.spec.tsx.snap | 48 +++++++++++++++++++ src/core/server/graph/loaders/Stories.ts | 9 ++-- src/core/server/graph/schema/schema.graphql | 12 +++++ .../services/stories/scraper/scraper.ts | 12 ++++- src/locales/en-US/admin.ftl | 4 ++ 6 files changed, 114 insertions(+), 5 deletions(-) diff --git a/src/core/client/admin/routes/Configure/sections/Advanced/StoryCreationConfig.tsx b/src/core/client/admin/routes/Configure/sections/Advanced/StoryCreationConfig.tsx index c367002d2..75dc9e311 100644 --- a/src/core/client/admin/routes/Configure/sections/Advanced/StoryCreationConfig.tsx +++ b/src/core/client/admin/routes/Configure/sections/Advanced/StoryCreationConfig.tsx @@ -26,6 +26,7 @@ graphql` scraping { enabled proxyURL + customUserAgent } disableLazy } @@ -117,6 +118,39 @@ const StoryCreationConfig: FunctionComponent = ({ disabled }) => ( )} + + + + + + } + > + + When specified, overrides the User-Agent header sent + with each scrape request. + + + + + {({ input, meta }) => ( + + )} + + ); diff --git a/src/core/client/admin/test/configure/__snapshots__/advanced.spec.tsx.snap b/src/core/client/admin/test/configure/__snapshots__/advanced.spec.tsx.snap index 15bde59be..9ecd0a802 100644 --- a/src/core/client/admin/test/configure/__snapshots__/advanced.spec.tsx.snap +++ b/src/core/client/admin/test/configure/__snapshots__/advanced.spec.tsx.snap @@ -625,6 +625,54 @@ proxy as parsed by the +
+
+ +

+ When specified, overrides the + + User-Agent + + header sent with each +scrape request. +

+
+
+
+ +
+
+
diff --git a/src/core/server/graph/loaders/Stories.ts b/src/core/server/graph/loaders/Stories.ts index 730e3fa1e..d6bb2e90c 100644 --- a/src/core/server/graph/loaders/Stories.ts +++ b/src/core/server/graph/loaders/Stories.ts @@ -119,9 +119,12 @@ export default (ctx: GraphContext) => ({ // This typecast is needed because the custom `ms` format does not return // the desired `number` type even though that's the only type it can // output. - scraper.scrape(url, (ctx.config.get( - "scrape_timeout" - ) as unknown) as number) + scraper.scrape( + url, + (ctx.config.get("scrape_timeout") as unknown) as number, + ctx.tenant.stories.scraping.customUserAgent, + ctx.tenant.stories.scraping.proxyURL + ) ), { // Disable caching for the DataLoader if the Context is designed to be diff --git a/src/core/server/graph/schema/schema.graphql b/src/core/server/graph/schema/schema.graphql index ba16baa2e..9b3d03912 100644 --- a/src/core/server/graph/schema/schema.graphql +++ b/src/core/server/graph/schema/schema.graphql @@ -1150,6 +1150,12 @@ type StoryScrapingConfiguration { the [proxy-agent](https://www.npmjs.com/package/proxy-agent) package. """ proxyURL: String + + """ + customUserAgent when specified will override the user agent used by fetch + requests made during the scraping process. + """ + customUserAgent: String } """ @@ -3519,6 +3525,12 @@ input StoryScrapingConfigurationInput { the [proxy-agent](https://www.npmjs.com/package/proxy-agent) package. """ proxyURL: String + + """ + customUserAgent when specified will override the user agent used by fetch + requests made during the scraping process. + """ + customUserAgent: String } """ diff --git a/src/core/server/services/stories/scraper/scraper.ts b/src/core/server/services/stories/scraper/scraper.ts index 4ea1b0e23..9c9277bd6 100644 --- a/src/core/server/services/stories/scraper/scraper.ts +++ b/src/core/server/services/stories/scraper/scraper.ts @@ -86,6 +86,7 @@ class Scraper { public async download( url: string, abortAfterMilliseconds: number, + customUserAgent?: string, proxyURL?: string ) { const log = this.log.child({ storyURL: url }, true); @@ -95,7 +96,7 @@ class Scraper { const options: RequestInit = { headers: { - "User-Agent": `Talk Scraper/${version}`, + "User-Agent": customUserAgent || `Talk Scraper/${version}`, }, signal: controller.signal, }; @@ -136,9 +137,15 @@ class Scraper { public async scrape( url: string, abortAfterMilliseconds: number, + customUserAgent?: string, proxyURL?: string ): Promise { - const html = await this.download(url, abortAfterMilliseconds, proxyURL); + const html = await this.download( + url, + abortAfterMilliseconds, + customUserAgent, + proxyURL + ); if (!html) { return null; } @@ -199,6 +206,7 @@ export async function scrape( const metadata = await scraper.scrape( storyURL, abortAfterMilliseconds, + tenant.stories.scraping.customUserAgent, tenant.stories.scraping.proxyURL ); if (!metadata) { diff --git a/src/locales/en-US/admin.ftl b/src/locales/en-US/admin.ftl index 3e74c8bfa..d21390449 100644 --- a/src/locales/en-US/admin.ftl +++ b/src/locales/en-US/admin.ftl @@ -885,6 +885,10 @@ configure-advanced-stories-proxy-detail = When specified, allows scraping requests to use the provided proxy. All requests will then be passed through the appropriote proxy as parsed by the npm proxy-agent package. +configure-advanced-stories-custom-user-agent = Custom Scraper User Agent Header +configure-advanced-stories-custom-user-agent-detail = + When specified, overrides the User-Agent header sent with each + scrape request. forgotPassword-forgotPasswordHeader = Forgot password? forgotPassword-checkEmailHeader = Check your email