lyrics-cmus.sh 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #!/usr/bin/env bash
  2. set -euo pipefail # bash strict mode
  3. # Author: Winfried Dietmayer -==WDI==-, 12.07.2014
  4. # Original version retrieved from https://gist.github.com/febuiles/1549991/download
  5. # Precondition:
  6. # In CMUS:
  7. # ':set status_display_program=/path/to/program/cmus-artist-title.sh'
  8. # To make this permanent, add the line above to your ~/.cmus/rc
  9. #------------------------------------------------------------------------------
  10. # lyrics-cmus.sh is free software: you can redistribute it and/or modify
  11. # it under the terms of the GNU General Public License as published by
  12. # the Free Software Foundation, either version 3 of the License, or
  13. # (at your option) any later version.
  14. # lyrics-cmus.sh is distributed in the hope that it will be useful,
  15. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. # GNU General Public License for more details.
  18. # You should have received a copy of the GNU General Public License
  19. # along with lyrics-cmus.sh. If not, see <http://www.gnu.org/licenses/>.
  20. #
  21. # To summarize: This is *free* but copyrighted software,
  22. # *free* as in *free*dom or *free* will.
  23. #------------------------------------------------------------------------------
  24. # Pathes to commands - change appropriately for your system
  25. perl=/usr/bin/perl # Use the Apple version of perl
  26. curl=/opt/local/bin/curl
  27. say=/usr/bin/say # speak command a la Apple
  28. echo=/bin/echo
  29. tail=/usr/bin/tail
  30. cut=/usr/bin/cut
  31. sed=/usr/bin/sed
  32. #------------------------------------------------------------------------------
  33. # Full qualified path to the cmus status file - change as appropriate
  34. statusfile=~/cmus-status.txt
  35. # Static part of url to retrieve the lyrics
  36. urlstatic=https://makeitpersonal.co/lyrics
  37. #------------------------------------------------------------------------------
  38. # Output the arguments to stderr instead of stdin
  39. function echoerr()
  40. {
  41. echo=/bin/echo
  42. $echo "$@" 1>&2;
  43. }
  44. #-----------------------------------------------------------------------------
  45. # You name it
  46. function usage
  47. {
  48. echoerr "Usage: 'lyrics-cmus' [-v] [<artist> <title>]"
  49. echoerr " 'lyrics-cmus' retrieves the lyrics for a song from makeitpersonal.co."
  50. echoerr " If no parameter is given the artist and title"
  51. echoerr " of the current cmus song will be used."
  52. echoerr " -v: Additionally declaim artist and title using 'say' of OS X."
  53. echoerr " If <artist> and <title> is provided use this data instead of the cmus data."
  54. echoerr " -- <artist> - the name of the artist"
  55. echoerr " -- <title> - the title of the song"
  56. return 1
  57. }
  58. #------------------------------------------------------------------------------
  59. # Read artist name und song title from the cmus interface file
  60. function get-cmus-metadata
  61. {
  62. artist_name=`$tail -n 1 $statusfile | $cut -d "-" -f 1 | $sed -E -e "s/^[[:blank:]]+//" -e "s/[[:blank:]]+$//"`
  63. song_title=`$tail -n 1 $statusfile | $cut -d "-" -f 2 | $sed -E -e "s/^[[:blank:]]+//" -e "s/[[:blank:]]+$//"`
  64. }
  65. #-----------------------------------------------------------------------------
  66. # Read artist name und song title from the cmus interface file
  67. function get-shell-metadata
  68. {
  69. artist_name="$1"
  70. song_title="$2"
  71. }
  72. #-----------------------------------------------------------------------------
  73. # Escape unusual chars with perl module escape.pm
  74. function escape-chars
  75. {
  76. artist=`$perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$artist_name"`
  77. title=`$perl -MURI::Escape -e 'print uri_escape($ARGV[0]);' "$song_title"`
  78. }
  79. #-----------------------------------------------------------------------------
  80. # Audio output of artist and title
  81. # If you don't use Mac OS please adapt appropriately
  82. function speak-metadata
  83. {
  84. $say -v Alex artist $artist_name
  85. $say -v Alex title $song_title
  86. }
  87. #-----------------------------------------------------------------------------
  88. # Output of artist and title to stdout
  89. function print-metadata
  90. {
  91. $echo artist: $artist_name
  92. $echo "title :" $song_title
  93. }
  94. #-----------------------------------------------------------------------------
  95. # Retrieve the lyrics from makeitpersonal.co
  96. function get-lyrics
  97. {
  98. $curl -s "${urlstatic}?artist=$artist&title=$title"
  99. }
  100. #------------------------------------------------------------------------------
  101. #-----------------------------------------------------------------------------
  102. if [ $# == 0 ]; then
  103. get-cmus-metadata
  104. escape-chars
  105. print-metadata
  106. get-lyrics
  107. elif [ $# == 1 ] && [ "${1:0:2}" == "-v" ]; then
  108. get-cmus-metadata
  109. escape-chars
  110. speak-metadata
  111. print-metadata
  112. get-lyrics
  113. elif [ $# == 2 ]; then
  114. artist_name="${1:0:245}"
  115. song_title="${2:0:245}"
  116. escape-chars
  117. print-metadata
  118. get-lyrics
  119. elif [ $# == 3 ] && [ "$1" == "-v" ]; then
  120. artist_name="${2:0:245}"
  121. song_title="${3:0:245}"
  122. escape-chars
  123. speak-metadata
  124. print-metadata
  125. get-lyrics
  126. else
  127. usage
  128. fi
  129. #------------------------------------------------------------------------------