Biblioteka queries
Biblioteka queriesImportuj HTML z URL-i jako nowe posty w WordPress

Importuj HTML z URL-i jako nowe posty w WordPress

To query importuje strony HTML z podanych URL-i jako nowe posty w WordPress.

Dla każdego URL pobiera tytuł z <title>...</title> w sekcji meta oraz treść z <body>...</body>, lub dostosowaną do konkretnego wewnętrznego elementu HTML za pomocą zmiennej $contentMatchInnerRegex.

Za pomocą $contentMatchInnerRegex możemy określić, którą podczęść HTML elementu <body> chcemy przechwycić.

Na przykład, jeśli treść ma być wyodrębniona z:

<article class="main">...</article>

...możemy ją przechwycić za pomocą:

{
  "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
    }
  }
}