#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
#         .:: beatport.com API by stevehero v3.0 [10.05.2013] ::.         #
#         .:: Please read the 'Version History + Readme' file ::.         #
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
[Name]=beatport.com (v3.0 by stevehero)
[BasedOn]=http://api.beatport.com
[IndexUrl]=http://api.beatport.com/catalog/search?v=2.0&perPage=50&format=xml&query=%s&facets[]=fieldType:release
[AlbumUrl]=http://api.beatport.com/catalog/3/releases/detail?v=1.0&format=xml&id=
[WordSeperator]=+
[IndexFormat]=%_url%|%_preview%|%Cover [px]%|%Artist/s%|%Release%|%Released%|%Label%|%Catalog%|%Description%
[SearchBy]=[ release for RELEASE ]$trim($regexp($regexp($regexp(%album%,'\b-\b|_', ),'(?i)\[.*\]|cd\s*\d*|[^\s]+.com|\bep\b|feat\.|ft\.|\bom\b|\boriginal\b|\bclub\b|\bmix\b|\bremix\b|\bthe\b|vinyl|web|www[^\s]+|[^\w\s]',),'\s+', ))
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
[ParserScriptIndex]=...
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
# debug "on" "C:\debug index beatport.txt"                  #"# PLACE BEFORE HEADINGS TO WANT TO DEBUG
# debug "off" "C:\debug index beatport.txt"                 #"# PLACE AFTER HEADINGS TO WANT TO DEBUG (OPTIONAL)
replace "|" "/"
findline "<document"
do
    joinuntil "</document>"
    regexpreplace ">\s+<" "><"
#"# URL
    findinline "<release"
    findinline " id=\""
   sayuntil "\">"
    say "|"
#"# PREVIEW
    gotochar 1
    say "http://www.beatport.com/release/"
    findinline "<urlName>"
    sayuntil "</"
    say "/"
    gotochar 1
    findinline "<release"
    findinline " id=\""
    sayuntil "\">"
    say "|"
#"# COVER
    gotochar 1
    findinline "width=\"500\" height=\"500\" ref=\"release\"" 1 1
    movechar -38
    if "width=\"500\" height=\"500\" ref=\"release\""
    say "500 x 500"
    else
    findinline "width=\"60\" height=\"60\" ref=\"release\"" 1 1
    say "60 x 60"
    endif
    say "|"
#"# ARTIST, ARTISTS
    gotochar 1
    findinline "<performer ref=\"Artist\""
    findinline "<name>"
    sayuntil "</"
    findinline "</performer>"
    if "<performer ref=\"Artist\""
    say ", "
    findinline "<name>"
    sayuntil "</"
    findinline "</performer>"
    if "<performer ref=\"Artist\""
    say ", and more..."
    endif
    endif
    say "|"
#"# RELEASE
    gotochar 1
    findinline "<name>"
    sayuntil "</"
    say "|"
#"# RELEASED
    gotochar 1
    findinline "<dateReleased>"
    sayuntil "</"
    say "|"
#"# LABEL
    gotochar 1
    findinline "<label"
    findinline "<name>"
    sayuntil "</"
    say "|"
#"# CATALOG
    gotochar 1
    findinline "<catalogNumber>"
    sayuntil "</"
    say "|"
#"# DESCRIPTION
    gotochar 1
    findinline "<description>"
    regexpreplace "(.+)<description>(.*)</description>(.+)" "$2"
    replace "&amp;" "&"
    replace "&#13;" "  "
    regexpreplace "(,|\?|\)|]|!|,)\b" "$1 "       #"# FIXES PUNCTUATION SPACES
    regexpreplace "\s+(,|\.|\?|!|,)" "$1"         #"# FIXES PUNCTUATION SPACES
    regexpreplace "^\w{1,3}$" ""                  #"# REMOVES DESCRIPTION WITH ONLY 1-3 LETTERS
    regexpreplace "\.(?-i)([A-Z])" ". $1"         #"# FIXES 'Hits.Any' TO 'Hits. Any'
    regexpreplace "^\s*$" "-"                     #"# PLACES '-' IF NO DESCRIPTION AVAILABLE
    regexpreplace "\s+" " "
    unspace
    sayrest
#"# END PARSING
    moveline 1
    saynewline
    unspace
while "<document"
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
[ParserScriptAlbum]=...
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
# debug "on" "C:\debug album beatport.txt"        #"# Place before headings to want to debug
# debug "off" "C:\debug album beatport.txt"       #"# Place after headings to want to debug (OPTIONAL)
#"# MAIN SECTION - JSON REPLACMENTS
#"# Replace special JSON characters
replace "\\\\" "\\u005c"
replace "\\\"" "\\u0022"    #"# = "&quot;"
replace "\\b" "\\u0008"     #"# backspace
replace "\\f" "\\u000c"     #"# formfeed
replace "\\n" "\\u000a"     #"# newline
replace "\\r" "\\u000d"     #"# carriage return
replace "\\t" "\\u0009"     #"# horizontal tab
replace "&#9839;" "#"
replace "_" " "

#"# Spring clean
regexpreplace "(.*),\"recommendations.*" "$1<<ENDPARSING>>"                                                                     #"# 01. After 'recommendations'
regexpreplace "^\{.+?release-(\d+).+?name\":\"([^\"]+).+?slug\":\"([^\"]+).+?releaseDate\":\"([^\"]+).+?publishDate\":\"([^\"]+).+?category\":\"([^\"]+).+?description\"*:*\"*(\\\"*|\s*|.+?|\\\"*)\"*,*\"*current.+?catalogNumber\":\"([^\"]+).+?id\":(\d+),\"name\":\"([^\"]+).+?slug\":\"([^\"]+)\"\}" "<<STARTPARSING>>REALID:$1>RELEASE:$2>REALURL:$3>YEAR:$4>DATE:$5>CATEGORY:$6>DESCRIPTION:$7>CAT#:$8>LABELID:$9>LABEL:$10>LABELURL:$11><<ALBUMEND>>"     #"# 02. ALBUM
regexpreplace ",\"artists\":([^\]]+\]).+?,\"tracks\":.+?track-" "<<albumArtist>>$1<<albumArtistEnd>>track-"                     #"# 03. ABLUM ARTIST
regexpreplace "name\":\"(?=.+<<albumArtistEnd>>)" "albumArtistName\":\""                                                        #"# 03a. ABLUM ARTIST fix
regexpreplace "charts[^]]+\],\"" ""                                                                                             #"# 04. Chart
regexpreplace "sampleUrl.+?length" "length"                                                                                     #"# 05. Sample URLs
replace "\/" "/"                                                                                                                #"# 06. URLS
regexpreplace "(.+)(500)(,\"url.+)" "$1<<$2>>$3"                                                                                #"# 07. Artwork
regexpreplace "(.+)(60)(,\"url.+)" "$1<<$2>>$3"                                                                                 #"# 08. Artwork
regexpreplace "images.+?width\":60.+?sku\":\"" ""                                                                               #"# 09. Images
regexpreplace ","images\".+?medium\":(\{.+?\.jpg).+?large\":(\{.*?\.jpg).+?\{h\}.+?\.png\"\}\}" "$1$2"                          #"# 10. Images
regexpreplace ",\"genres\":\[.+?name\":\"([^\"]+)[^]]+\]" "trackGenre>>$1>"                                                     #"# 11. Genres
regexpreplace "track-(.+?)\",\"isrc\":\"([^\"]+).+?length\":\"([^\"]+).+?bpm\":([^\,]+).+?shortName\":\"([^\"]+)(.+?status[^\}]+\})" "<<nextTrack>>$1>ISRC:$2>LENGTH:$3>BPM:$4>KEY:$5>$6<<endTrack>>"                                                                #"# 12. Track markers
regexpreplace "(,\")saleType.+?(name\":\")" "trackArtists>>$1$2"                                                                #"# 13. Artist track
regexpreplace "\",\"status\":true" ""                                                                                           #"# 14. Status
regexpreplace ",\"name(\":\"[^\"]+\",\"slug.{1,50}artist\")" ",\"artistName$1"                                                  #"# 15. Artist
regexpreplace ",\"name(\":\"[^\"]+\",\"slug.{1,50}remixer\")" ",\"remixername$1"                                                #"# 16. Remixer
regexpreplace ",\"albumArtistName(\":\"[^\"]+\",\"slug.{1,50}remixer\")" ",\"albumArtistRemixer$1"                              #"# 17. Remixer (Album Artist)
replace "<<endTrack>>,\"<<nextTrack>>" "<<endTrack>><<nextTrack>>"                                                              #"# 18. Track loop fix
regexpreplace "(<<nextTrack>>)([^\"]+)(.+?slug\":\")([^\>]+)(.+?)(<<endTrack>>)" "$1$2$3$4$5>BPTrackURL>$4/$2$6"                #"# 19. Create track URL

#"# Totaltrack/Track
regexpreplace "(trackNumber\"):(\d)," "$1:0$2,"                                                             #"# ADD LEADING ZEROS
regexpreplace "(.+trackNumber\":)(\d+),\"(.+?)" "$1$2,\"<<totaltracks>>$3"                                  #"# GET TOTALTRACKS

#"# Genre
regexpreplace "trackGenre>>(Deep|Electro|Minimal|Progressive|Tech)\s?(House)?.{0}>" "trackGenre>>House $1>" #"# 'Progressive House' to 'House Progressive'
replace "trackGenre>>House Progressive>" "trackGenre>>House Prog>"                                          #"# 'Progressive House' to 'House Prog'

#"# Title
replace " (Original Mix)" " (OM)"

#"# BPM
replace ">BPM:0" ">BPM:-"

#"# Year
#regexpreplace "(>YEAR:)(\d\d\d\d)-(\d\d)-(\d\d)" "$1$2-$4-$3"      #"# YEAR in YYYY-DD-MM format
#regexpreplace "(>YEAR:)(\d\d\d\d)-(\d\d)-(\d\d)" "$1$4-$3-$2"      #"# YEAR in DD-MM-YYYY format
#regexpreplace "(>YEAR:)(\d\d\d\d)-(\d\d)-(\d\d)" "$1$2"            #"# YEAR in YYYY format

#"# Date
regexpreplace "(>DATE:)(\d\d\d\d)-(\d\d)-(\d\d)" "$1$3$4"           #"# DATE in MMDD format
#regexpreplace "(>DATE:)(\d\d\d\d)-(\d\d)-(\d\d)" "$1$4$3"          #"# DATE in DDMM format

#"# Initial key to camelot notation. PLEASE DO NOT change values or you can delete these replaces to get 'Dmin' etc.
    replace "A♭min" "01A"
    replace "G#min" "01A" #"# SAME KEY #
    replace "E♭min" "02A"
    replace "D#min" "02A" #"# SAME KEY #
    replace "B♭min" "03A"
    replace "A#min" "03A" #"# SAME KEY #
    replace "Fmin" "04A"
    replace "Cmin" "05A"
    replace "Gmin" "06A"
    replace "Dmin" "07A"
    replace "Amin" "08A"
    replace "Emin" "09A"
    replace "Bmin" "10A"
    replace "F#min" "11A"
    replace "D♭min" "12A"
    replace "C#min" "12A" #"# SAME KEY #
    replace "Bmaj" "01B"
    replace "F#maj" "02B"
    replace "D♭maj" "03B"
    replace "C#maj" "03B" #"# SAME KEY #
    replace "A♭maj" "04B"
    replace "G#maj" "04B" #"# SAME KEY #
    replace "E♭maj" "05B"
    replace "D#maj" "05B" #"# SAME KEY #
    replace "B♭maj" "06B"
    replace "A#maj" "06B" #"# SAME KEY #
    replace "Fmaj" "07B"
    replace "Cmaj" "08B"
    replace "Gmaj" "09B"
    replace "Dmaj" "10B"
    replace "Amaj" "11B"
    replace "Emaj" "12B"

#"# Replace syntax characters in non-syntax position
#"# regexpreplace ":(?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u003a"
#"# regexpreplace ",(?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u002c"
#"# regexpreplace "\[(?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u005b"
#"# regexpreplace "\](?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u005d"
#"# regexpreplace "\{(?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u007b"
#"# regexpreplace "\}(?=[^\"]*\"[\s,\[\]\{\}]+\"[^\"]+\":)" "\\\\u007d"

#"# Replace problematic characters for Mp3tag
#replace "|" "ǀ"
#replace "|" "\\u007c"                           #"# Not working anymore
replace "|" "$verticalBar()"                     #"# Does not work in UNSYNCEDLYRICS field
replace "\\u005c\\u005c" "\\u005c\\u200b\\u005c\\u200b"
replace "\"\"" "\" \""
regexpreplace "\s\s+" " "
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
 debug "on" "C:\debug album beatport.txt"        #"# Place before headings to want to debug
# debug "off" "C:\debug album beatport.txt"       #"# Place after headings to want to debug (OPTIONAL)
#:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::#
#"# TRACK LOOP
findinline "<<albumArtistEnd>>"
do
findinline "<<nextTrack>>"
    outputto "Title" #"# TITLE
    sayregexp "(?<=title\":\")[^\"]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "Artist" #"#  ARTIST
    sayregexp "(?<=,\"artistName\":\")[^\"]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "Mixartist" #"#  MIXARTIST
    sayregexp "(?<=,\"remixername\":\")[^\"]+" ", " "<<endTrack>>" #"#
    say "|"
    
    outputto "BPM" #"# BPM
    sayregexp "(?<=>BPM:)[^>]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "Genre" #"# GENRE
    sayregexp "(?<=trackGenre>>)[^>]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "ISRC" #"# ISRC
    sayregexp "(?<=>ISRC:)[^>]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "InitialKey" #"# INITIAL KEY
    sayregexp "(?<=>KEY:)[^>]+" ", " "<<endTrack>>" #"#
    say "|"

    outputto "Beatport_Track_URL" #"# BEATPORT_TRACK_URL
    say "http://www.beatport.com/track/"
    sayregexp "(?<=>BPTrackURL>)[^<]+" "/" "<<endTrack>>"
    say "|"

    outputto "track temp / total track temp" #"# TRACK/TOTALTRACK *FIX AT BOTTOM
    sayregexp "(?<=trackNumber\":)[^\,]+" ", " "<<endTrack>>" #"#
    say "/"
    sayoutput "totaltracks"
    say "|"
    
findinline "<<endTrack>>"
while "<<nextTrack>>" 99 #<<#"# Think this is the maximum amount of loops
# TRACK FIX: TRACK / TOTALTRACKS **KEEP AT VERY BOTTOM OF SCRIPT
    outputto "Title"
    sayoutput "Tracks"
    outputto "Track"
    sayoutput "track temp / total track temp"
    set "track temp / total track temp"