#################################################################################################
#                         iTunes Web Sources Script for Mp3Tag                                  #
#                                                                                               #
#                                 Update 14 Release 3                                           #
#         created by pone and improved by JonaaaBR and amine in September 2015                  #
#################################################################################################


########################################################
#          SEARCH BY DIALOG                            #
########################################################
[Name]=iTunes
[BasedOn]=http://itunes.apple.com
[IndexUrl]=http://itunes.apple.com/search?country=HK&entity=album&term=%s
[AlbumUrl]=http://itunes.apple.com/lookup?country=HK&entity=song&limit=200&id=
[WordSeperator]=+
[IndexFormat]=%_preview%|%_url%|%Artist%|%Album%|%Year%|%Tracks%|%Type%|%Copyright%|
[SearchBy]=%artist% %album%
[Encoding]=url-utf-8

########################################################
#          LIST OF SEARCH RESULTS DIALOG               #
########################################################
[ParserScriptIndex]=...
findline "wrapperType\""

do
	joinuntil "}"
              replace "\\\"" "&quot;"
	      replace "?uo=4" ""	  

	# PREVIEW
	sayregexp "(?<=\"collectionViewUrl\":\").+?(?=\")" ", " "}"
	say "|"

	# URL
	sayregexp "(?<=\"collectionId\":)\d+?(?=,)" ", " "}"
	say "&entity=song&limit=200"
	say "|"

	FindInLine "\"artistName\":\""
	SayUntil "\", \""
	Say "|"

	# ALBUM
	sayregexp "(?<=\"collectionName\":\").+?(?=\")" ", " "}"
	say "|"

              # YEAR
	sayregexp "(?<=\"releaseDate\":\")\d\d\d\d(?=.)" ", " "}"
	say "|"

	# TRACKS
	sayregexp "(?<=\"trackCount\":)\d+?(?=,)" ", " "}"
	say "|"

	# TYPE
	sayregexp "(?<=\"collectionType\":\").+?(?=\")" ", " "}"
	say "|"

	# COPYRIGHT
	sayregexp "(?<=\"copyright\":\").+?(?=\")" ", " "}"
	say "|"


	saynewline
	findline "wrapperType\"" 2 1
while "{\"wrapperType" 201

########################################################
#          ADJUST TAG INFORMATION DIALOG               #
########################################################
[ParserScriptAlbum]=...

findline "wrapperType\""
joinuntil "}"
replace "\\\"" "&quot;"
replace "?uo=4" ""

outputto "COLLECTIONTYPE"
sayregexp "(?<=\"collectionType\":\").+?(?=\")" ", " "}"

outputto "ITUNESMEDIATYPE"
Say "Normal"

outputto "COVERURL"
regexpreplace "100x100" "1200x1200"
sayregexp "(?<=\"artworkUrl100\":\").+?(?=\")" ", " "}"

OutputTo "AMGARTISTID"
sayregexp "(?<=\"amgArtistId\":)\d+(?=,)" ", " "}"

OutputTo "ALBUMARTIST"
FindLine "\"artistName\":\""
FindInLine "\"artistName\":\""
SayUntil "\", \""

OutputTo "ARTISTSORT"
FindLine "\"artistName\":\""
FindInLine "\"artistName\":\""
SayUntil "\", \""

outputto "COPYRIGHT"
sayregexp "(?<=\"copyright\":\").+?(?=\")" ", " "}"

outputto "ALBUM"
sayregexp "(?<=\"collectionName\":\").+?(?=\")" ", " "}"

outputto "ALBUMSORT"
sayregexp "(?<=\"collectionName\":\").+?(?=\")" ", " "}"

outputto "CONTENTADVISORYRATING"
sayregexp "(?<=\"contentAdvisoryRating\":\").+?(?=\")" ", " "}"
Say "|"

outputto "COUNTRY"
sayregexp "(?<=\"country\":\").+?(?=\")" ", " "}"

outputto "CURRENCY"
sayregexp "(?<=\"currency\":\").+?(?=\")" ", " "}"

outputto "COLLECTIONEXPLICITNESS"
sayregexp "(?<=\"collectionExplicitness\":\").+?(?=\")" ", " "}"

outputto "YEAR"
FindLine "\"releaseDate\":\""
FindInLine "\"releaseDate\":\""
SayUntil "\", \""
Say "|"

outputto "PUBLISHER"
regexpreplace "<<<year\d*>>>" ""
regexpreplace " +" " "
sayregexp "(?<=\"copyright\":\").+?(?=\")" ", " "}"


# TRACK LOOP
findline "wrapperType\"" 2 1
do
	joinuntil "}"
              replace "\\\"" "&quot;"
	      replace "?uo=4" ""

	outputto "TRACKS"
	sayregexp "(?<=\"trackCensoredName\":\").+?(?=\")" ", " "}"
	#sayregexp "(?<=\"trackName\":\").+?(?=\")" ", " "}"
	say "|"

	OutputTo "ARTIST"
	FindLine "\"artistName\":\""
	FindInLine "\"artistName\":\""
	SayUntil "\", \""
	Say "|"

	outputto "GENRE"
	sayregexp "(?<=\"primaryGenreName\":\").+?(?=\")" ", " "}"
	say "|"
	
	outputto "_LENGTH"
	sayregexp "(?<=\"trackTimeMillis\":)\d+?(?=\D)" ", " "}"
	say "|"
	
	OutputTo "DISCNUMBER"
	FindLine "\"discNumber\":"
	FindInLine "\"discNumber\":"
	SayUntil ", \""
	Say "/"
	FindLine "\"discCount\":"
	FindInLine "\"discCount\":"
	SayUntil ", \""
	Say "|"

	OutputTo "TRACKTIMEMILLIS"
	sayregexp "(?<=\"trackTimeMillis\":)\d.+?(?=,)" ", " "}"
	Say "|"

	OutputTo "ITUNESPURCHASEDATE"
	FindLine "\"releaseDate\":\""
	FindInLine "\"releaseDate\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "COLLECTIONID"
	FindLine "\"collectionId\":"
	FindInLine "\"collectionId\":"
	SayUntil ", \""
	Say "|"

	OutputTo "ARTISTID"
	FindLine "\"artistId\":"
	FindInLine "\"artistId\":"
	SayUntil ", \""
	Say "|"

	OutputTo "COLLECTIONARTISTID"
	sayregexp "(?<=\"collectionArtistId\":)\d+(?=,)" ", " "}"
	Say "|"

	OutputTo "TITLESORT"
	sayregexp "(?<=\"trackCensoredName\":\").+?(?=\")" ", " "}"
	Say "|"

	OutputTo "TRACKEXPLICITNESS"
	FindLine "\"trackExplicitness\":\""
	FindInLine "\"trackExplicitness\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "ITUNESADVISORY"
	regexpreplace "notExplicit" "0"
	regexpreplace "cleaned" "2"
	regexpreplace "explicit" "4"
	FindLine "\"trackExplicitness\":\""
	FindInLine "\"trackExplicitness\":\""
	SayUntil "\", \""
	Say "|"

	Outputto "ITUNESCATALOGID"
	FindLine "\"trackId\":"
	FindInLine "\"trackId\":"
	SayUntil ", \""
	Say "|"

	OutputTo "TOTALTRACKS"
	FindLine "\"trackCount\":"
	FindInLine "\"trackCount\":"
	SayUntil ", \""
	Say "|"

	OutputTo "COMPOSER"
	FindLine "\"artistName\":\""
	FindInLine "\"artistName\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "COMPOSERSORT"
	FindLine "\"artistName\":\""
	FindInLine "\"artistName\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "KIND"
	FindLine "\"kind\":\""
	FindInLine "\"kind\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "MP3TAGDISC"
	FindLine "\"discNumber\":"
	FindInLine "\"discNumber\":"
	SayUntil ", \""
	Say "/"
	FindLine "\"discCount\":"
	FindInLine "\"discCount\":"
	SayUntil ", \""
	Say "|"

	OutputTo "MP3TAGTRACK"
	FindLine "\"trackNumber\":"
	FindInLine "\"trackNumber\":"
	SayUntil ", \""
	Say "/"
	FindLine "\"trackCount\":"
	FindInLine "\"trackCount\":"
	SayUntil ", \""
	Say "|"

	OutputTo "COLLECTIONPRICE"
	sayregexp "(?<=\"collectionPrice\":)\d.+?(?=,)" ", " "}"
	Say "|"

	OutputTo "TRACKPRICE"
	sayregexp "(?<=\"trackPrice\":)\d.+?(?=,)" ", " "}"
	Say "|"

	OutputTo "RELEASEDATE"
	FindLine "\"releaseDate\":\""
	FindInLine "\"releaseDate\":\""
	SayUntil "\", \""
	Say "|"

	OutputTo "RADIOSTATIONURL"
	sayregexp "(?<=\"radioStationUrl\":\").+?(?=\")" ", " "}"
	Say "|"

	OutputTo "TRACKID"
	FindLine "\"trackId\":"
	FindInLine "\"trackId\":"
	SayUntil ", \""
	Say "|"

	findline "wrapperType\"" 2 1
while "{\"wrapperType"