# ###################################################################
# Mp3tag (v2.29+) parsing for discogs.com, created by dano on 2005-05-23
# Search for ALBUMS only, Various Artists also supported
# This file should be in your sources directory
# On Windows XP it's 
# C:\Documents and Settings\*username*\Application Data\Mp3tag\data\sources
# 
# Modifications:
#   2005-09-01: updated coverurl fetching 
#   2005-09-18: updated
#   2005-09-19: updated
#   2005-10-11: updated Genre, Title and VA
#
# ###################################################################

[Name]=Discogs
[BasedOn]=http://www.discogs.com
[IndexUrl]=http://www.discogs.com/search?type=releases&q="%s"&btn=Go
[AlbumUrl]=http://www.discogs.com
[WordSeperator]=+
[IndexFormat]=%_url%|%artist%|%album%|%type%|%year%
[SearchBy]=%album%

[ParserScriptIndex]=...

# ###################################################################
# This script parses the artist's page for all releases
# by this artist.
#
# The output format is defined by IndexFormat above, which
# is used by the selection dialog
# ###################################################################

# Uncomment the following line if you want to write some debug output
debug "on" "c:\\debug_discogs_albums_A.html"

findline "<title>"
findinline "<title>"

if "Search<" # multiple results
    findline "<p>Search results for: <b>"
    findline "<ol start=1>"
    do
        findline "<a href=\""
        killtag "b"
        killtag "/b"
        # URL
        findinline "<a href=\""
        sayuntil "\">"
        say "|"
        # ARTIST
        findinline ";\">"
        sayuntil " - "
        say "|"
        # ALBUM
        movechar 2
        sayuntil "</span>"
        say "|"
        moveline 1
        
        killtag "br"
        # FORMAT
        findinline "Format: " 1 1
        movechar -2
        if ": "
        	movechar 2
            sayuntil " "
        endif
        say "|"
        # YEAR
        findinline "Released:" 1 1
        movechar -2
        if "d:"
        	movechar 2
            sayuntil " "
        endif
        saynewline
        moveline 4
    while "  <li>"
exit

else # only 1 hit
    findline "<tr><td class=ar><a href=\"/list/collection?release_id="
    findinline "release_id="
    say "/release/"
    sayuntil "\""
    say "|"
    say "|"
    say "|"
    say "|"
endif

[ParserScriptAlbum]=...

# ###################################################################
# This script parses the albums page for all common information.
#
# The current output variable is set via the outputto command
# All these fields are used by the confirm online information dialog
# ###################################################################

# Uncomment the following line if you want to write some debug output
#debug "on" "c:\\debug_discogs_albums_album.txt"

findline "<title>"
findinline "<title>"

if "Various " 
    #################################################################  
    # VA-mode

    # album
    outputto "album"
    findline "- "
    findinline "- "
    sayuntil "<"

    # coverurl
    outputto "coverurl"
    findline "<td valign=top"
    findinline "<td valign=top"
    if " align=center>"
        findinline "<img src=\""
        sayuntil "-150-"  
        movechar 4
        sayuntil "\""  
    endif

    # year
    outputto "year"
    findline "Released:"
    findinline "<td>"
    # This trick makes sure, that we get the year for both "27 Feb 2004" and "2004" formats
    findinline "<"
    movechar -5
    sayuntil "<"

	# Genre (only 1)
	outputto "genre"
	findline "Genre:"
	joinuntil "</tr>"
	killtag "*"
	regexpreplace "\s+" " "
	regexpreplace "\s+$" ""
	regexpreplace ",\s\w+$" ""
	findinline "Genre:"
	sayrest

    # Styles
    outputto "styles"
    findline "Style:"
    joinuntil "</tr>"
    killtag "*"
    regexpreplace "\s+" " "
    findinline "Style:"
    sayrest

    # Discnumber & Artist & Tracks
    findline "Tracklisting:"
    findline "<td nowrap>"
    do
	# Discnumber
	outputto "discnumber"
	killtag "*"
	sayuntil "-"
	say "|"
	# Track No
	outputto "track"
	movechar 1
	sayrest
	say "|"
        moveline 6
        # Artist
        outputto "Artist"
        joinuntil "</td>"
        killtag "*"
        regexpreplace "\s{2,}" " "
        sayrest
        say "|"
        findline "<td>"
        # Title
        outputto "Tracks"
        unspace
        killtag "*"
        regexpreplace "\s\(\d?\d:\d\d\)" ""
		sayuntil "</td>"
        say "|"
        findline "<td nowrap>" 1 1
    while "    <td nowrap>"
    # End of VA
    # ###############################################################

else

    # Artist
    outputto "artist"
    joinuntil "</title>"
    regexpreplace "\s{2,}" " "
    findinline "<title>"
    sayuntil " - "

    # Album
    outputto "album"
    findline "- "
    findinline "- "
    sayuntil "<" 

    # Cover url
    outputto "coverurl"
    findline "<td valign=top"
    findinline "<td valign=top"
    if " align=center>"
        findinline "<img src=\""
        sayuntil "-150-"
        movechar 4
        sayuntil "\""  
    endif

    # Year
    outputto "year"
    findline "Released:"
    findinline "<td>"
    # This trick makes sure, that we get the year for both "27 Feb 2004" 
    # and "2004" formats
    findinline "<"
    movechar -5
    sayuntil "<"

	# Genre (only 1)
	outputto "genre"
	findline "Genre:"
	joinuntil "</tr>"
	killtag "*"
	regexpreplace "\s+" " "
	regexpreplace "\s+$" ""
	regexpreplace ",\s\w+$" ""
	findinline "Genre:"
	sayrest

    # Styles (all)
    outputto "styles"
    findline "Style:"
    joinuntil "</tr>"
    killtag "*"
    regexpreplace "\s+" " "
    findinline "Style:"
    sayrest

    # Tracks
    findline "Tracklisting:"
    findline "<td nowrap>"
    do 
	# Discnumber
	outputto "discnumber"
	killtag "*"
	sayuntil "-"
	say "|"
        moveline 6
   	outputto "tracks"
        # Title
        unspace
        if "<td></td>" # OMG an emtpy table row WTF!!
        	exit
        else
        	killtag "*"
        	regexpreplace "\s\(\d?\d:\d\d\)" ""
			sayuntil "</td>"
        	say "|"
        	findline "<td nowrap>" 1 1
        endif
    while "    <td nowrap>"
endif
