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

v7.1.2 #129

Merged
merged 1 commit into from
Apr 26, 2024
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "7.1.1",
"version": "7.1.2",
"name": "@extractus/feed-extractor",
"description": "To read and normalize RSS/ATOM/JSON feed data",
"homepage": "https://extractor-demos.pages.dev",
Expand Down
18 changes: 17 additions & 1 deletion src/utils/retrieve.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// utils -> retrieve

import fetch from 'cross-fetch'
import { XMLParser } from 'fast-xml-parser'

const profetch = async (url, options = {}) => {
const { proxy = {}, signal = null } = options
Expand All @@ -15,6 +16,20 @@ const profetch = async (url, options = {}) => {
return res
}

const getCharsetFromText = (text) => {
try {
const firstLine = text.split('\n')[0].trim().replace('<?', '<').replace('?>', '>')
const parser = new XMLParser({
ignoreAttributes: false,
})
let obj = parser.parse(firstLine)
const { xml: root = {} } = obj
return root['@_encoding'] || 'utf8'
} catch {
return 'utf8'
}
}

export default async (url, options = {}) => {
const {
headers = {
Expand All @@ -35,9 +50,10 @@ export default async (url, options = {}) => {
const buffer = await res.arrayBuffer()
const text = buffer ? Buffer.from(buffer).toString().trim() : ''

console.log(contentType)
if (/(\+|\/)(xml|html)/.test(contentType)) {
const arr = contentType.split('charset=')
const charset = arr.length === 2 ? arr[1].trim() : 'utf8'
let charset = arr.length === 2 ? arr[1].trim() : getCharsetFromText(text)
const decoder = new TextDecoder(charset)
const xml = decoder.decode(buffer)
return { type: 'xml', text: xml.trim(), status, contentType }
Expand Down
Loading