-
-
Notifications
You must be signed in to change notification settings - Fork 292
feat: add parsing of response body for encoding #501
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
Closed
phawxby
wants to merge
2
commits into
website-scraper:master
from
xeroxinteractive:PH-response-body-encoding
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,7 @@ | ||
| .idea | ||
| .DS_Store | ||
| node_modules | ||
| package-lock.json | ||
| npm-debug.log | ||
| coverage | ||
| test/e2e/results | ||
| .nyc-output | ||
| .nyc-output |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,94 @@ | ||
| import got from 'got'; | ||
| import logger from './logger.js'; | ||
| import { extend } from './utils/index.js'; | ||
|
|
||
| function getMimeType (contentType) { | ||
| return contentType ? contentType.split(';')[0] : null; | ||
| } | ||
| import * as cheerio from 'cheerio'; | ||
|
|
||
| function defaultResponseHandler ({response}) { | ||
| return Promise.resolve(response); | ||
| } | ||
|
|
||
| function extractEncodingFromHeader (headers) { | ||
| const contentTypeHeader = headers['content-type']; | ||
| function extractEncodingFromResponse (response) { | ||
| if (typeof response?.headers === 'object') { | ||
| const contentTypeHeader = response.headers['content-type']; | ||
|
|
||
| return contentTypeHeader && contentTypeHeader.includes('utf-8') ? 'utf8' : 'binary'; | ||
| } | ||
|
|
||
| return undefined; | ||
| } | ||
|
|
||
| function extractMimeTypeFromResponse (response) { | ||
| if (typeof response?.headers === 'object') { | ||
| const contentTypeHeader = response.headers['content-type']; | ||
|
|
||
| if (typeof contentTypeHeader === 'string') { | ||
| return contentTypeHeader.split(';')[0]; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
|
|
||
| function extractEncodingFromHtmlResponse (response) { | ||
| try { | ||
| const body = getBodyAsString(response); | ||
|
|
||
| if (body) { | ||
| const $ = cheerio.load(body); | ||
| const charset = $('meta[charset]').attr('charset'); | ||
|
|
||
| if (charset && charset.toLowerCase() === 'utf-8') { | ||
| return 'utf-8'; | ||
| } | ||
| } | ||
|
|
||
| } catch (err) { | ||
| logger.error('Error parsing response html', response.url); | ||
| } | ||
|
|
||
| return contentTypeHeader && contentTypeHeader.includes('utf-8') ? 'utf8' : 'binary'; | ||
| return 'binary'; | ||
| } | ||
|
|
||
| function extractEncodingFromCssResponse (response) { | ||
| try { | ||
| const body = getBodyAsString(response); | ||
|
|
||
| if (body && body.includes('@charset "UTF-8"')) { | ||
| return 'utf-8'; | ||
| } | ||
|
|
||
| } catch (err) { | ||
| logger.error('Error parsing response html', response.url); | ||
| } | ||
|
|
||
| return 'binary'; | ||
| } | ||
|
|
||
| function getEncodingByMime (response) { | ||
| switch (extractMimeTypeFromResponse(response)) { | ||
| case 'text/html': | ||
| return extractEncodingFromHtmlResponse(response); | ||
|
|
||
| case 'text/css': | ||
| return extractEncodingFromCssResponse(response); | ||
|
|
||
| default: | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| function getEncoding (response) { | ||
| let encoding = 'binary'; | ||
|
|
||
| if (response && typeof response === 'object') { | ||
| if (response.headers && typeof response.headers === 'object') { | ||
| return extractEncodingFromHeader(response.headers); | ||
| } else if (response.encoding) { | ||
| return response.encoding; | ||
| encoding = response.encoding || extractEncodingFromResponse(response) || encoding; | ||
|
|
||
| if (encoding === 'binary') { | ||
| encoding = getEncodingByMime(response) || encoding; | ||
| } | ||
| } | ||
|
|
||
| return 'binary'; | ||
| return encoding; | ||
| } | ||
|
|
||
| function throwTypeError (result) { | ||
|
|
@@ -42,15 +105,15 @@ function throwTypeError (result) { | |
|
|
||
| function getData (result) { | ||
| let data = result; | ||
| if (result && typeof result === 'object' && 'body' in result) { | ||
|
|
||
| if (result?.body !== undefined) { | ||
| data = result.body; | ||
| } | ||
|
|
||
| return data; | ||
| } | ||
|
|
||
| function transformResult (result) { | ||
| const encoding = getEncoding(result); | ||
| function getBodyAsString (result, encoding) { | ||
| const data = getData(result); | ||
|
|
||
| // Check for no data | ||
|
|
@@ -61,17 +124,29 @@ function transformResult (result) { | |
| // Then stringify it. | ||
| let body = null; | ||
| if (data instanceof Buffer) { | ||
| body = data.toString(encoding); | ||
| body = data.toString(encoding || 'binary'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need |
||
| } else if (typeof data === 'string') { | ||
| body = data; | ||
| } else { | ||
| throwTypeError(result); | ||
| } | ||
|
|
||
| return body; | ||
| } | ||
|
|
||
| function transformResult (result) { | ||
| const encoding = getEncoding(result); | ||
| const data = getData(result); | ||
| const body = getBodyAsString(result, encoding); | ||
|
|
||
| if (data === null || body === null) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| body, | ||
| encoding, | ||
| metadata: result.metadata || data.metadata || null | ||
| metadata: (result && result.metadata) || (data && data.metadata) || null | ||
| }; | ||
| } | ||
|
|
||
|
|
@@ -92,9 +167,10 @@ async function getRequest ({url, referer, options = {}, afterResponse = defaultR | |
| if (!responseHandlerResult) { | ||
| return null; | ||
| } | ||
|
|
||
| return { | ||
| url: response.url, | ||
| mimeType: getMimeType(response.headers['content-type']), | ||
| mimeType: extractMimeTypeFromResponse(response), | ||
| body: responseHandlerResult.body, | ||
| metadata: responseHandlerResult.metadata, | ||
| encoding: responseHandlerResult.encoding | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do we return
undefinedfrom this functions, butnullfromextractMimeTypeFromResponse? Maybe it will be better to return same resule (null or undefined) from both functions because they seem to be very similar.