You are not logged in.

#1 2008-03-01 00:20:02

bruenig
Member
Registered: 2007-05-20
Posts: 175

mpl - lyrics for mpd

This is a bash script I put together to fetch and show lyrics for mpd. It uses the leoslyrics api to fetch and saves the lyrics in ~/.lyrics as artist-title.txt. This format is compatible with the lyrics tab in sonata as well (so that's good right?). If someone knows how to use the lyricwiki api, reply  here because that is a better site I think but I can't figure it out since they changed it.

But anyways here is the usage:

Usage: mpl [options] [arguments]

Options

  -d, --download - download lyrics (see below for examples)
  -s, --show     - show lyrics     (see below for examples)
  -h, --help     - display this

Examples

  mpl -s current
         shows lyrics of current song
  mpl -s ARTIST TITLE
         shows lyrics of specified song, not case sensitive

  mpl -d all
         download lyrics of all the songs in the mpd database
  mpl -d current
         download lyrics of current song
  mpl -d ARTIST TITLE
         download lyrics of specified song, not case sensitive

From this message, you can see quite a bit of functionality. It has the basic functionality that most lyric fetching things have such as getting the current song or a specific song, but it also lets you downloads all of the songs at once. This is useful in particular for those who don't have internet connections at all times (e.g. laptop users). The download options do not show the lyrics once they have downloaded them. However, if you choose to "show" the lyrics of a song whose lyrics haven't been downloaded, the script downloads them first then shows them.

Here is the script:

#!/bin/bash
#This script does some nifty lyric stuff with mpd
MPDDB="$HOME/.mpd/mpd.db"
TEMPFILE="/tmp/mpl.tmp"
LYRICDIR="$HOME/.lyrics"

COLOR='yes'
PAGER='cat'
SCUR='0'
SSPEC='0'
DCUR='0'
DSPEC='0'
DALL='0'


err() {
  echo "$1"
  exit 1
}

usage() {
  echo "Usage: mpl [options] [arguments]"
  echo
  echo "Options"
  echo
  echo "  -d, --download - download lyrics (see below for examples)"
  echo "  -s, --show     - show lyrics     (see below for examples)"
  echo "  -h, --help     - display this"
  echo
  echo "Examples"
  echo
  echo "  mpl -s current"
  echo "         shows lyrics of current song"
  echo "  mpl -s ARTIST TITLE"
  echo "         shows lyrics of specified song, not case sensitive" 
  echo
  echo "  mpl -d all"
  echo "         download lyrics of all the songs in the mpd database"
  echo "  mpl -d current"
  echo "         download lyrics of current song"
  echo "  mpl -d ARTIST TITLE"
  echo "         download lyrics of specified song, not case sensitive"
  exit
}

#Arg 1 - artist, Arg 2 - title
lyricfileexists() {
  filename="$(tr -d '/' <<<$1-$2).txt"
  findoutput="$(find $LYRICDIR -iname "$filename")"

  if [ -n "$findoutput" ]; then
    return 0
  else
    return 1
  fi
}

#Arg 1 - artist, Arg 2 - title
download() {
  #leoslyrics needs underscores, not spaces
  artist="$(tr ' ' '_' <<<$1)"
  title="$(tr ' ' '_' <<<$2)"
  metaurl="http://api.leoslyrics.com/api_search.php?auth=mpl&artist=$artist&songtitle=$title"


  #Download leoslyrics meta file, extract info, delete
  wget -q "$metaurl" -O- | sed 's/<\/.*$//' > "$TEMPFILE"
  realartist="$(grep '<name>' $TEMPFILE | cut -d '>' -f 2)"
  realtitle="$(grep '<title>' $TEMPFILE | cut -d '>' -f 2)"
  filename="$(tr -d '/' <<<$realartist-$realtitle).txt"
  hid="$(grep 'exactMatch="true"' $TEMPFILE | cut -d '"' -f 4)"
  rm -f "$TEMPFILE"

  #No matches, means no $hid
  if [ -z "$hid" ]; then
    return 1
  fi

  #Download, do some formatting, and write lyric file
  actualurl="http://api.leoslyrics.com/api_lyrics.php?auth=mpl&hid=$hid"
  wget -q "$actualurl" -O- | grep -e "
" -e "text>" | sed -e 's/^.*<text>//' -e 's/<\/text>//' -e 's/,
//' -e 's/
//' > "$LYRICDIR/$filename"
  if [ -z "$(<"$filename")" ]; then
    rm -f "$filename"
    return 1
  else
    return 0
  fi
}

#Parameter parsing
[ "$#" -eq "0" ] && usage

case "$1" in
    '-d'|'--download')
      case "$2" in
        all)
          DALL='1'
          ;;
        current)
          ARTIST="$(mpc --format %artist% | head -n 1)"
          TITLE="$(mpc --format %title% | head -n 1)"
          DCUR='1'
          ;;
        '')  
          err "No argument specified for option \`$1', see mpl --help."
          ;;
        *)
          ARTIST="$2"
          [ -z "$3" ] && err "No TITLE specified for option \`$1', see mpl --help."
          TITLE="$3"
          DSPEC='1'
          ;;
      esac
      ;;
    '-s'|'--show')
      case "$2" in
        current)
          ARTIST="$(mpc --format %artist% | head -n 1)"
          TITLE="$(mpc --format %title% | head -n 1)"
          SCUR='1'
          ;;
        '')  
          err "No argument specified for option \`$1', see mpl --help."
          ;;
        *)
          ARTIST="$2"
          [ -z "$3" ] && err "No TITLE specified for option \`$1', see mpl --help."
          TITLE="$3"
          SSPEC='1'
          ;;
      esac
      ;;
    '-h'|'--help')
      usage
      ;;
    *)
      err "Option \`$1' is not recognized, see mpl --help."
      ;;
esac

#Configuration goodness
[ -d "$LYRICDIR" ] || mkdir -p "$LYRICDIR"

if [ "$COLOR" = "yes" ]; then
  FAILCOLOR="\033[1;31m"
  NORMAL="\033[1;0m"
fi

#Download all
if [ "$DALL" -eq "1" ]; then
  [ -f "$MPDDB" ] || err "No database found at $MPDDB."

  #Fill array from mpd db
  SAVEIFS="$IFS"
  IFS=$'\n'
  artists=( $(grep '^Artist: ' "$MPDDB" | cut -d : -f 2 | sed 's/^ //') )
  titles=( $(grep '^Title: ' "$MPDDB" | cut -d : -f 2 | sed 's/^ //') )
  TOTAL="${#artists[@]}"
  IFS="$SAVEIFS"
  
  for ((i=0; i<$TOTAL ; i++)); do
    if ! lyricfileexists "${artists[$i]}" "${titles[$i]}"; then
      echo -n "${artists[$i]} - ${titles[$i]}..."
      download "${artists[$i]}" "${titles[$i]}"
      if [ "$?" -eq "0" ]; then
        echo "success."
      else
        echo -e "${FAILCOLOR}failure.${NORMAL}"
      fi 
    fi
  done
  exit
fi

#Download current or specific
if [ "$DCUR" -eq "1" -o "$DSPEC" -eq "1" ]; then
  if lyricfileexists "$ARTIST" "$TITLE"; then
    echo "Lyric file for \`$ARTIST - $TITLE' already exists."
  else
    echo -n "$ARTIST - $TITLE..."
    download "$ARTIST" "$TITLE"
    if [ "$?" -eq "0" ]; then
      echo "success."
    else
      echo -e "${FAILCOLOR}failure.${NORMAL}"
    fi 
  fi
  exit
fi

#Show current or specific
if [ "$SCUR" -eq "1" -o "$SSPEC" -eq "1" ]; then
  if ! lyricfileexists "$ARTIST" "$TITLE"; then
    download "$ARTIST" "$TITLE"
    [ "$?" -ne "0" ] && err "Could not fetch lyrics for \`$ARTIST - $TITLE'"
  fi

  filename="$(tr -d '/' <<<$ARTIST-$TITLE).txt"
  echo
  find $LYRICDIR -iname "$filename" -exec $PAGER {} \;
  echo
  exit
fi

Few things to note, at the top of the script, there are two variables, COLOR and PAGER that you might want to change. COLOR is obvious, pager would be something like less or more or whatever. I have it set as cat by default. But basically when you show lyrics, the script does $PAGER lyricfile, so whatever you want where it says pager goes there.

Oh and you need mpc to do the show/download current stuff by the way.

Also, I wrote this pretty quickly, like an hour or so, so it is almost certain to have some bugs. Please report anything you see or anything you might want to see.

Last edited by bruenig (2008-03-01 16:20:01)

Offline

#2 2008-04-12 03:04:15

zenix
Member
From: Earth - Save it!
Registered: 2007-08-05
Posts: 104
Website

Re: mpl - lyrics for mpd

rawr, I'm reviving a 'dead' topic. How can such an incredibly awesome app like this go overlooked by the Arch community?

Anyways, when mpl doesn't find lyrics, I get an error from line 82 about Artist-Title.txt not found.

PS: I just decided to run it again, and it was found....how odd. A new song started playing, lyrics weren't found (as it is a musical, no speech at all, so this was expected). Huh, it works now, wonderfully. How hard would it be to add the lyrics as one of the mp3/flac/ogg tags?


I made an AUR helper once.
I also go by evaryont and nogweii elsewhere on the internet.
Check out my projects and packages.

Offline

#3 2008-04-12 16:16:36

bruenig
Member
Registered: 2007-05-20
Posts: 175

Re: mpl - lyrics for mpd

This is fail, don't use it.

Offline

Board footer

Powered by FluxBB