123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- #!/bin/sh
- # Assigning defaults
- query=""
- instance="https://nyaa.si"
- page=""
- max=75
- type="c=0_0"
- filter="&f=0"
- modifiers=0
- while [ $# -gt 0 ]; do
- case "$1" in
- -l|--link)
- modifiers=$((modifiers|1))
- shift
- ;;
- -r|--rss)
- modifiers=$((modifiers|2))
- shift
- ;;
- -t|--type)
- case "$2" in
- all)
- type="c=0_0"
- ;;
- anime)
- type="c=1_0"
- ;;
- anime_mv)
- type="c=1_1"
- ;;
- anime_english)
- type="c=1_2"
- ;;
- anime_other)
- type="c=1_3"
- ;;
- anime_raw)
- type="c=1_4"
- ;;
- audio)
- type="c=2_0"
- ;;
- lossless)
- type="c=2_1"
- ;;
- lossy)
- type="c=2_2"
- ;;
- literature)
- type="c=3_0"
- ;;
- literature_english)
- type="c=3_1"
- ;;
- literature_other)
- type="c=3_2"
- ;;
- literature_raw)
- type="c=3_3"
- ;;
- live_action)
- type="c=4_0"
- ;;
- live_action_english)
- type="c=4_1"
- ;;
- live_action_pv)
- type="c=4_2"
- ;;
- live_action_other)
- type="c=4_3"
- ;;
- live_action_raw)
- type="c=4_4"
- ;;
- picture)
- type="c=5_0"
- ;;
- graphic)
- type="c=5_1"
- ;;
- photo)
- type="c=5_2"
- ;;
- software)
- type="c=6_0"
- ;;
- apps)
- type="c=6_1"
- ;;
- games)
- type="c=6_2"
- ;;
- *)
- echo "Invalid type $2" 1>&2
- exit 1
- ;;
- esac
- shift
- shift
- ;;
- -*f|--filter)
- case "$2" in
- none)
- filter="&f=0"
- ;;
- noremakes)
- filter="&f=1"
- ;;
- trusted)
- filter="&f=2"
- ;;
- *)
- echo "Invalid filter $2" 1>&2
- exit 1
- ;;
- esac
- shift
- shift
- ;;
- -m|--max)
- max=$(($2 < 75 ? $2 : 75))
- shift
- shift
- ;;
- -p|--page)
- page="&p=$2"
- shift
- shift
- ;;
- -i|--instance)
- instance=$(echo "$2" | sed "s/\/^//")
- shift
- shift
- ;;
- -v|--version)
- echo "1.1.rc3"
- exit
- ;;
- -h|-?|--help)
- echo "nyaash
- Minimal nyaa query tool
- USAGE:
- nyaash [args] [query] [args]
- OPTIONS
- -l, --link output request link
- -r, --rss output rss page (can be combined with -l)
- -f, --filter select filter (none, noremakes, trusted) (def. none)
- -t, --type select torrent type (def. all)
- -m, --max maximum numbers of results (def.=max=75)
- -p, --page choose page
- -i, --instance specify instance (including protocol)
- -v, --version print version
- -h, --help print help
- Accepted torrent types (-t option)
- all, anime, anime_mv, anime_english anime_other anime_raw, audio, lossless, lossy, literature, literature_english, literature_other, literature_raw, live_action, live_action_english, live_action_pv, live_action_other, live_action_raw, picture, graphic, photo, software, apps, games "
- exit
- ;;
- *)
- query="&q=$1"
- shift
- ;;
- esac
- done
- # Generating query
- request=$(echo "$instance"/?"$type$filter$page$query")
- # Checking output modifiers
- case "$modifiers" in
- 1)
- echo "$request"
- exit
- ;;
- 2) curl -s "$request"'&page=rss'
- exit
- ;;
- 3)
- echo "$request"'&page=rss'
- exit
- ;;
- *)
- ;;
- esac
- # Querying nyaa and filtering torrent table
- page=$(lynx -source "$request")
- torrents=$(echo "$page" | grep -A 1000000000 '<tbody>' | grep -B 1000000000 '</tbody>' | sed -e '/<\/tbody>/d' -e '/<tbody>/d')
- # Parsing torrent table
- titles=$(echo "$torrents" | sed '/class="comments"/{N;d}' | grep -A 1 '<td colspan="2">' | sed -e '/<td colspan="2">/d' -e '/^--/d' -e 's/<\/a>//g' -e 's/^.*<a.*>//')
- magnets=$(echo "$torrents" | grep -o 'magnet:?xt=urn:btih:[0-9a-f]*')
- size=$(echo "$torrents" | grep -o -E '[0-9]{1,4}.[0-9]{,4} ([TGMKk]i)?B')
- date=$(echo "$torrents" | grep -o -E '[0-9]{4,4}-[0-9]{2,2}-[0-9]{2,2} [0-9]{2,2}:[0-9]{2,2}( ?([APap][mM]))?')
- type=$(echo "$torrents" | grep '<img src.*alt="' | sed -e 's/^.*alt="//' -e 's/" class="category-icon">//')
- extras=$(echo "$torrents" | grep -o -E '<td class="text-center">[0-9]*</td>' | sed -e 's/<td class="text-center">//' -e 's/<\/td>//')
- # Decoding html chars
- titles=$(echo "$titles" | sed 's/ / /g; s/&/\&/g; s/</\</g; s/>/\>/g; s/"/\"/g; s/'/\'"'"'/g; s/“/\"/g; s/”/\"/g;')
- # Generating output
- length=$(echo "$magnets" | wc -l)
- length=$((length>max ? max : length))
- i=1
- while [ $length -ge $i ]; do
- echo "$i [$(echo "$type" | sed -n "$i"p)]: $(echo "$titles" | sed -n "$i"p)"
- echo "$(echo "$magnets" | sed -n "$i"p )"
- echo -n "size: $(echo "$size" | sed -n "$i"p ) "
- echo -n "date: $(echo "$date" | sed -n "$i"p ) "
- echo -n "seeds: $(echo "$extras" | sed -n "$((3*$i-2))"p ) "
- echo -n "leechs: $(echo "$extras" | sed -n "$((3*$i-1))"p ) "
- echo "completed: $(echo "$extras" | sed -n "$((3*$i))"p )"
- echo ""
- i=$(($i + 1))
- done
|