From 4f0a10a9996697c6e5c70bcd381b9c3b7b1f6f5c Mon Sep 17 00:00:00 2001 From: yaemiku Date: Tue, 12 Jul 2022 02:40:59 +0200 Subject: [PATCH] basically change the whole script, for better of course :) --- README.md | 32 ++++++++++++++++++++--- nyaa | 76 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 81 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 1e97a13..79f8c8d 100644 --- a/README.md +++ b/README.md @@ -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 + diff --git a/nyaa b/nyaa index b0cdccd..1a52ea7 100755 --- a/nyaa +++ b/nyaa @@ -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 - case "${flag}" in - x) url="https://sukebei.nyaa.si";; - d) dir=${OPTARG};; - q) query=${OPTARG};; - esac +# 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 -if [ -z "$query" ]; then +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 '(.*?)" \ +| sed -E 's/]*>/\t/g; s/<\/?i[^>]*>|<\/td>//g' \ +| sed -E 's/]*>[^<]*<\/a>//g' \ +| sed -E 's/[^<]*<\/a>/\1/g' \ +| sed -E 's/<\/a>/\1/g' \ +| sed -E 's/<\/?a[^>]*>|<\/tr>//g' \ +| sed -E 's//\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! -