nyaa.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. #!/bin/sh
  2. # Assigning defaults
  3. query=""
  4. instance="https://nyaa.si"
  5. page=""
  6. max=75
  7. type="c=0_0"
  8. filter="&f=0"
  9. modifiers=0
  10. while [ $# -gt 0 ]; do
  11. case "$1" in
  12. -l|--link)
  13. modifiers=$((modifiers|1))
  14. shift
  15. ;;
  16. -r|--rss)
  17. modifiers=$((modifiers|2))
  18. shift
  19. ;;
  20. -t|--type)
  21. case "$2" in
  22. all)
  23. type="c=0_0"
  24. ;;
  25. anime)
  26. type="c=1_0"
  27. ;;
  28. anime_mv)
  29. type="c=1_1"
  30. ;;
  31. anime_english)
  32. type="c=1_2"
  33. ;;
  34. anime_other)
  35. type="c=1_3"
  36. ;;
  37. anime_raw)
  38. type="c=1_4"
  39. ;;
  40. audio)
  41. type="c=2_0"
  42. ;;
  43. lossless)
  44. type="c=2_1"
  45. ;;
  46. lossy)
  47. type="c=2_2"
  48. ;;
  49. literature)
  50. type="c=3_0"
  51. ;;
  52. literature_english)
  53. type="c=3_1"
  54. ;;
  55. literature_other)
  56. type="c=3_2"
  57. ;;
  58. literature_raw)
  59. type="c=3_3"
  60. ;;
  61. live_action)
  62. type="c=4_0"
  63. ;;
  64. live_action_english)
  65. type="c=4_1"
  66. ;;
  67. live_action_pv)
  68. type="c=4_2"
  69. ;;
  70. live_action_other)
  71. type="c=4_3"
  72. ;;
  73. live_action_raw)
  74. type="c=4_4"
  75. ;;
  76. picture)
  77. type="c=5_0"
  78. ;;
  79. graphic)
  80. type="c=5_1"
  81. ;;
  82. photo)
  83. type="c=5_2"
  84. ;;
  85. software)
  86. type="c=6_0"
  87. ;;
  88. apps)
  89. type="c=6_1"
  90. ;;
  91. games)
  92. type="c=6_2"
  93. ;;
  94. *)
  95. echo "Invalid type $2" 1>&2
  96. exit 1
  97. ;;
  98. esac
  99. shift
  100. shift
  101. ;;
  102. -*f|--filter)
  103. case "$2" in
  104. none)
  105. filter="&f=0"
  106. ;;
  107. noremakes)
  108. filter="&f=1"
  109. ;;
  110. trusted)
  111. filter="&f=2"
  112. ;;
  113. *)
  114. echo "Invalid filter $2" 1>&2
  115. exit 1
  116. ;;
  117. esac
  118. shift
  119. shift
  120. ;;
  121. -m|--max)
  122. max=$(($2 < 75 ? $2 : 75))
  123. shift
  124. shift
  125. ;;
  126. -p|--page)
  127. page="&p=$2"
  128. shift
  129. shift
  130. ;;
  131. -i|--instance)
  132. instance=$(echo "$2" | sed "s/\/^//")
  133. shift
  134. shift
  135. ;;
  136. -v|--version)
  137. echo "1.1.rc3"
  138. exit
  139. ;;
  140. -h|-?|--help)
  141. echo "nyaash
  142. Minimal nyaa query tool
  143. USAGE:
  144. nyaash [args] [query] [args]
  145. OPTIONS
  146. -l, --link output request link
  147. -r, --rss output rss page (can be combined with -l)
  148. -f, --filter select filter (none, noremakes, trusted) (def. none)
  149. -t, --type select torrent type (def. all)
  150. -m, --max maximum numbers of results (def.=max=75)
  151. -p, --page choose page
  152. -i, --instance specify instance (including protocol)
  153. -v, --version print version
  154. -h, --help print help
  155. Accepted torrent types (-t option)
  156. 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 "
  157. exit
  158. ;;
  159. *)
  160. query="&q=$1"
  161. shift
  162. ;;
  163. esac
  164. done
  165. # Generating query
  166. request=$(echo "$instance"/?"$type$filter$page$query")
  167. # Checking output modifiers
  168. case "$modifiers" in
  169. 1)
  170. echo "$request"
  171. exit
  172. ;;
  173. 2) curl -s "$request"'&page=rss'
  174. exit
  175. ;;
  176. 3)
  177. echo "$request"'&page=rss'
  178. exit
  179. ;;
  180. *)
  181. ;;
  182. esac
  183. # Querying nyaa and filtering torrent table
  184. page=$(lynx -source "$request")
  185. torrents=$(echo "$page" | grep -A 1000000000 '<tbody>' | grep -B 1000000000 '</tbody>' | sed -e '/<\/tbody>/d' -e '/<tbody>/d')
  186. # Parsing torrent table
  187. 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.*>//')
  188. magnets=$(echo "$torrents" | grep -o 'magnet:?xt=urn:btih:[0-9a-f]*')
  189. size=$(echo "$torrents" | grep -o -E '[0-9]{1,4}.[0-9]{,4} ([TGMKk]i)?B')
  190. 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]))?')
  191. type=$(echo "$torrents" | grep '<img src.*alt="' | sed -e 's/^.*alt="//' -e 's/" class="category-icon">//')
  192. extras=$(echo "$torrents" | grep -o -E '<td class="text-center">[0-9]*</td>' | sed -e 's/<td class="text-center">//' -e 's/<\/td>//')
  193. # Decoding html chars
  194. titles=$(echo "$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;')
  195. # Generating output
  196. length=$(echo "$magnets" | wc -l)
  197. length=$((length>max ? max : length))
  198. i=1
  199. while [ $length -ge $i ]; do
  200. echo "$i [$(echo "$type" | sed -n "$i"p)]: $(echo "$titles" | sed -n "$i"p)"
  201. echo "$(echo "$magnets" | sed -n "$i"p )"
  202. echo -n "size: $(echo "$size" | sed -n "$i"p ) "
  203. echo -n "date: $(echo "$date" | sed -n "$i"p ) "
  204. echo -n "seeds: $(echo "$extras" | sed -n "$((3*$i-2))"p ) "
  205. echo -n "leechs: $(echo "$extras" | sed -n "$((3*$i-1))"p ) "
  206. echo "completed: $(echo "$extras" | sed -n "$((3*$i))"p )"
  207. echo ""
  208. i=$(($i + 1))
  209. done