Query-bibliotheekImporteer HTML van URL's als nieuwe posts in WordPress
Importeer HTML van URL's als nieuwe posts in WordPress
Deze query importeert de HTML-pagina's van de opgegeven URL's als nieuwe posts in WordPress.
Voor elke URL haalt hij de titel op uit <title>...</title> in de meta, en de inhoud uit <body>...</body>, of aangepast aan een specifiek inner HTML-element via de variabele $contentMatchInnerRegex.
Met $contentMatchInnerRegex kun je bepalen welk specifiek gedeelte van de HTML van <body> je wilt vastleggen.
Als de inhoud bijvoorbeeld moet worden uitgehaald uit:
<article class="main">...</article>...kun je die vastleggen met:
{
"contentMatchInnerRegex": ".*?<\\s*?article\\b[^>]*>(.*?)<\\/article\\b[^>]*>.*?"
}query GenerateURLInputs(
$urls: [URL!]!
$contentMatchInnerRegex: String! = "(.*?)"
) {
urlInputs: _echo(value: $urls)
@underEachArrayItem(
passValueOnwardsAs: "url"
)
@applyField(
name: "_echo",
arguments: {
value: {
url: $url,
method: GET
}
},
setResultInResponse: true
)
@export(as: "urlInputs")
@remove
contentMatchRegex: _sprintf(
string: "/(?:<!DOCTYPE html>)?<\\s*?html\\b[^>]*>.*?<\\s*?body\\b[^>]*>%s<\\/body\\b[^>]*>.*?<\\/html\\b[^>]*>/sim",
values: [$contentMatchInnerRegex]
)
@export(as: "contentMatchRegex")
}
query RequestPages
@depends(on: "GenerateURLInputs")
{
urlContents: _sendHTTPRequests(inputs: $urlInputs, async: false) {
statusCode
body
@remove
title: _strRegexReplace(
searchRegex: "/(?:<!DOCTYPE html>)?<\\s*?html\\b[^>]*>.*?<head\\b[^>]*>.*?<\\s*?title\\b[^>]*>(.*?)<\\/title\\b[^>]*>.*?<\\/head\\b[^>]*>(.*?)<\\/html\\b[^>]*>/sim"
replaceWith: "$1"
in: $__body
)
content: _strRegexReplace(
searchRegex: $contentMatchRegex
replaceWith: "$1"
in: $__body
)
createPostInput: _echo(value: {
status: publish,
title: $__title
contentAs: {
html: $__content
}
})
@export(as: "createPostInputs", type: LIST)
}
}
mutation CreatePostsFromURLs
@depends(on: "RequestPages")
{
createPosts(inputs: $createPostInputs) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
status
title
content
}
}
}