basically change the whole script, for better of course :)

This commit is contained in:
Nikola Kubiczek 2022-07-12 02:40:59 +02:00
parent 6ad0fd9c71
commit 4f0a10a999
Signed by: yaemiku
GPG Key ID: ADC039636B3E4AAB
2 changed files with 81 additions and 27 deletions

View File

@ -1,5 +1,5 @@
# nyaa
cli tool for downloading torrents from [nyaa.si](https://nyaa.si), heavily inspired by [notflix](https://github.com/Bugswriter/notflix)
cli tool for streaming and downloading torrents from [nyaa.si](https://nyaa.si), heavily inspired by [notflix](https://github.com/Bugswriter/notflix)
## Dependencies
@ -10,9 +10,30 @@ cli tool for downloading torrents from [nyaa.si](https://nyaa.si), heavily inspi
## Usage
```sh
$ nyaa
$ nyaa -q "XYZ" # Search for XYZ (otherwise show search prompt)
$ nyaa -d some/dir # Download to some/dir, defaults to $HOME/Downloads
$ nyaa # will prompt for query
$ nyaa your query here
$ nyaa fate apocrypha # an example
```
### Flags
- d) download torrent to $NYAA_DIR using aria2
- p) create a playlist of all files in the torrent
- x) query [sukebei.nyaa.si](https://sukebei.nyaa.si)
### Config
The program sources the file at $XDG_CONFIG_HOME/nyaa/config or $HOME/.config/nyaa/config
Currently, there are only 2 environmental variables available
- NYAA_DIR -> directory to which the torrents will be downloaded
- NYAA_PLAYER_ARGS -> arguments passed to mpv
Example of the config file:
```sh
NYAA_DIR=$HOME/Torrents
NYAA_PLAYER_ARGS="--slang=eng --alang=jpn"
```
## Installation
@ -21,3 +42,6 @@ $ nyaa -d some/dir # Download to some/dir, defaults to $HOME/Downloads
$ sudo curl -sL "https://gitlab.com/yaemiku/nyaa/-/raw/main/nyaa" -o /usr/local/bin/nyaa
$ sudo chmod +x /usr/local/bin/nyaa
```
To upgrade, you only need to run the first command

70
nyaa
View File

@ -1,23 +1,46 @@
#!/usr/bin/env bash
#!/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"
dir=${NYAA_DIR:-"$HOME/Downloads"}
download=0
dir=${NYAA_DIR:-"."}
url="https://nyaa.si"
extra_flags="-i"
trap "rm -rf $temp" SIGINT SIGTERM ERR EXIT
while getopts axd:q: flag; do
# 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) dir=${OPTARG};;
q) query=${OPTARG};;
d) download=1;;
p) extra_flags="--playlist";;
esac
done
[ $? -eq 0 ] || exit 1
[ $OPTIND -gt $# ] && break
if [ -z "$query" ]; then
shift $[$OPTIND - 1]
OPTIND=1
ARGS[${#ARGS[*]}]=$1
shift
done
query=${ARGS[*]}
if test -z "$query";
then
query=$(pmenu -p "Search Torrent: ")
if [ -z "$query" ]; then
if test -z "$query"; then
exit 0;
fi
fi
@ -25,30 +48,37 @@ 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
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/titles | cut -d " " -f 1)
if [ $results -lt 1 ]; then
results=$(wc -l $temp/data | cut -d " " -f 1)
if test $results -lt 1; then
echo No results
exit 0
fi
grep -Po 'magnet:\?xt=urn:btih:[^"]*' $temp/html > $temp/magnets
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:.\+')
magnet=$(paste $temp/titles $temp/magnets | pmenu | cut -f 2)
if [ -z "$magnet" ]; then
if test -z "$magnet"; then
echo Nothing selected
exit 0
fi
mkdir -p $dir
if command -v webtorrent 2> /dev/null
if test $download -eq 1;
then
webtorrent "$magnet" -o $dir
mkdir -p $dir
aria2c "$magnet" -d $dir;
else
aria2c "$magnet" -d $dir
webtorrent "$extra_flags" --not-on-top --player-args="$NYAA_PLAYER_ARGS" --mpv "$magnet";
fi
echo Enjoy watching!