dmenu_nyaa.sh 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. if [ -z "$DMENU_COMMAND" ]; then
  44. dmenu="dmenu"
  45. else
  46. dmenu="$DMENU_COMMAND"
  47. fi
  48. select=""
  49. while ! ${exitsig}; do
  50. exitsig=true
  51. while true; do
  52. selection=$(printf "search\nchoose filter\nchoose category\nchoose instance\n" | $dmenu "$@" -p "Nyaash: ")
  53. case $selection in
  54. "search")
  55. break
  56. ;;
  57. "choose filter")
  58. user_filter="$(printf "%s\n" "$filter_list" | $dmenu "$@" -p "(Nyaash) Pick filter")"
  59. if [ -n "$user_filter" ]; then
  60. category="$user_filter"
  61. fi
  62. ;;
  63. "choose category")
  64. user_category="$(printf "%s\n" "$category_list" | $dmenu "$@" -p "(Nyaash) Pick category")"
  65. if [ -n "$user_category" ]; then
  66. category="$user_category"
  67. fi
  68. ;;
  69. "choose instance")
  70. case $(printf "https://nyaa.si\nhttps://sukebei.nyaa.si\ncustom" | $dmenu "$@" -p "(Nyaash) instance: ") in
  71. "https://nyaa.si")
  72. instance="https://nyaa.si"
  73. ;;
  74. "https://sukebei.nyaa.si")
  75. instance="https://sukebei.nyaa.si"
  76. ;;
  77. "custom")
  78. custom_instance="$(echo "" | $dmenu "$@" -p "(Nyaash) Custom Instance: ")"
  79. if [ -n "$custom_instance"]; then
  80. instance="$custom_instance"
  81. fi
  82. ;;
  83. *)
  84. ;;
  85. esac
  86. ;;
  87. *)
  88. if [ -z "$selection" ]; then
  89. exit 0
  90. fi
  91. break
  92. ;;
  93. esac
  94. done
  95. if [ "$selection" = "search" ]; then
  96. query=$(echo "" | $dmenu "$@" -p "(Nyaash) Search: ")
  97. else
  98. query=$selection
  99. fi
  100. new_search=true
  101. while true; do
  102. if $new_search; then
  103. page2="-p $page"
  104. output=$(nyaash "$query" -t $category -f $filter -i "$instance" -p $page)
  105. i=0
  106. itemname=""
  107. names=$(echo "$output" | awk '!((NR+4)%5)' | sed 's/^[0-9]* //')
  108. magnets=$(echo "$output" | awk '!((NR+3)%5)')
  109. sizes=$(echo "$output" | awk '!((NR+2)%5)' | grep -Eo "[0-9]+(\.[0-9]+)? (.i)?B")
  110. lines=$(echo "$names" | wc -l)
  111. unset namesizes
  112. while [ -n "$names" ]; do
  113. name=$(printf "%s" "$names" | head -n 1);
  114. size=$(printf "%s" "$sizes" | head -n 1);
  115. if [ -z "$namesizes" ]; then
  116. namesizes="$size $name"
  117. else
  118. namesizes=$(printf "%s\n%s %s" "$namesizes" "$size" "$name")
  119. fi
  120. names=$(echo "$names" | tail -n +2)
  121. sizes=$(echo "$sizes" | tail -n +2)
  122. done
  123. else
  124. new_search=true
  125. fi
  126. if [ $lines -gt 0 ]; then
  127. if [ $page -eq 1 ] && [ $lines -eq 75 ]; then
  128. selected="$(printf "main menu\nnext page\n%s\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  129. elif [ $page -gt 1 ] && [ $lines -eq 75 ]; then
  130. selected="$(printf "main menu\nnext page\nprevious page\n%s\nprevois page\nnext page\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  131. elif [ $page -eq 1 ]; then # It's the first page, but has less than 75 items -> also the last page
  132. selected="$(printf "main menu\n%s\nmain menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  133. else # It's not the first page, but it has less than 75 items -> it's the last page
  134. selected="$(printf "main menu\nprevious page\n%s\nprevous page\n main menu" "$namesizes" | $dmenu "$@" -p "(Nyaash) Download: ")"
  135. fi
  136. elif [ $page -gt 1 ]; then
  137. selected="$(printf "main menu\nprevious page\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
  138. else
  139. selected="$(printf "no results\nmain menu\n" | $dmenu "$@" -p "(Nyaash) Download: ")"
  140. fi
  141. case "$selected" in
  142. "next page")
  143. page=$(( page + 1))
  144. ;;
  145. "previous page")
  146. page=$(( page - 1))
  147. ;;
  148. "main menu")
  149. exitsig=false
  150. break
  151. ;;
  152. "")
  153. break
  154. ;;
  155. *)
  156. selectedline=$(echo "$namesizes" | grep -nF "$selected" | grep -Eo '^[0-9]+')
  157. xdg-open $(printf "%s" "$magnets" | awk "NR==$selectedline") &
  158. choice=$(printf "yes\nno\n" | $dmenu "$@" -p "(Nyaash) Select another torrent?" )
  159. if ! echo "$choice" | grep -Eq '[Yy].*'; then
  160. break
  161. else
  162. new_search=false
  163. fi
  164. ;;
  165. esac
  166. done
  167. done