-
-
Save ztgasdf/5e38f1b7ca63fb58bdb68a8b9bc73ccb to your computer and use it in GitHub Desktop.
szuru static thing i don't know
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
start_time=$(date +%s.%3N) | |
# Get json response from szurubooru | |
jsonoutput=$(curl -H "Accept: application/json" -s http://127.0.0.1:8082/api/post/$1) | |
# Error handler, if user inputs non-existant post ID or invalid ID. | |
result=$(echo $jsonoutput | jq -r .name) | |
if [[ $? -eq 4 ]] | |
then | |
echo "FAIL!" | |
exit 1 | |
else | |
if [[ $result = "PostNotFoundError" ]] | |
then | |
echo "FAIL!" | |
exit 1 | |
else | |
: | |
fi | |
fi | |
# Gets main URL for media, removes '/data/' from it as well | |
url=$(echo $jsonoutput | jq -r .contentUrl | cut -c 6-) | |
# Gets mimetype for opengraph tag | |
mime=$(echo $jsonoutput | jq -r .mimeType) | |
# Gets mime category (image/video) for opengraph tag | |
mimename=$(echo $jsonoutput | jq -r .mimeType | head -c 5) | |
# Gets file extension and capitalizes it | |
type=$(echo $jsonoutput | jq -r .mimeType | sed 's/image\///g' | sed 's/video\///g' | tr '[:lower:]' '[:upper:]') | |
# Gets image width | |
width=$(echo $jsonoutput | jq -r .canvasWidth) | |
# Gets image height | |
height=$(echo $jsonoutput | jq -r .canvasHeight) | |
# Gets artist tag, removes dnp tag if exists | |
artist=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "artist") | .names[]' | sed '/dnp/d') | |
# Gets source urls for post | |
source=$(echo $jsonoutput | jq -r .source) | |
# Gets filesize and makes it natural | |
filesize=$(echo $jsonoutput | jq -r .fileSize | numfmt --to iec) | |
#tag acquiring, explanation TODO | |
artisttag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "artist") | .names[0]') | |
mainchrtag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "mainchr") | .names[0]') | |
notmainchrtag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "notmainchr") | .names[0]') | |
notomorichrtag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "notomorichr") | .names[0]') | |
metatag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "meta") | .names[0]') | |
defaulttag1=$(echo $jsonoutput | jq -r '.tags[] | select(.category == "default") | .names[0]') | |
if [[ -z $artisttag1 ]] | |
then | |
artisttag2="" | |
else | |
artisttag2="<span style=\"color:#c00004\">$artisttag1</span>" | |
fi | |
if [[ -z $mainchrtag1 ]] | |
then | |
mainchrtag2="" | |
else | |
mainchrtag2=" | |
<span style=\"color:#00ab2c\">$mainchrtag1</span>" | |
fi | |
if [[ -z $notmainchrtag1 ]] | |
then | |
notmainchrtag2="" | |
else | |
notmainchrtag2=" | |
<span style=\"color:#007620\">$notmainchrtag1</span>" | |
fi | |
if [[ -z $notomorichrtag1 ]] | |
then | |
notomorichrtag2="" | |
else | |
notomorichrtag2=" | |
<span style=\"color:#f33abb\">$notomorichrtag1</span>" | |
fi | |
if [[ -z $metatag1 ]] | |
then | |
metatag2="" | |
else | |
metatag2=" | |
<span style=\"color:#fd9200\">$metatag1</span>" | |
fi | |
if [[ -z $defaulttag1 ]] | |
then | |
defaulttag2="" | |
else | |
defaulttag2=" | |
<span style=\"color:#24aadd\">$defaulttag1</span>" | |
fi | |
# Gets all comments, which should include post description (if exists) and timestamp | |
# TODO: Find a way to parse the comments so I don't have to manually edit the post descriptions | |
# the sed command removes the extra '\'s i added to disable szurubooru formatting in the comments | |
comments1=$(echo $jsonoutput | jq -r '.comments[].text' | sed 's/\\//g') | |
if [[ -z $comments1 ]] | |
then | |
comments2="" | |
else | |
comments2=$(echo " | |
<i>Post comments</i> (Text with \">\" is text from the original post.) | |
<div class=\"pre\">$comments1</div>") | |
fi | |
# Gets post relations (if exists), separates them with commas | |
relations=$(echo $jsonoutput | jq -r '.relations[].id' | sed -z 's/\n/,/g;s/,$/\n/') | |
# Base URL for public sharing | |
url="https://gitlab.com/sussyballs/art1/-/raw/main/public/$url" | |
# If post is video/image, set html + twitter card to respective value | |
if [[ $mimename = "video" ]] | |
then | |
html="<video style=\"max-width:100%\" controls><source src=\"$url\" type=\"$mime\"></video>" | |
twitter="player" | |
else | |
html="<img src=\"$url\" style=\"max-width:100%\">" | |
twitter="summary_large_image" | |
fi | |
# If post has relations, set relation HTML | |
if [[ -z $relations ]] | |
then | |
: | |
else | |
relationhtml=" | |
related posts: $relations" | |
fi | |
# Output complete HTML result. | |
echo "<!DOCTYPE html> | |
<head> | |
<title>${width}x${height} $type $mimename by $artist</title> | |
<meta charset=\"UTF-8\"> | |
<style> | |
.pre{white-space:pre-wrap;font-family:monospace} | |
aside {float:left;padding-right:8px} | |
.utterances {float:left} | |
</style> | |
<meta property=\"og:title\" content=\"$width"x"$height $type $mimename by $artist\"> | |
<meta property=\"og:$mimename\" content=\"$url\"> | |
<meta property=\"og:$mimename:type\" content=\"$mime\"> | |
<meta property=\"og:$mimename:width\" content=\"$width\"> | |
<meta property=\"og:$mimename:height\" content=\"$height\"> | |
<meta name=\"twitter:card\" content=\"$twitter\"> | |
<body> | |
<aside><b>Post #$1</b><br> | |
<a href=\"$url\">${filesize} $type</a> (${width}x$height)<br> | |
<b>Tags</b> | |
<div class="pre">$artisttag2$mainchrtag2$notmainchrtag2$notomorichrtag2$metatag2$defaulttag2</div> | |
</aside> | |
<div style=\"overflow:auto\">$html | |
<div class=\"pre\">Source URL(s): | |
$source | |
$relationhtml</div> | |
<hr>$comments2 | |
<i>Generated in $(echo "scale=3; $(date +%s.%3N) - $start_time" | bc)s on $(date --rfc-3339=seconds | sed 's/ /T/g').</i></div> | |
</body>" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment