# ###################################################################
# Mp3tag Tag Source for Douban API
#
# Created by Cople
# 
# This file should be stored in your tag sources sources directory
# %APPDATA%\Mp3tag\data\sources
# and requires Mp3tag v2.90b or above.
#
# [2017-05-20] v2.0
# [2017-12-15] v2.0.1 修复 Publisher 包含 "|" 时报错
# [2018-10-15] v2.0.2 优化代码
#
# ###################################################################
# http://help.mp3tag.de/main_online.html

[Name]=douban.com
[BasedOn]=https://api.douban.com
[IndexUrl]=https://api.douban.com/v2/music/search?q=%s
[AlbumUrl]=https://api.douban.com/v2/music/
[WordSeparator]=%20
[IndexFormat]=%Artist%|%Album%|%Type%|%Released%|%Publisher%|%_url%|%_preview%
[SearchBy]=%album%
[Encoding]=url-utf-8

[ParserScriptIndex]=...
# debug "on" "c:\mp3tag_debug_douban_a.html" "5"
# ###################################################################
#         I  N  D  E  X
# ###################################################################
json "ON"

json_foreach "musics"

	# Artist 艺术家
	json_select_many "author" "name" ", "
	sayrest
	say "|"

	# Album 唱片集
	json_select "title"
	sayrest
	say "|"

	json_select_object "attrs"
		# Type 类型
		json_select_array "version" 1
		sayrest
		say "|"

		# Released 发行时间
		json_select_array "pubdate" 1
		sayrest
		say "|"

		# Publisher 出版者
		json_select_array "publisher" 1
		replace "|" "-"
		sayrest
		say "|"
	json_unselect_object

	# URL 专辑查询地址
	json_select "id"
	sayrest
	say "|"

	# Preview
	json_select "alt"
	sayrest

	saynewline

json_foreach_end


[ParserScriptAlbum]=...
# debug "on" "c:\mp3tag_debug_douban_b.html" "5"
# ###################################################################
#         A  L  B  U  M
# ###################################################################
json "ON"

# Album
outputto "ALBUM"
json_select "title"
sayrest

outputto "SUBTITLE"
json_select "alt_title"
sayrest

# Douban ID
outputto "DOUBAN_ID"
json_select "id"
sayrest

# WWW
outputto "WWW"
json_select "alt"
sayrest

# Mobile Link
outputto "MOBILELINK"
json_select "mobile_link"
sayrest

# Cover URL
outputto "coverurl"
json_select "image"
regexpreplace "https" "http"
regexpreplace "spic" "lpic"
sayrest

# Comment
outputto "COMMENT"
json_select "summary"
ifnot ""
	sayrest
else
	json_select "alt_title"
	sayrest
endif

json_select_object "attrs"
	# Publisher
	outputto "PUBLISHER"
	json_select_array "publisher" 1
	sayrest

	# Media Type
	outputto "MEDIATYPE"
	json_select_array "media" 1
	sayrest

	# YEAR 发行时间
	outputto "YEAR"
	json_select_array "pubdate" 1
	sayregexp "\d{4}"

	# Released 发行时间
	outputto "RELEASETIME"
	json_select_array "pubdate" 1
	sayrest

	# Artist
	outputto "ARTIST"
	json_select_array "singer" -1 ", "
	sayrest

	# Version
	outputto "VERSION"
	json_select_array "version" 1
	sayrest

	# Total Discs
	outputto "tTotalDiscs"
	json_select_array "discs" 1
	sayrest

	# Tracks
	outputto "TRACKS"
	json_select_array "tracks" 1
	regexpreplace "\n" "|"
	regexpreplace "CD \d\|" ""
	regexpreplace "(^|\|)0?\d+\.?( |\t)" "$1"
	sayrest
json_unselect_object
