# Mp3tag parsing for Amazon Web Service (v2.36 and higher)
#
# Web source created and maintained by dano
#  Last modified 2011-01-26
#
# minor additions by Florian and Dave Reid
#
# Save it in your Mp3tag data directory, i.e.
# C:\Documents and Settings\*username*\Application Data\Mp3tag\data\sources
#
#
#
[Name]=Amazon.com
[BasedOn]=http://webservices.amazon.com
[IndexUrl]=http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag=mp3tag-20&Operation=ItemSearch&SearchIndex=Music&Keywords=%s&ItemPage=1&ResponseGroup=ItemAttributes,Small,Tracks,Images
[AlbumUrl]=http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AssociateTag=mp3tag-20&Operation=ItemLookup&ItemId
[WordSeperator]=%20
[IndexFormat]=%_url%|%_preview%|%Artist%|%Publisher%|%Tracks%|%Released%|%Album%
[SearchBy]=%album%

[ParserScriptIndex]=...
# ###################################################################
# This script parses the webpage for all releases
# by this albumname.
#
# The output format is defined by IndexFormat above, which
# is used by the selection dialog
# ###################################################################
regexpreplace "<Binding>[^<]+</Binding>" ""
do
	findinline "<Item>"
	say "="
	findinline "<ASIN>"
	sayuntil "</ASIN>"
	say "&ItemPage=1&ResponseGroup=ItemAttributes,Small,Tracks,Images"
	say "|"
	
	# Preview
	findinline "<DetailPageURL>"
	sayuntil "</DetailPageURL>"
	say "|"
	
	# Artist
	findinline "<ItemAttributes>"
	if "<Artist>"
		findinline "<Artist>"
		sayuntil "</Artist>"
	else
		if "<Creator"
			findinline "<Creator"
			findinline ">"
			sayuntil "</Creator>"
		else
			if "<Author>"
				findinline "<Author>"
				sayuntil "</Author>"
			endif
		endif
	endif
	say "|"
	
	# Publisher
	sayregexp "(?<=<Label>)[^<]+(?=</Label>)" ", " "</ItemAttributes>"
	say "|"
	
	# Total tracks
	sayregexp "(?<=Track Number=\")\d+(?=\">[^<]+</Track></Disc>)" ", " "</Item>"
	say "|"
	
	# Released	
	sayregexp "(?<=<ReleaseDate>)[-0-9]{4,10}(?=</ReleaseDate>)" ", " "</ItemAttributes>"
	say "|"
	
	#Album
	findinline "<Title>"
	sayuntil "</Title>"
	
	saynewline
	findinline "</Item>"
while "<Item>"

[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
# ###################################################################
regexpreplace "<Binding>[^<]+</Binding>" ""
# CoverURL
outputto "coverurl"
findinline "<LargeImage>" 1 1
movechar -12
if "<LargeImage>"
	findinline "<URL>"
	sayuntil "</URL>"
else
	findline "<ItemAttributes>"
	findinline "<MediumImage>" 1 1
	movechar -13
	if "<MediumImage>"
		findinline "<URL>"
		sayuntil "</URL>"
	else
		findline "<ItemAttributes>"
	endif
endif

# Artist
findinline "<ItemAttributes>"
outputto "Artist"
if "<Artist>"
	findinline "<Artist>"
	sayuntil "</Artist>"
else
	if "<Creator"
		findinline "<Creator"
		findinline ">"
		sayuntil "</Creator>"
	else
		sayregexp "(?<=<Author>)[^<]+(?=</Author>)" ", " "</ItemAttributes>"
	endif
endif

# Publisher
findinline "<Label>" 1 1
movechar -7
if "<Label>"
	outputto "Publisher"
	findinline "<Label>"
	sayuntil "</Label>"
else
	findline "<ItemAttributes>"
endif

# Year
findinline "<ReleaseDate>" 1 1
movechar -13
if "<ReleaseDate>"
	outputto "Year"
	findinline "<ReleaseDate>"
	saynextnumber
else
	findline "<ItemAttributes>"
	findinline "<PublicationDate>" 1 1
	movechar -17
	if "<PublicationDate>"
		outputto "Year"
		findinline "<PublicationDate>"
		saynextnumber
	else
		findline "<ItemAttributes>"
	endif
endif

# Album
outputto "Album"
findinline "<Title>"
sayuntil "</Title>"


#
gotoline 1
findinline "<Tracks>" 1 1
movechar -8
if "<Tracks>"
    findinline "</Tracks>"
    findinline "Disc Number=\"" -1 1
    findinline "\""
endif

ifnot "1\""

# Totaldiscs
outputto "tTotalDiscs"
saynextnumber

gotoline 1

# Tracks
findinline "<Tracks>" 1 1
movechar -8
if "<Tracks>"
	findinline " Number=\""
	movechar -9
	do  		
        movechar -4
        
        if "Disc"
        
            set "tTotalTracks"
            outputto "tTotalTracks"
    		sayregexp "(?<=Track Number=\")\d+(?=\">[^<]+</Track></Di)" "" "sc><"	    
		    
            set "tmp" 
            outputto "tmp"
            findinline " Number=\""
            sayregexp "\d+" "" "\""
            
            outputto "Discnumber"
            sayregexp "\d+" "" "\""
            say "/"
            sayoutput "tTotalDiscs"
            
            findinline "<Track Number="
        else
            outputto "Discnumber"
            sayoutput "tmp"
            say "/"
            sayoutput "tTotalDiscs"
        endif
        say "|"
       
		
		outputto "Track Temp"
		sayregexp "\d{1,3}" "," ">"
		say "/"
		sayoutput "ttotaltracks"
		say "|"
		
		outputto "Title Temp"
		findinline ">"
		sayuntil "</Track>"
		say "|"
		findinline "</Track>"
		
		outputto "tracks"
		say "|"
		
		findinline " Number=\"" 1 1
		movechar -9
	while " Number=\"" 500
	set "tmp"
	set "tTotalDiscs"
	set "tTotalTracks"
endif   # Tracks Discnumber

else
# Single Disc

gotoline 1

# Tracks
findinline "<Tracks>" 1 1
movechar -8
if "<Tracks>"
    outputto "TotalTracks"
	sayregexp "(?<=Track Number=\")\d+(?=\">[^<]+</Track></Disc>)" "" "</Tracks>"
	
	do	
		findinline "<Track Number="
		
		
		outputto "Track Temp"
		sayregexp "\d{1,3}" "," ">"
		say "/"
		sayoutput "totaltracks"
		say "|"
		
		outputto "Title Temp"
		findinline ">"
		sayuntil "</Track>"
		say "|"
		
		outputto "tracks"
		say "|"
		findinline "</Track>"
		if "</Disc>"
			findinline "<Disc Numbe" 1 1
			if "r="			    
				findinline ">"
			endif
		endif
	while "<Track "
	set "TotalTracks"
endif

endif


outputto "title"
sayoutput "title temp"

outputto "track"
sayoutput "track temp"

set "title temp"
set "track temp"
