fix: dedupe url if failed to save

This commit is contained in:
Hongbo Wu 2023-05-24 20:29:35 +08:00
parent 8ddde61285
commit d69e732579
3 changed files with 26 additions and 16 deletions

View file

@ -207,6 +207,16 @@ export const parsePreparedContent = async (
let highlightData = undefined
const { document, pageInfo } = preparedDocument
if (!document) {
console.log('No document')
return {
canonicalUrl: url,
parsedContent: null,
domContent: '',
pageType: PageType.Unknown,
}
}
// Checking for content type acceptance or if there are no contentType
// at all (backward extension versions compatibility)
if (
@ -222,14 +232,15 @@ export const parsePreparedContent = async (
}
}
let dom = parseHTML(document).document
let dom: Document | null = null
try {
dom = parseHTML(document).document
if (!article) {
// Attempt to parse the article
// preParse content
const preParsedDom = await preParseContent(url, dom)
preParsedDom && (dom = preParsedDom)
dom = (await preParseContent(url, dom)) || dom
article = await getReadabilityResult(url, document, dom, isNewsletter)
}
@ -260,7 +271,7 @@ export const parsePreparedContent = async (
codeBlocks.forEach((e) => {
if (e.textContent) {
const att = hljs.highlightAuto(e.textContent)
const code = dom.createElement('code')
const code = articleDom.createElement('code')
const langClass =
`hljs language-${att.language}` +
(att.second_best?.language
@ -356,7 +367,7 @@ export const parsePreparedContent = async (
domContent: document,
parsedContent: article,
canonicalUrl,
pageType: parseOriginalContent(dom),
pageType: dom ? parseOriginalContent(dom) : PageType.Unknown,
highlightData,
}
}

View file

@ -288,7 +288,8 @@ const getTweetIds = async (
return Array.from(ids)
}, author)) as string[]
} catch (error) {
console.log(error)
console.error('Error getting tweets', error)
return []
} finally {
if (context) {

View file

@ -73,8 +73,9 @@ const fetchContentWithScrapingBee = async (url) => {
const dom = parseHTML(response.data).document;
return { title: dom.title, domContent: dom.documentElement.outerHTML, url }
} catch (e) {
console.log('error fetching with scrapingbee', e)
return { title: '', domContent: '', url }
console.error('error fetching with scrapingbee', e.message)
return { title: url, domContent: '', url }
}
}
@ -325,19 +326,16 @@ async function fetchContent(req, res) {
let readabilityResult = null;
if (content) {
let document = parseHTML(content).document;
const document = parseHTML(content).document;
// preParse content
const preParsedDom = await preParseContent(url, document)
if (preParsedDom) {
document = preParsedDom
}
const preParsedDom = (await preParseContent(url, document)) || document;
readabilityResult = await getReadabilityResult(url, document);
readabilityResult = await getReadabilityResult(url, preParsedDom);
}
const apiResponse = await sendSavePageMutation(userId, {
url: finalUrl,
url,
clientRequestId: articleSavingRequestId,
title,
originalContent: content,
@ -373,7 +371,7 @@ async function fetchContent(req, res) {
}
const apiResponse = await sendSavePageMutation(userId, {
url: finalUrl,
url,
clientRequestId: articleSavingRequestId,
title,
originalContent: content,