Skip to content

Instantly share code, notes, and snippets.

@zeratax
Last active September 20, 2022 15:57
Show Gist options
  • Save zeratax/5213bc07eb29f682f816b33d65c9f967 to your computer and use it in GitHub Desktop.
Save zeratax/5213bc07eb29f682f816b33d65c9f967 to your computer and use it in GitHub Desktop.
scriptable booru metadata
/*
function showNotification(title, message) {
const notification = new Notification()
notification.title = title
notification.subtitle = message
notification.schedule()
}
*/
// Script.setShortcutOutput(1)
let settings, link
if (!args.shortcutParameter || !args.plainTexts) {
const path = await DocumentPicker.open(["public.json"])
const files = FileManager.iCloud()
files.downloadFileFromiCloud(path[0])
settings = JSON.parse(files.readString(path[0]))
const alert = new Alert()
alert.title = "Enter link:"
alert.message = "a booru link to retrieve metadata"
alert.addTextField("https://danbooru.donmai.us/posts/1")
alert.addAction("OK")
await alert.presentAlert()
link = alert.textFieldValue(0)
} else {
const input = args.plainTexts.join(" ")
const re = /(?:https?:\/\/)?(?:www.)?(danbooru\.donmai\.us|gelbooru\.com|yande\.re|e621\.net)\/[\w?&+=\/\.]+?(\d+)/ig
const matches = input.match(re)
if (!matches) {
console.error("No supported booru link found")
return
}
link = matches[0]
settings = args.shortcutParameter
}
console.log(link)
const DANBOORU = "DANBOORU"
const YANDERE = "YANDE.RE"
const E621 = "E621"
const GELBOORU = "GELBOORU"
let current_website
if (link.includes("danbooru.donmai.us")) {
current_website = DANBOORU
}
if (link.includes("yande.re")) {
current_website = YANDERE
}
if (link.includes("e621.net")) {
current_website = E621
}
if (link.includes("gelbooru.com")) {
current_website = GELBOORU
}
console.log(current_website)
let username, password, api_uri, id
switch (current_website) {
case DANBOORU:
username = settings.danbooru_username
password = settings.danbooru_api_key
api_uri = `${link}.json`
break
case E621:
username = settings.e621_username
password = settings.e621_api_key
api_uri = `${link}.json`
break
case YANDERE:
username = settings.yandere_username
password = settings.yandere_api_key
id = link.split("/").pop()
api_uri = `https://yande.re/post.json?tags=id:${id}`
break
case GELBOORU:
id = link.split("&id=").pop()
api_uri = `https://gelbooru.com/index.php?page=dapi&s=post&q=index&json=1&id=${id}`
break
}
console.log(id)
console.log(api_uri)
const request = new Request(api_uri)
if (username && password) {
request.headers = {
"Authorization": `Basic ${btoa(username + ":" + password)}`,
"User-Agent": "Booru Metadata iOS Shortcut"
}
}
console.log(request)
const response = await request.loadJSON()
if (response.success == false) {
console.error(JSON.stringify(response))
Script.setShortcutOutput(JSON.stringify(response))
return
}
console.log(response)
let message = ""
let artists, copyrights, characters, tags, source
switch (current_website) {
case DANBOORU:
artists = response.tag_string_artist
artists = artists?.split(" ")
copyrights = response.tag_string_copyright
copyrights = copyrights?.split(" ")
characters = response.tag_string_character
characters = characters?.split(" ")
tags = response.tag_string
tags = tags?.split(" ")
source = response.source
break
case E621:
artists = response.post.tags.artist
copyrights = response.post.tags.copyright
characters = response.post.tags.character
tags = response.post.tags.general
source = response.post.sources
source = source.join("\n")
break
case YANDERE:
tags = response[0].tags
tags = tags?.split(" ")
source = response[0].source
break
case GELBOORU:
tags = response.post[0].tags
tags = tags?.split(" ")
source = response.post[0].source
break
}
console.log(artists)
if (artists) {
message += `Artist${artists.length> 1 ? "s" : ""}: ${artists.join(", ")}\n\n`
}
console.log(copyrights)
if (copyrights) {
message += `Series: ${copyrights.join(", ")}\n\n`
}
console.log(characters)
if (characters) {
message += `Character${characters.length > 1 ? "s" : ""}: ${characters.join(", ")}\n\n`
}
console.log(tags)
if (tags) {
message += `Tag${tags.length > 1 ? "s" : ""}: ${tags.join(", ")}\n\n`
}
message += link + "\n"
message += source
message = message.replace(/_/g, " ")
message = message.trim()
console.log(message)
Script.setShortcutOutput(message)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment