#!/usr/bin/env bash
mkdir -p "${XDG_CACHE_HOME:-'$HOME/.cache'}/nyaa"
temp="${XDG_CACHE_HOME:-'$HOME/.cache'}/nyaa"
dir=${NYAA_DIR:-"$HOME/Downloads"}
url="https://nyaa.si"
trap "rm -rf $temp" SIGINT SIGTERM ERR EXIT
while getopts axd:q: flag; do
case "${flag}" in
x) url="https://sukebei.nyaa.si";;
d) dir=${OPTARG};;
q) query=${OPTARG};;
esac
done
if [ -z "$query" ]; then
query=$(pmenu -p "Search Torrent: ")
exit 0;
fi
query="$(echo $query | sed 's/ /+/g')"
curl -s "$url/?s=seeders&q=$query" > $temp/html
grep -o '<a href="/view/.*</a>' $temp/html | sed 's/<[^>]*>//g' > $temp/titles
results=$(wc -l $temp/titles | cut -d " " -f 1)
if [ $results -lt 1 ]; then
echo No results
exit 0
grep -Po 'magnet:\?xt=urn:btih:[^"]*' $temp/html > $temp/magnets
magnet=$(paste $temp/titles $temp/magnets | pmenu | cut -f 2)
if [ -z "$magnet" ]; then
echo Nothing selected
mkdir -p $dir
if command -v webtorrent 2> /dev/null
then
webtorrent "$magnet" -o $dir
else
aria2c "$magnet" -d $dir
echo Enjoy watching!