dmenu_nyaa.sh 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. #!/bin/sh
  2. filter_list="none
  3. noremakes
  4. trusted"
  5. category_list="all
  6. anime
  7. anime_mv
  8. anime_english
  9. anime_other
  10. anime_raw
  11. audio
  12. lossless
  13. lossy
  14. literature
  15. literature_english
  16. literature_other
  17. literature_raw
  18. live_action
  19. live_action_english
  20. live_action_pv
  21. live_action_other
  22. live_action_raw
  23. picture
  24. graphic
  25. photo
  26. software
  27. apps
  28. games
  29. art
  30. art_anime
  31. art_doujin
  32. art_games
  33. art_manga
  34. art_pictures
  35. real_life
  36. real_life_pictures
  37. real_life_videos"
  38. filter="none"
  39. category="all"
  40. page=1
  41. instance="https://nyaa.si"
  42. exitsig=false
  43. rss_mode=false
  44. dmenu="${DMENU_COMMAND:-dmenu}"
  45. while ! ${exitsig}; do
  46. exitsig=true
  47. while true; do
  48. selection=$(printf "search\nchoose filter\nchoose category\nchoose instance\nchoose mode\n" | $dmenu "$@" -p "Nyaash: ")
  49. case $selection in
  50. "search")
  51. break
  52. ;;
  53. "choose filter")
  54. user_filter="$(printf "%s\n" "$filter_list" | $dmenu "$@" -p "(Nyaash) Pick filter")"
  55. if [ -n "$user_filter" ]; then
  56. filter="$user_filter"
  57. fi
  58. ;;
  59. "choose category")
  60. user_category="$(printf "%s\n" "$category_list" | $dmenu "$@" -p "(Nyaash) Pick category")"
  61. if [ -n "$user_category" ]; then
  62. category="$user_category"
  63. fi
  64. ;;
  65. "choose instance")
  66. case $(printf "https://nyaa.si\nhttps://sukebei.nyaa.si\ncustom" | $dmenu "$@" -p "(Nyaash) instance: ") in
  67. "https://nyaa.si")
  68. instance="https://nyaa.si"
  69. ;;
  70. "https://sukebei.nyaa.si")
  71. instance="https://sukebei.nyaa.si"
  72. ;;
  73. "custom")
  74. custom_instance="$(echo "" | $dmenu "$@" -p "(Nyaash) Custom Instance: ")"
  75. if [ -n "$custom_instance" ]; then
  76. instance="$custom_instance"
  77. fi
  78. ;;
  79. *)
  80. ;;
  81. esac
  82. ;;
  83. "choose mode")
  84. case $(printf "normal\nrss" | $dmenu "$@" -p "(Nyaash) mode:") in
  85. "normal")
  86. rss_mode=false
  87. ;;
  88. "rss")
  89. rss_mode=true
  90. ;;
  91. *)
  92. ;;
  93. esac
  94. ;;
  95. *)
  96. if [ -z "$selection" ]; then
  97. exit 0
  98. fi
  99. break
  100. ;;
  101. esac
  102. done
  103. if [ "$selection" = "search" ]; then
  104. query=$(echo "" | $dmenu "$@" -p "(Nyaash) Search: ")
  105. else
  106. query=$selection
  107. fi
  108. new_search=true
  109. while true; do
  110. if $new_search; then
  111. if $rss_mode; then
  112. output=$(nyaash "$query" -t "$category" -f "$filter" -i "$instance" -p $page -r -l)
  113. lines=1
  114. namesizes="$output"
  115. else
  116. output=$(nyaash "$query" -t "$category" -f "$filter" -i "$instance" -p $page)
  117. names=$(echo "$output" | awk '!((NR+4)%5)' | sed 's/^[0-9]* //')
  118. magnets=$(echo "$output" | awk '!((NR+3)%5)')
  119. sizes=$(echo "$output" | awk '!((NR+2)%5)' | grep -Eo "[0-9]+(\.[0-9]+)? (.i)?B")
  120. lines=$(echo "$names" | wc -l)
  121. unset namesizes
  122. while [ -n "$names" ]; do
  123. name=$(printf "%s" "$names" | head -n 1);
  124. size=$(printf "%s" "$sizes" | head -n 1);
  125. if [ -z "$namesizes" ]; then
  126. namesizes="$size $name"
  127. else
  128. namesizes=$(printf "%s\n%s %s" "$namesizes" "$size" "$name")
  129. fi
  130. names=$(echo "$names" | tail -n +2)
  131. sizes=$(echo "$sizes" | tail -n +2)
  132. done
  133. fi
  134. else
  135. new_search=true
  136. fi
  137. if [ "$lines" -gt 0 ]; then
  138. if [ $page -eq 1 ] && [ "$lines" -eq 75 ]; then
  139. selected="$(printf "main menu\nnext page\n%s\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  140. elif [ "$page" -gt 1 ] && [ "$lines" -eq 75 ]; then
  141. selected="$(printf "main menu\nnext page\nprevious page\n%s\nprevois page\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  142. elif [ $page -eq 1 ]; then # It's the first page, but has less than 75 items -> also the last page
  143. selected="$(printf "main menu\n%s\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  144. else # It's not the first page, but it has less than 75 items -> it's the last page
  145. selected="$(printf "main menu\nprevious page\n%s\nprevous page\n main menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  146. fi
  147. elif [ $page -gt 1 ]; then
  148. selected="$(printf "main menu\nprevious page\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
  149. else
  150. selected="$(printf "no results\nmain menu\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
  151. fi
  152. case "$selected" in
  153. "next page")
  154. page=$(( page + 1))
  155. ;;
  156. "previous page")
  157. page=$(( page - 1))
  158. ;;
  159. "main menu")
  160. exitsig=false
  161. break
  162. ;;
  163. "")
  164. break
  165. ;;
  166. *)
  167. if $rss_mode; then
  168. if [ "$(printf "copy\nopen" | $dmenu "$@" -p "Nyaash:")" = "copy" ]; then
  169. printf "%s" "$selected" | cb copy
  170. else
  171. xdg-open "$selected"
  172. fi
  173. else
  174. echo "$selected" | while read -r name; do
  175. selectedline=$(echo "$namesizes" | grep -nF "$name" | grep -Eo '^[0-9]+')
  176. xdg-open "$(printf "%s" "$magnets" | awk "NR==$selectedline")" &
  177. done
  178. fi
  179. choice=$(printf "yes\nno\n" | $dmenu "$@" -p "(Nyaash) Select another torrent?" )
  180. if ! echo "$choice" | grep -Eq '[Yy].*'; then
  181. break
  182. else
  183. new_search=false
  184. fi
  185. ;;
  186. esac
  187. done
  188. done