Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save leoloso/932d8c3afaa021039e902f70868c8849 to your computer and use it in GitHub Desktop.
Save leoloso/932d8c3afaa021039e902f70868c8849 to your computer and use it in GitHub Desktop.
Create WP posts from external HTML files
query GenerateURLs(
$total: Int!
$offset: Int!
$limit: Int! = 10
) {
numbers: _arrayFill(count: $total, value: "")
@remove
boundNumbers: _arraySlice(
array: $__numbers,
offset: $offset,
length: $limit
)
@remove
urls: _echo(value: $__boundNumbers)
@underEachArrayItem(
passIndexOnwardsAs: "index",
affectDirectivesUnderPos: [1, 2, 3]
)
@applyField(
name: "_intAdd",
arguments: {
add: 1,
to: $index
},
passOnwardsAs: "indexPlusOne"
)
@applyField(
name: "_intAdd",
arguments: {
add: $offset,
to: $indexPlusOne
},
passOnwardsAs: "indexPlusOffset"
)
@applyField(
name: "_sprintf",
arguments: {
string: "https://vitalweekly.net/%d.html",
values: $indexPlusOffset
},
setResultInResponse: true
)
@export(as: "urls")
}
query GenerateURLInputs
@depends(on: "GenerateURLs")
{
urlInputs: _echo(value: $urls)
@underEachArrayItem(
passValueOnwardsAs: "url"
)
@applyField(
name: "_echo",
arguments: {
value: {
url: $url,
method: GET
}
},
setResultInResponse: true
)
@export(as: "urlInputs")
@remove
}
query RequestPages
@depends(on: "GenerateURLInputs")
{
urlContents: _sendHTTPRequests(inputs: $urlInputs, async: false) {
body
@remove
content: _strRegexReplace(
searchRegex: "/<\\s*?html\\b[^>]*>(.*?)<\\s*?body\\b[^>]*>(.*?)<\\/body\\b[^>]*>(.*?)<\\/html\\b[^>]*>/sim"
replaceWith: "$2"
in: $__body
)
@export(as: "contents", type: LIST)
@remove
statusCode
}
}
query GeneratePostInputs(
$offset: Int!
)
@depends(on: "RequestPages")
{
postInputs: _echo(value: $urls)
@underEachArrayItem(
passIndexOnwardsAs: "index"
passValueOnwardsAs: "url"
affectDirectivesUnderPos: [1, 2, 3, 4, 5]
)
@applyField(
name: "_arrayItem",
arguments: {
array: $contents,
position: $index
},
passOnwardsAs: "content"
)
@applyField(
name: "_intAdd",
arguments: {
add: 1,
to: $index
},
passOnwardsAs: "indexPlusOne"
)
@applyField(
name: "_intAdd",
arguments: {
add: $offset,
to: $indexPlusOne
},
passOnwardsAs: "indexPlusOffset"
)
@applyField(
name: "_sprintf",
arguments: {
string: "Number: %d",
values: [$indexPlusOffset]
},
passOnwardsAs: "title"
)
@applyField(
name: "_echo",
arguments: {
value: {
status: publish,
contentAs: {
html: $content
},
title: $title
}
},
setResultInResponse: true
)
@export(as: "postInputs")
@remove
}
mutation CreatePosts
@depends(on: "GeneratePostInputs")
{
createdPostIDs: _echo(value: $postInputs)
@underEachArrayItem(
passValueOnwardsAs: "input"
)
@applyField(
name: "createPost"
arguments: {
input: $input
},
setResultInResponse: true
)
@export(as: "createdPostIDs")
@remove
}
query RetrieveCreatedPosts
@depends(on: "CreatePosts")
{
createdPosts: posts(
filter: {
ids: $createdPostIDs
}
) {
id
title
content
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment