nyaa.sh 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. #!/bin/sh
  2. # Assigning defaults
  3. query=""
  4. instance="https://nyaa.si"
  5. page="p=1"
  6. max=75
  7. type="c=0_0"
  8. filter="f=0"
  9. modifiers=0
  10. download=0
  11. while [ $# -gt 0 ]; do
  12. case "$1" in
  13. -l|--link)
  14. modifiers=$((modifiers|1))
  15. shift
  16. ;;
  17. -r|--rss)
  18. modifiers=$((modifiers|2))
  19. shift
  20. ;;
  21. -d|--download)
  22. download=1
  23. shift
  24. ;;
  25. -t|--type)
  26. case "$2" in
  27. all)
  28. type="c=0_0"
  29. ;;
  30. anime|art)
  31. type="c=1_0"
  32. ;;
  33. anime_mv|art_anime)
  34. type="c=1_1"
  35. ;;
  36. anime_english|art_doujin)
  37. type="c=1_2"
  38. ;;
  39. anime_other|art_games)
  40. type="c=1_3"
  41. ;;
  42. anime_raw|art_manga)
  43. type="c=1_4"
  44. ;;
  45. art_pictures)
  46. type="c=1_5"
  47. ;;
  48. audio|real_life)
  49. type="c=2_0"
  50. ;;
  51. lossless|real_life_pictures)
  52. type="c=2_1"
  53. ;;
  54. lossy|real_life_videos)
  55. type="c=2_2"
  56. ;;
  57. literature)
  58. type="c=3_0"
  59. ;;
  60. literature_english)
  61. type="c=3_1"
  62. ;;
  63. literature_other)
  64. type="c=3_2"
  65. ;;
  66. literature_raw)
  67. type="c=3_3"
  68. ;;
  69. live_action)
  70. type="c=4_0"
  71. ;;
  72. live_action_english)
  73. type="c=4_1"
  74. ;;
  75. live_action_pv)
  76. type="c=4_2"
  77. ;;
  78. live_action_other)
  79. type="c=4_3"
  80. ;;
  81. live_action_raw)
  82. type="c=4_4"
  83. ;;
  84. picture)
  85. type="c=5_0"
  86. ;;
  87. graphic)
  88. type="c=5_1"
  89. ;;
  90. photo)
  91. type="c=5_2"
  92. ;;
  93. software)
  94. type="c=6_0"
  95. ;;
  96. apps)
  97. type="c=6_1"
  98. ;;
  99. games)
  100. type="c=6_2"
  101. ;;
  102. *)
  103. printf "Invalid type: %s\n" "$2" 1>&2
  104. exit 1
  105. ;;
  106. esac
  107. shift
  108. shift
  109. ;;
  110. -T|--type-manual)
  111. if ! printf "%s" "$2" | grep -q -E '[0-9]+_[0-9]+'; then
  112. printf "Invalid type: %s\n" "$2"
  113. exit 1
  114. fi
  115. type="c=$2"
  116. shift
  117. shift
  118. ;;
  119. -f|--filter)
  120. case "$2" in
  121. none)
  122. filter="&f=0"
  123. ;;
  124. noremakes)
  125. filter="&f=1"
  126. ;;
  127. trusted)
  128. filter="&f=2"
  129. ;;
  130. *)
  131. printf "Invalid filter: %s\n" "$2" 1>&2
  132. exit 1
  133. ;;
  134. esac
  135. shift
  136. shift
  137. ;;
  138. -m|--max)
  139. if ! printf "%s" "$2" | grep -E -q '[0-9]+'; then
  140. printf "Max must be a number\n"
  141. exit 1
  142. fi
  143. max=$(($2 < 75 ? $2 : 75))
  144. shift
  145. shift
  146. ;;
  147. -p|--page)
  148. if ! printf "%s" "$2" | grep -E -q '[0-9]+'; then
  149. printf "Page must be a number\n"
  150. exit 1
  151. fi
  152. page="p=$2"
  153. shift
  154. shift
  155. ;;
  156. -i|--instance)
  157. instance=$(printf "%s" "$2" | sed "s/\/^//")
  158. shift
  159. shift
  160. ;;
  161. -s|--sukebei)
  162. instance="https://sukebei.nyaa.si"
  163. shift
  164. ;;
  165. -v|--version)
  166. printf "1.4.0\n"
  167. exit
  168. ;;
  169. -h|-?|--help)
  170. printf "nyaash
  171. Minimal nyaa query tool
  172. USAGE:
  173. nyaash [args] [query] [args]
  174. OPTIONS
  175. -d, --download open one of the magnets
  176. -f, --filter select filter (none, noremakes, trusted) (def. none)
  177. -i, --instance specify instance (including protocol)
  178. -l, --link output request link
  179. -m, --max maximum numbers of results (def.=max=75)
  180. -p, --page choose page
  181. -r, --rss output rss page (can be combined with -l)
  182. -s, --sukebei open one of the magnets
  183. -t, --type select torrent type (def. all)
  184. -T, --type-manual like -t but directly specify the value (ex 1_0 for anime)
  185. -h, --help print help
  186. -v, --version print version
  187. Accepted torrent types (-t option)
  188. 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, art, art_anime, art_doujin, art_games, art_games, art_pictures, real_life, real_life_pictures, real_life_videos\n"
  189. exit
  190. ;;
  191. *)
  192. query="q=$1"
  193. shift
  194. ;;
  195. esac
  196. done
  197. # Checking output modifiers
  198. webpage=""
  199. case "$modifiers" in
  200. 1)
  201. curl -G -L -s -w "\n%{url_effective}\n" --data-urlencode "$query" --data-urlencode "$type" --data-urlencode "$filter" --data-urlencode "$page" "$instance" | tail -1
  202. exit
  203. ;;
  204. 2)
  205. curl -G -L -s --data-urlencode "$query" --data-urlencode "$type" --data-urlencode "$filter" --data-urlencode "$page" "$instance" --data-urlencode "page=rss"
  206. exit
  207. ;;
  208. 3)
  209. curl -G -L -s -w "\n%{url_effective}\n" --data-urlencode "$query" --data-urlencode "$type" --data-urlencode "$filter" --data-urlencode "$page" "$instance" --data-urlencode "page=rss" | tail -1
  210. exit
  211. ;;
  212. *)
  213. webpage="$(curl -G -L -s -w "\n%{url_effective}\n" --data-urlencode "$query" --data-urlencode "$type" --data-urlencode "$filter" --data-urlencode "$page" "$instance" | tail -1)"
  214. ;;
  215. esac
  216. # Querying nyaa and filtering torrent table
  217. torrents=$(lynx -source "$webpage" | grep -A 1000000000 '<tbody>' | grep -B 1000000000 '</tbody>' | sed -e '/<\/tbody>/d' -e '/<tbody>/d')
  218. # Parsing torrent table
  219. titles=$(printf "%s" "$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.*>//')
  220. magnets=$(printf "%s" "$torrents" | grep -E "<a href" | grep -o 'magnet:?xt=urn:btih:[0-9a-f]*')
  221. size=$(printf "%s" "$torrents" | grep -o -E '<td class="text-center">[0-9a-zA-Z. ]*</td>' | grep -o -E '[0-9]{1,4}(\.[0-9]{,4})? ([TGMKk]i)?B')
  222. date=$(printf "%s" "$torrents" | grep -o -E '<td class="text-center" data-timestamp="[0-9]*">.*</td>' | 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]))?')
  223. type=$(printf "%s" "$torrents" | grep '<img src.*alt="' | sed -e 's/^.*alt="//' -e 's/" class="category-icon">//')
  224. extras=$(printf "%s" "$torrents" | grep -o -E '<td class="text-center">[0-9]*</td>' | sed -e 's/<td class="text-center">//' -e 's/<\/td>//')
  225. # Decoding html chars
  226. titles=$(printf "%s" "$titles" | sed 's/&nbsp;/ /g; s/&amp;/\&/g; s/&lt;/\</g; s/&gt;/\>/g; s/&quot;/\"/g; s/&#39;/\'"'"'/g; s/&ldquo;/\"/g; s/&rdquo;/\"/g; s/&#34;/\"/g')
  227. # Generating output
  228. if [ -n "$titles" ]; then
  229. length=$(printf "%s\n" "$magnets" | wc -l)
  230. length=$((length>max ? max : length))
  231. i=1
  232. while [ $length -ge $i ]; do
  233. printf "%s [%s]: %s\n" "$i" "$(printf "%s" "$type" | sed -n "$i"p)" "$(printf "%s" "$titles" | sed -n "$i"p)"
  234. printf "%s\n" "$(printf %s "$magnets" | sed -n "$i"p)"
  235. printf "size: %s date: %s\n" "$(printf "%s" "$size" | sed -n "$i"p)" "$(printf "%s" "$date" | sed -n "$i"p )"
  236. printf "seeds: %s leechs: %s downloads: %s\n\n" "$(printf "%s" "$extras" | sed -n "$((3*i-2))"p )" "$(printf "%s" "$extras" | sed -n "$((3*i-1))"p )" "$(printf "%s" "$extras" | sed -n "$((3*i))"p )"
  237. i=$((i+1))
  238. done
  239. # download prompt
  240. if [ $download -eq 1 ]; then
  241. selected=0
  242. while [ $selected -gt $length ] || [ $selected -le 0 ]; do
  243. printf "ID of the file to download (non-integer to exit)\n"
  244. read -r selected
  245. if ! printf "%s" "$selected" | grep -E -q "^ *[0-9]+ *$"; then
  246. selected=0
  247. break
  248. fi
  249. done
  250. if [ $selected -gt 0 ]; then
  251. xdg-open "$(printf "%s" "$magnets" | sed -n "$selected"p)" &
  252. fi
  253. fi
  254. fi