**------------------------------------------------------------------------------------------------
* @header_start
* WebGrab+Plus ini for grabbing MDB data from TvGuide websites
* @MinSWversion : V1.1.1/56.25
*     -  (postprocess V2.0)
* @Site: thetvdb.com
* @Revision 2 - [19/06/2016] Jan van Straaten
*	- improved mdb_show_id
* @Revision 1 - [22/05/2016] Jan van Straaten
* 	- support for match on episode-num or sub-title, use of new scopes
*	- added mdbiniype
* @Revision 0 - [02/12/2015] Jan van Straaten
*   - creation
* @Remarks: - series database , primarysearch with bing
* @header_end
**------------------------------------------------------------------------------------------------
*
*
site {url=thetvdb.com|mdbinitype=serie|cultureinfo=en-US|charset=utf-8,iso-8859-2|matchfactor=70|searchsite=bing}
*site {episodesystem=xmltv_ns}
* primary search:
url_primarysearch {url()|http://www.bing.com/search?q=thetvdb+|'title'|}
url_primarysearch.headers {customheader=Accept-Encoding=gzip,deflate}
*http://www.bing.com/search?q=thetvdb+Malcolm+in+the+Middle
scope.range {(primarysearch)|end}
url_primarysearch.modify {replace()| |+} * if title has spaces
*<a href="http://thetvdb.com/?tab=series&id=72227"
*<a href="http://www.thetvdb.com/?tab=series&id=73838"
*<a href="http://thetvdb.com/?tab=season&amp;seriesid=73838&amp;seasonid=6527"
*<a href="http://thetvdb.com/?id=73388&tab=series" h="ID=SERP,5122.1">
*<a href="http://thetvdb.com/?tab=seasonall&id=73388"
mdb_show_id.scrub {regex()|primary||<a href="http://(?:www\.)?thetvdb.com/.*\?series&\?id=(\d+)||}
mdb_show_id.scrub {regex()|primary||<a href="http://(?:www\.)?thetvdb.com/\?.*?id=(\d+)||}
mdb_temp_4.modify {clear}
mdb_temp_5.modify {clear}
loop {(each "mdb_temp_1" in 'mdb_show_id' max=50)|end}
mdb_temp_4.modify {addend('mdb_temp_1' not "")|'mdb_temp_1'/all/de####} * change this line to set your prefered language!! e.g. /all/sv####}
mdb_temp_5.modify {addend('mdb_temp_1' not "")|'mdb_temp_1'/all/en####} * leave this as it is!
end_loop
mdb_show_id.modify {clear}
* English always the first
mdb_show_id.modify {addstart|'mdb_temp_4''mdb_temp_5'} * add together
mdb_show_id.modify {replace()|####|\|} * multi
*
end_scope

*mdb url's:
* apikey=8DAFF524CAF680FA , project name WG++
* http://www.thetvdb.com/api/8DAFF524CAF680FA/series/82654/all/nl
url_mdb_p1 {url()|primary|http://thetvdb.com/api/8DAFF524CAF680FA/series/|mdb_show_id|.xml}
*http://thetvdb.com/api/8DAFF524CAF680FA/series/295743/all/sv.xml
url_mdb.headers {customheader=Accept-Encoding=gzip,deflate}
url_mdb.headers {accept=text/html, application/xhtml+xml, image/jxr, */*}
*
* the returned page starts with a series data element <Series> followed by all the known episodes in a <Episode> element
* following the mdbconfig mustmatch="title,subtitle" the program looks for a match of the title (in the <Series> element)
* and a match of the subtitle in one of the <Episode> elements
*
scope.range {(match)|end}
* the subtitle is the episode title. The next scrub first results in all the subtitles for this series
* but the matching routine will automatically replace it by the one that matches (highest matchfactor )
*mdb_subtitle.scrub {multi|p1|<Episode>|<EpisodeName>|</EpisodeName>|</Episode>} * old style: use episodetitlelist instead:
mdb_episodetitlelist.scrub {multi|p1|<Episode>|<EpisodeName>|</EpisodeName>|</Episode>}
*****************************
* the episodenum can be used as alternative for episode matching if subtitle is not available
* it will be matched following the same procedure as for subtitle matching it with the xmltv episode-num
mdb_episodenumlist.scrub {regex(pattern="<EpisodeNumber>'E1'<SeasonNumber>'S1'")|p1||(<EpisodeNumber>.+?)</SeasonNumber>||}
mdb_episodenumlist.modify {remove(type=regex)|'mdb_episodenumlist' "</EpisodeNumber>(.+?)<SeasonNumber>"}
mdb_episodenumlist.modify {replace|</EpisodeNumber>| }
************************ 
* get the matching <episode> element (with most of the episode data)
* and select the matching one
* mdb_temp_6 contains all the episodes
mdb_temp_6.scrub {multi|p1|<Episode>||</Episode>|</Episode>} * all episodes
*mdb_temp_6.modify {replace()|\||\n\n\|} * testing only
end_scope
scope.range {(getelements)|end}
***************

* in case of matched subtitle
mdb_temp_1.modify {calculate('mdb_episodetitlelist' not "" type=element format=F0)|'mdb_episodetitlelist' 'mdb_subtitle' @} * index of the episode
* in case of matched episodenum
mdb_temp_1.modify {calculate('mdb_episodenumlist' not "" type=element format=F0)|'mdb_episodenumlist' 'mdb_episode' @} * index of the episode
mdb_temp_1.modify {substring(type=element)|'mdb_temp_6' 'mdb_temp_1' 1} * the episode in xml format
* from here mdb_temp_1 holds the <episode> with all the data
* episode_id
mdb_episode_id.modify {substring('mdb_temp_1' not "" type=regex)|'mdb_temp_1' "<id>(\d+?)</id>"}
*****************
* mdb elements:
* in the top <series> element (from p1):
mdb_title.scrub {single()|p1|<Data><Series>|<SeriesName>|</SeriesName>|</Series>}
mdb_actor.scrub {regex()|p1||<Actors>(.*?)</Actors>||}
mdb_category.scrub {single|p1|<Genre>||</Genre>|</Genre>}
mdb_category.modify {replace|!?!?!|\|}
* the rest of the elements (from temp_1)


*
* subtitle, if not already there
mdb_subtitle.modify {substring("" type=regex)|'mdb_temp_1' "<EpisodeName>(.*?)</EpisodeName>"}
* description
mdb_description.modify {substring(type=regex)|'mdb_temp_1' "<Overview>(.*?)</Overview>"}
* starrating
mdb_starrating.modify {substring(type=regex)|'mdb_temp_1' "<Rating>(.*?)</Rating>"}
* director
mdb_director.modify {substring(type=regex)|'mdb_temp_1' "<Director>(.*?)</Director>"}
mdb_director.modify {replace|!?!?!|\|}
* actor, if not already from the top elements
mdb_actor.modify {substring("" type=regex)|'mdb_temp_1' "<GuestStars>(.*?)</GuestStars>"}
mdb_actor.modify {replace|!?!?!|\|}
* writer
*mdb_writer.modify {substring(type=regex)|'mdb_temp_1' "<Writer>(.*?)</Writer>"}
mdb_showicon.modify {substring(type=regex)|'mdb_temp_1' "<filename>(.*?)</filename>"}
mdb_showicon.modify {addstart(not "")|http://www.thetvdb.com/banners/}
* productiondate
mdb_productiondate.modify {substring(type=regex)|'mdb_temp_1' "<FirstAired>(\d{4}.*?)</FirstAired>"}
*
* episodenum if not already done in the episodenumlist
* this part as the last one because episode is used to get mdb_temp_1 and may not be changed 
* during the part above 
* at this spot it can be changed from the internal 'onscreen' Sn En format to xmltv_ns or whatever else.
<Combined_episodenumber>7</Combined_episodenumber>
  <Combined_season>3</Combined_season>
mdb_episode.modify {clear}  
loop {('mdb_episode' "" max=1)|end}
mdb_episode.modify {substring("" type=regex)|'mdb_temp_1' "<SeasonNumber>(\d*?)</SeasonNumber>"}
mdb_temp_2.modify {substring(type=regex)|'mdb_temp_1' "<EpisodeNumber>(\d*?)(?:\.\d\|)</EpisodeNumber>"}
* onscreen
mdb_episode.modify {addstart(not "")|S}
mdb_episode.modify {addend('mdb_temp_2' not "")| E'mdb_temp_2'} *format Sx Ey
* xmltv_ns conversie
*mdb_temp_2.modify {calculate(not "" format=F0)|1 -} * episode is 1 based in tvdb!
*mdb_episode.modify {calculate(not "" format=F0)|1 -} * season is 1 based
*mdb_episode.modify {addend()|.'mdb_temp_2'.} * in xmltv_ns
end_loop
end_scope
