# parsemedium: ZOMG functions for parsing media files
#   Copyright (C) 2005-2025  Clint Adams

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

parse_ogginfo_or_opusinfo() {
  local output album artist mbid title len tstamp

  output=(${(f)"$($1 $2)"})

  album=${${(M)output:#(#i)*ALBUM=*}#(#i)*ALBUM=}
  artist=${${(M)output:#(#i)*ARTIST=*}#(#i)*ARTIST=}
  mbid=${${(M)output:#(#i)*MUSICBRAINZ_TRACKID=*}#(#i)*MUSICBRAINZ_TRACKID=}
  title=${${(M)output:#(#i)*TITLE=*}#(#i)*TITLE=}
  len=${${(M)output:#*Playback length:*}#*Playback length: }

  if [[ "$len" == (#b)([0-9]##)m:([0-9.]##)s ]]
  then
    (( len = $match[1] * 60 + $match[2] + 1 ))
  else
    (( len = 0 ))
  fi

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len}")
}

parse_zomghelper() {
  local output album artist mbid title len tstamp

  output=(${(f)"$(zomghelper $1)"})

  album=${${(M)output:#(#i)ALBUM=*}#(#i)ALBUM=}
  artist=${${(M)output:#(#i)ARTIST=*}#(#i)ARTIST=}
  mbid=${${(M)output:#(#i)MUSICBRAINZ_TRACKID=*}#(#i)MUSICBRAINZ_TRACKID=}
  title=${${(M)output:#(#i)TITLE=*}#(#i)TITLE=}
  len=${${(M)output:#ZOMGSECS:*}#ZOMGSECS: }

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len}")
}

parse_eyeD3() {
  local output album artist mbid title len tstamp

  output=(${(f)"$(eyeD3 --no-color $1)"})

  album=${${${(M)output:#album: *}#album: }%%	*}
  artist=${${(M)output:#*	artist: *}#*	artist: }
  mbid=${${(M)output:#Unique File ID: \[http://musicbrainz.org\] *}#Unique File ID: \[http://musicbrainz.org\] }
  title=${${${(M)output:#title: *}#title: }%%	*}
  len=${${(M)output:#Time: *}#Time: }

  if [[ "$len" == (#b)([0-9]##):([0-9]##):([0-9]##)[^0-9]* ]]
  then
    (( len = $match[1] * 3600 + $match[2] * 60 + $match[3] + 1 ))
  elif [[ "$len" == (#b)([0-9]##):([0-9]##)[^0-9]* ]]
  then
    (( len = $match[1] * 60 + $match[2] + 1 ))
  else
    (( len = 0 ))
  fi

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len:-0}")
}

parse_mutagen() {
  local output album artist mbid title len tstamp

  output=(${(f)"$(mutagen-inspect --no-apev2 --no-flac $1)"})

  album=${${(M)output:#TALB=*}#TALB=}
  artist=${${(M)output:#TPE1=*}#TPE1=}
  mbid=${${(M)output:#UFID=http://musicbrainz.org=*}#UFID=http://musicbrainz.org=}
  title=${${(M)output:#TIT2=*}#TIT2=}
  len=${${(M)output:#- MPEG*seconds*}/- MPEG*(#b) ([0-9.]##) seconds*/$match[1]}

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len:-0}")

  [[ "${len}" -eq 0 ]] && return 1 || return 0
}

parse_mutagenflac() {
  local output album artist mbid title len tstamp

  output=(${(f)"$(mutagen-inspect --no-apev2 --no-mp3 $1)"})
  album=${${(M)output:#(#i)album=*}#(#i)album=}
  artist=${${(M)output:#(#i)artist=*}#(#i)artist=}
  mbid=${${(M)output:#(#i)musicbrainz_trackid=*}#(#i)musicbrainz_trackid=}
  title=${${(M)output:#(#i)title=*}#(#i)title=}
  len=${${(M)output:#- FLAC,*seconds*Hz*}/- FLAC,(#b) ([0-9.]##) seconds*/$match[1]}

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len}")
}

parse_mutagenape() {
  local output album artist mbid title len tstamp

  output=(${(f)"$(mutagen-inspect --no-flac --no-mp3 $1)"})
  album=${${(M)output:#(#i)Album=*}#(#i)Album=}
  artist=${${(M)output:#(#i)Artist=*}#(#i)Artist=}
  mbid=${${(M)output:#(#i)musicbrainz_trackid=*}#(#i)musicbrainz_trackid=}
  title=${${(M)output:#(#i)Title=*}#(#i)Title=}
  len=${${(M)output:#- Monkey\'s Audio*seconds*Hz*}/- Monkey\'s Audio*,(#b) ([0-9.]##) seconds*/$match[1]}

  typeset -i len
  tstamp=$(export TZ=UTC; strftime "%Y-%m-%d %H:%M:%S" ${EPOCHSECONDS})
  reply=("${artist}" "${title}" "${album}" "${mbid}" "${len}")
}
