쿼리 라이브러리
쿼리 라이브러리URL에서 HTML을 가져와 WordPress에 새 게시물로 추가하기

URL에서 HTML을 가져와 WordPress에 새 게시물로 추가하기

이 쿼리는 지정된 URL의 HTML 페이지를 WordPress의 새 게시물로 가져옵니다.

각 URL에서 메타 내의 <title>...</title>로부터 제목을 가져오고, <body>...</body>에서 콘텐츠를 가져옵니다. 또는 $contentMatchInnerRegex 변수를 사용하여 특정 내부 HTML 요소를 지정할 수도 있습니다.

$contentMatchInnerRegex를 사용하면 <body>의 HTML에서 캡처할 특정 하위 부분을 지정할 수 있습니다.

예를 들어, 다음에서 콘텐츠를 추출해야 하는 경우:

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

...다음과 같이 캡처할 수 있습니다:

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