My Scripts.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bin/walls

238 lines
4.4 KiB

#!/bin/bash
# Author: https://github.com/skiqqy
# This script will fetch pictures from a thread from 4chan and test them for wallpapers.
# Our variables
VERSION="v1.0"
declare -A threads # Hashmap of title to thread id
titles=() # Array containing titles from the catalog
usage ()
{
cat << EOF
walls $VERSION ~ The easy way to browse 4chan wallpapers.
Usage: walls [options]
options
-------
-h Shows this message.
-b <board> Specify the board.
-t <n> Only fetch the top n threads.
-a Fetch all threads.
-u Test wallpapers using setwp (found here https://github.com/skiqqy/bin/blob/master/setwp).
-o <number> Open the first n wallpapers in your browser.
-s <thread> Specify a thread to use.
Written by skiqqy.
EOF
}
# Fetch links to pictures from a thread.
# Usage: thread_pics <board> <thread_nr>
# TODO: Improve thise regex, grep was being wierd with \w etc, hence the scuffed version below.
thread_pics ()
{
res=$(curl -s https://boards.4chan.org/"$1"/thread/"$2" | \
grep -o -E "href=\"[/\.a-zA-Z0-9]*\.(jpg|png)\"" | uniq)
for l in $res
do
url=$(echo "$l" | cut -d= -f 2)
url="https:${url//\"}"
echo "$url"
done
}
# Get the title of a thread from a board
# Usage: thread_title <board> <thread>
# Prints: <Thread Title>:
# TODO: Improve thise regex, grep was being wierd with \w etc, hence the scuffed version below.
thread_title ()
{
res=$(curl -s https://boards.4chan.org/"$1"/thread/"$2" | \
grep -o -E "<span class=\"subject\">[/\\\.a-zA-Z0-9\ ]*</span>" | head -1)
res=$(echo "$res" | cut -d ">" -f 2 | cut -d "<" -f 1)
[ -n "$res" ] && echo "$res:$2"
}
# Fetch threads from a board
# Usage: board_threads <board>
board_threads ()
{
curl -s https://a.4cdn.org/"$1"/catalog.json | jq '.[].threads[].no'
}
print_tui ()
{
clear # Clear the screen so that the printing looks nice
echo "To exit type 'EXIT'"
echo -e "-------------------\n"
i=0
for title in "${titles[@]}"
do
echo "$i. $title"
((i++))
done
echo
}
# Print the list of threads with thier title, ask the user which thread they want, and return the result
get_thread ()
{
read -r -p "Index: " index
if [[ "$index" =~ ^(exit)|(EXIT)$ ]]
then
return 1
fi
# We check that the input is a number and it is in the correct range, else we exit.
if [[ ! "$index" =~ ^[0-9]+$ ]] || [ "$index" -lt 0 ] || [ "$index" -ge "$cnt" ] # Short circuiting
then
return 2
fi
index=${titles[$index]}
echo "${threads[$index]}"
}
# Iterate over the links found in $links
use_links ()
{
# Safety stuff I dont nuke my ram by opening alot of links.
if [ -n "$open" ]
then
tmp=( $links ) # I want splitting
n=$((${#tmp[@]} - 1))
if [ "$open" -gt "$n" ]
then
open="$n"
fi
read -r -p "[WARNING] This will open $open link(s), proceed Y/N? " input
[[ ! $input =~ ^y|Y(es)?$ ]] && exit
fi
i=0
for link in $links
do
if [ -n "$open" ]
then
[ "$i" -eq "$open" ] && exit
echo "Opening $link"
xdg-open "$link"
((i++))
elif [ -n "$apply" ]
then
setwp "$link"
else
echo "$link" # This is just for debugging.
fi
done
}
# Spawn the tui
tui ()
{
print_tui
thread=$(get_thread)
case $? in
1)
echo "Goodbye!"
exit
;;
2)
if [ "$cnt" -eq 1 ]
then
range="{0}"
else
range="[0 , $((cnt-1))]"
fi
echo "Invalid input, expecting an integer in the set $range."
exit 2
;;
esac
links=$(thread_pics "$board" "$thread")
use_links
}
# Entry point
main ()
{
# Vars
board=wg
top=3 # Fetch top 3 by default.
cnt=0
open=
apply=
thread=
while getopts "hb:t:auo:s:" opt
do
case $opt in
h)
usage
exit 0
;;
b)
board="$OPTARG"
;;
t)
top="$OPTARG"
;;
a)
top=
;;
u)
apply=1
;;
o)
open="$OPTARG"
;;
s)
thread="$OPTARG"
;;
*)
usage
exit 1
;;
esac
done
if [ -z "$thread" ]
then
# First we map thread titles to thier respective threads for later use.
res=$(board_threads "$board")
for r in $res
do
if [ -n "$top" ] && [ "$cnt" -ge "$top" ]
then
break # We have fetched the top n, hence exit
fi
res=$(thread_title "$board" "$r")
title=$(echo "$res" | cut -d ":" -f 1)
thread=$(echo "$res" | cut -d ":" -f 2)
if [ -z "$thread" ] || [ -z "$title" ]
then
continue
fi
titles+=( "$title" )
threads[$title]="$thread"
((cnt++))
done
for ((;;))
do
tui
done
else
links=$(thread_pics "$board" "$thread")
use_links
fi
}
main "$@"