Advertisement
Guest User

Untitled

a guest
Aug 31st, 2022
424
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # Quick bash script that allows you to grab the currently playing song from VLC/Spotify Web
  4. # Nothing special, use it, modify it or distribute it
  5. # This program uses sed make sure it's installed
  6.  
  7. # Arguments: [Source(VLC|SpotifyWeb)]
  8.  
  9. # Config vars
  10. prefix="Song: " # This is prefixed in the output file
  11. updateInterval=2 # In seconds
  12. outputFile="./nowPlaying"
  13. webBrowser="Google Chrome"
  14.  
  15. # Uncomment this line if you use FireFox
  16. #webBrowser="Mozilla Firefox"
  17.  
  18. if [ "$#" -ne 1 ]; then # Missing or too many arguments
  19. echo "Arguments: [Source(VLC|SpotifyWeb)]"
  20. echo "Spotify Web assumes you're using chrome, modify the script for FireFox etc."
  21. exit
  22. fi
  23.  
  24. # Functions
  25. xwinlength=21 # The length of this string "xwininfo: Window id: "
  26. function grabResourceID() {
  27. xwinoutput="$(xwininfo | grep 'Window id:')" # Grab the line containing the resource id from xwininfo
  28. cutoutput="${xwinoutput:xwinlength}" # Format it
  29. resourceID="${cutoutput%% *}"
  30. }
  31.  
  32. function grabXOutput() {
  33. xOutput="$(xwininfo -tree -root | grep $resourceID)" # Get all open windows and output only the one with the resource id of the chosen player
  34. }
  35.  
  36. function removeFileEndings() {
  37. # Get rid of file endings
  38. # Those are the common ones I could think of
  39. songClean="$(sed 's/\.mp3//g' <<< $songName)"
  40. songClean="$(sed 's/\.aac//g' <<< $songClean)"
  41. songClean="$(sed 's/\.wav//g' <<< $songClean)"
  42. songClean="$(sed 's/\.ogg//g' <<< $songClean)"
  43. songClean="$(sed 's/\.flac//g' <<< $songClean)"
  44. songClean="$(sed 's/\.ogg//g' <<< $songClean)"
  45. songClean="$(sed 's/\.aiff//g' <<< $songClean)"
  46. songClean="$(sed 's/\.alac//g' <<< $songClean)"
  47. }
  48.  
  49. echo "Click in the window of your chosen player..."
  50. grabResourceID
  51. echo "Resource ID is $resourceID"
  52.  
  53. # Defaults
  54. playerSuffix=" - VLC media player\"" # That '"' at the end is from xwininfo
  55. sourceType="$1" # Available: VLC | Spotify web, For Spotify web you have to have the Spotify web player tab selected!
  56.  
  57. # Leave these
  58. resIDLength=${#resourceID}
  59. totalLength=$((resIDLength + 7))
  60. playerSuffixLength=${#playerSuffix}
  61. oldOutput="NULL"
  62.  
  63.  
  64. if [ "$sourceType" == "VLC" ]; then
  65. echo "Using formatting for VLC..."
  66.  
  67. while true; do
  68. grabXOutput
  69. if [ "$xOutput" == "$oldOutput" ]; then # Song hasn't changed
  70. sleep $updateInterval
  71. continue
  72. fi
  73.  
  74. oldOutput=$xOutput
  75. cutColon="${xOutput%%:*}" # Cut everything after the colon off
  76. cutResourceId="${cutColon:totalLength}" # Cut off ResourceID
  77. songName="${cutResourceId:0:${#cutResourceId}-$playerSuffixLength}" # Cut off the Suffix eg. ' - VLC Media Player'
  78.  
  79. removeFileEndings
  80.  
  81. echo "Now Playing: $songClean"
  82. echo "$prefix$songClean">$outputFile
  83. sleep $updateInterval
  84. done
  85. else
  86. echo "Using formatting for Spotify Web..."
  87.  
  88. playerSuffix=" - Spotify - $webBrowser\""
  89. playerSuffixLength=${#playerSuffix}
  90.  
  91. while true; do
  92. grabXOutput
  93.  
  94. if [ "$xOutput" == "$oldOutput" ]; then # Song hasn't changed
  95. sleep $updateInterval
  96. continue
  97. fi
  98.  
  99. if [[ $xOutput == *"▶"* ]]; then # When Spotify is playing cut off the ▶
  100. totalLength=$((resIDLength + 9)) # Spotify prefixes a '▶' and a whitespace
  101. else
  102. totalLength=$((resIDLength + 7)) # Otherwise don't cut if off
  103. fi
  104.  
  105. oldOutput=$xOutput
  106. cutColon="${xOutput%%:*}"
  107. cutResourceId="${cutColon:totalLength}"
  108.  
  109. songName="${cutResourceId:0:${#cutResourceId}-$playerSuffixLength}"
  110.  
  111. echo "Now Playing: $songName"
  112. echo "$prefix$songName">$outputFile
  113. sleep $updateInterval
  114. done
  115. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement