[ prog / sol / mona ]

prog


Small programming projects ideas

147 2022-03-14 19:54

A website that shows you a random image tagged with "cheering" from one of the boorus along with an encouraging message. I used to have one like this but it is gone now.

148 2022-03-18 22:18

>>147
I hope you have netcat, curl and jq. And Coreutils. And Bash.

#!/bin/bash

# Config
BOORU_URL="${BOORU_URL:-https://danbooru.donmai.us}"
SEARCH_QUERY="${SEARCH_QUERY:-tags=cheering+rating%3Asafe}"
CURL_ARGS="$CURL_ARGS"
CHEERING_COOKIES="${CHEERING_COOKIES:-cheering-cookies.txt}"
PORT="${PORT:-8080}"
# Used for querying the booru
POSTS_PER_PAGE="${POSTS_PER_QUERY:-100}"
MAX_PAGES="${MAX_PAGES:-750}"

function response {
  POSTS_COUNT="$(curl -s $CURL_ARGS "$BOORU_URL/counts/posts.json?$SEARCH_QUERY" | jq -r ".counts.posts")"
  MAX_POSTS_COUNT=$(($MAX_PAGES * $POSTS_PER_PAGE))
  [ $POSTS_COUNT -lt $MAX_POSTS_COUNT ] || POSTS_COUNT=$MAX_POSTS_COUNT
  POST="$(curl -s $CURL_ARGS "$BOORU_URL/posts.json?$SEARCH_QUERY&limit=$POSTS_PER_PAGE&page=$((1 + $RANDOM % ($POSTS_COUNT / $POSTS_PER_PAGE)))" | jq -rc ".[]" | shuf -n 1)"
  CHEERING_COOKIE="$(shuf -n 1 "$CHEERING_COOKIES")"
  cat <<EOF
HTTP/1.1 200 OK
Content-Type: text/html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width,initial-scale=1">
</head>
<body style="text-align: center">
  <p><a href="$(echo "$POST" | jq -r .file_url)"><img height="600px" src="$(echo "$POST" | jq -r .large_file_url)"></img></a></p>
  <p style="font-size:larger">$CHEERING_COOKIE</p>
</body>
</html>
EOF
}

while true; do response | netcat -l "$PORT" || exit $?; done

Sample cheering-cookies.txt is over here: https://termbin.com/n65r

149 2022-03-19 11:56

It gets stuck as the browser can't tell that the whole page arrived.

150 2022-03-19 12:43

>>149
add -q 0 if youre using openbsd-netcat

163


VIP:

do not edit these