This repository has been archived on 2024-08-03. You can view files and clone it, but cannot push or open issues or pull requests.
nyaa/nyaa

85 lines
2.0 KiB
Bash
Executable File

#!/bin/sh
save_dir=$NYAA_DIR
if test -f $XDG_CONFIG_HOME/nyaa/config || test -f $HOME/.config/nyaa/config;
then
source "${XDG_CONFIG_HOME:-'$HOME/.config'}/nyaa/config" 2> /dev/null; # source config
fi
NYAA_DIR=$save_dir
mkdir -p "${XDG_CACHE_HOME:-'$HOME/.cache'}/nyaa"
temp="${XDG_CACHE_HOME:-'$HOME/.cache'}/nyaa"
download=0
dir=${NYAA_DIR:-"."}
url="https://nyaa.si"
extra_flags="-i"
trap "rm -rf $temp" SIGINT SIGTERM ERR EXIT
# https://gist.github.com/caruccio/836c2dda2bdfa5666c5f9b0230978f26
ARGS=()
while [ $# -gt 0 ]; do
while getopts xdp flag; do
case "${flag}" in
x) url="https://sukebei.nyaa.si";;
d) download=1;;
p) extra_flags="--playlist";;
esac
done
[ $? -eq 0 ] || exit 1
[ $OPTIND -gt $# ] && break
shift $[$OPTIND - 1]
OPTIND=1
ARGS[${#ARGS[*]}]=$1
shift
done
query=${ARGS[*]}
if test -z "$query";
then
query=$(pmenu -p "Search Torrent: ")
if test -z "$query"; then
exit 0;
fi
fi
query="$(echo $query | sed 's/ /+/g')"
curl -s "$url/?s=seeders&q=$query" > $temp/html
cat $temp/html | tr -d '\n\t' \
| sed -E 's/<thead>.*?<\/thead>//g' \
| grep -oP "<tr.*?>(.*?)</tr>" \
| sed -E 's/<td[^>]*>/\t/g; s/<\/?i[^>]*>|<\/td>//g' \
| sed -E 's/<a href="[^"]*" class="comments"[^>]*>[^<]*<\/a>//g' \
| sed -E 's/<a href="[^"]*" title="([^"]*)">[^<]*<\/a>/\1/g' \
| sed -E 's/<a href="(magnet\:[^"]*)"><\/a>/\1/g' \
| sed -E 's/<\/?a[^>]*>|<\/tr>//g' \
| sed -E 's/<tr class="([a-z]*)">/\1/g' \
> $temp/data
results=$(wc -l $temp/data | cut -d " " -f 1)
if test $results -lt 1; then
echo No results
exit 0
fi
selected=$(cat $temp/data | awk -F '\t' '{printf "[S:%4s | L:%3s] %9s %s { %s } %s \n", $7, $8, $5, $3, $2, $4 }' | pmenu )
magnet=$(echo $selected | grep -o 'magnet:.\+')
if test -z "$magnet"; then
echo Nothing selected
exit 0
fi
if test $download -eq 1;
then
mkdir -p $dir
aria2c "$magnet" -d $dir;
else
webtorrent "$extra_flags" --not-on-top --player-args="$NYAA_PLAYER_ARGS" --mpv "$magnet";
fi