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