open-stream 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/bin/sh
  2. # open a stream or media copied to the clipboard or given as argument
  3. if [ $# != 1 ]; then
  4. clipboard=$(xclip -out -sel clip | sed 's/!/\\!/')
  5. echo "$clipboard"
  6. else
  7. clipboard="$1"
  8. fi
  9. # default: media is video/audio
  10. media='videoAudio'
  11. showpdf() {
  12. pdfViewers=$(printf "llpp\nmupdf\nsave\nprint\n")
  13. viewer=$(echo "$pdfViewers" | dmenu -i -l 30)
  14. case "$viewer" in
  15. save) cachedir="${XDG_CACHE_HOME:-$HOME/.cache}/norisa/stream"
  16. [ -d "$cachedir" ] || mkdir -p "$cachedir"
  17. indexlist=$(ls -1 "$cachedir" | sort -h)
  18. [ -z "$indexlist" ] && touch "$cachedir/index0.pdf" &&
  19. indexlist="index0.pdf"
  20. index=$(echo "$indexlist" | tail -n 1 | sed 's/index//; s/.pdf//')
  21. : $((index = index + 1))
  22. cp "/tmp/index.pdf" "$cachedir/$index";;
  23. print) lp "/tmp/index.pdf";;
  24. *) devour "$viewer" "/tmp/index.pdf";;
  25. esac
  26. }
  27. # check media type
  28. if echo "$clipboard" | grep -q "^magnet:?xt="; then media='torrent'; fi
  29. notify-send "opening" "$media"
  30. case "$media" in
  31. videoAudio) streamType=$(printf "stream\ndownload\nbackground\ncopy link\n" | \
  32. dmenu -i -l 30)
  33. case "$streamType" in
  34. stream) mpv "$clipboard";;
  35. download) downFormat="$(youtube-dl -F "$clipboard" | \
  36. sed '1,/format code/d' | dmenu -i -l 30 | awk '{print $1}')"
  37. if [ -z "$downFormat" ]; then
  38. youtube-dl "$clipboard" -o \
  39. "$HOME/dl/%(title)s-%(id)s.%(ext)s" &&
  40. notify-send "downloading" "finished"
  41. else
  42. youtube-dl -f "$downFormat" "$clipboard" -o \
  43. "$HOME/dl/%(title)s-%(id)s.%(ext)s" &&
  44. notify-send "downloading" "finished"
  45. fi;;
  46. background) mpv --no-video "$clipboard";;
  47. "copy link") echo "$clipboard" | xclip -sel clip;;
  48. *) exit 1;;
  49. esac
  50. ;;
  51. torrent)
  52. [ -d "/tmp/torrent-stream" ] &&
  53. notify-send "torrent" "clearing stream cache" &&
  54. rm -rf "/tmp/torrent-stream/"
  55. killall peerflix
  56. peerflix "$clipboard" -q &
  57. sleep 4
  58. while true; do
  59. find "/tmp/torrent-stream/" -type f
  60. target="$(find "/tmp/torrent-stream/" -type f | \
  61. sed '/\/$/d; /\.torrent$/d')"
  62. if [ -n "$target" ]; then
  63. notify-send "torrent" "opening"
  64. mpv "$target" && break
  65. fi
  66. sleep 1
  67. done
  68. # TODO: add downloading option + more than audioVideo (maybe recognition)
  69. # add garbage collector that kills the peerflix process when unused
  70. ;;
  71. esac