Tools and other software Powershell Script to add rerun/New and categories to EPG. (1 Viewer)


New Member
August 25, 2015
Home Country
United States of America United States of America
I did some searching but didn't find much on this in the forums. I imagine there is someone else that has done this, because its really not so hard to use if you know a little powershell and XML.. but consider the following situation:

1. USA user, running Mediaportal on Windows 10 (so you have to use a 3rd party source to get new Rovio sourced EPG data).
2. You wish to use color coding on EPG and Mediaportal (1.12 at least) only seems to grab the first category when multiple categories are listed in the tvguide.xml.
3. You want to know when a program first ran, or if it is new, by looking at the description. Yes, you can set an * to appear in the title for recording but that doesn't give you the date.
4. You know how to write simple powershell scripts, schedule them with task scheduler, and troubleshoot them.

So, in my case, I wrote a simple powershell script to open the XML, parse the programme nodes, and adjust things so they work like I want. Powershell handles XML natively, so this script does the following:
1. Discards the "Other" category, as it appears to be overused.
2. Combines each category with a space between multiple ones in one category node, discarding the original category nodes.
3. Adds a description if your programme happens to not have one (rare but happens), that is just the title again.
4. Looks for new or previously-shown nodes, grabs the start datetime from the attribute (if found) and pre-pends it to the descriptions as (New), (Rerun), or (Rerun: 12/21/2015) depending on what it finds.

This is just a part of my EPG import process of course. But that probably varies enough with different users that its not worth showing here. In my case, I schedule a batch file to clean up the old epg data, run the acquisition utility, then parse it with this, then move it over to the proper APPDATA folder for MP to use.

Heres the script:

# Microsoft (ROVI based) EPG Clean Up Script.
# Date: 8/20/2015
# Usage: Run this in the folder your tvguide.xml lives in. It will create a tvguide.xml that is 'cleaned'.
# Since I intended this to run as part of a batch script, all those other details like moving the file to
# APPDATA for xmltv import or spawning mc2xml is handled outside this script.

$EPGXMLSourcePath = [string]::Format("{0}\{1}", (Get-Location), "tvguide.xml")
if([System.IO.File]::Exists($EPGXMLSourcePath)) {
$EPGXMLSourcePath = Resolve-Path($EPGXMLSourcePath)
} else {
Write-Host -ForegroundColor Red "Script could not find TVGuide.xml."
Write-Host -ForegroundColor Red "tvguide.xml should already exist in the folder this script runs from."

# Set up the EPG reader, then put the data into a new XML doc for processing.
$readerSettings = New-Object System.Xml.XmlReaderSettings
$readerSettings.ProhibitDtd = $false
$readerSettings.XmlResolver = $null
$EPGReader = [System.Xml.XmlReader]::Create($EPGXMLSourcePath, $readerSettings)
$EPGxmlDoc = New-Object System.Xml.XmlDocument

# Set up the nodes in the EPG XML Doc. The doc starts with a TV node, containing Channels then Program nodes.
$TVNode = $EPGxmlDoc.ChildNodes.Item($EPGxmlDoc.ChildNodes.Count-1)
$ChannelNodes = $TVNode.SelectNodes("/tv/channel")
$ProgramNodes = $TVNode.SelectNodes("/tv/programme")

#Start parsing program nodes, cleaning things up as needed.
foreach($Node in $ProgramNodes) {
# Fix the category for Mediportal by putting all of them into one entry.
$NewCategory = ""
$CatText = ""
$CategoryNodeList = $Node.selectNodes("category")
foreach($Category in $CategoryNodeList) {
$CatText = $Category.InnerText.ToLower()
if($CatText -ne "other") {
$NewCategory += "$CatText "
$ReturnCode = $Category.ParentNode.RemoveChild($Category)
$NewCategory = $NewCategory.Trim()
$NewCatNode = $Node.Appendchild($EPGxmlDoc.CreateElement("category"))
$ReturnCode = $NewCatNode.SetAttribute('lang','en')
$NewCatNode.InnerText = $NewCategory
$ReturnCode = $Node.Appendchild($NewCatNode)

# Rarely we find a program without a description. Fix that here.
$Desc = $Node.selectSingleNode("desc").InnerText
$DescNode = $Node.selectSingleNode("desc")
if(!$DescNode) {
$DescNode = $Node.Appendchild($EPGxmlDoc.CreateElement("desc"))
$ReturnCode = $DescNode.SetAttribute('lang','en')
$Desc = $Node.selectSingleNode("title").InnerText.Trim()
$DescNode.InnerText = $Desc.Trim()
$ReturnCode = $Node.Appendchild($DescNode)

# Now fix the new/rerun info so its part of the description.
$NewProgram = $Node.selectSingleNode("new")
$DescNode.InnerText = "(New) $Desc"
$ReRun = $Node.selectSingleNode("previously-shown")
$FirstRun = $ReRun.getAttribute("start")
if($FirstRun) {
$FirstRun = [DateTime]::parseExact($FirstRun,"yyyyMMddHHmmss",$null)
$FirstRun = $FirstRun.ToString("M/d/yyyy")
$DescNode.InnerText = "(Rerun: $FirstRun) $Desc"
} else {
$DescNode.InnerText = "(Rerun) $Desc"


It should be pretty simple to change formatting or a lot of the script actions if you know some powershell. I am not in a position to support this frequently, but I didn't find anything like it searching the web so I thought I would at least try to give back to the MP users a little by posting this.

And yes, if you try to just go run this, you are likely to encounter lots of little issues (like set-executionpolicy remotesigned, or UAC, or more restrictive folder permissions defaults in Windows 10) but all of those are resolvable. Google is your friend. :)

One item I might mention is that if you run it more than once on the same EPG data, you'll get duplicated text "(New) New) ..." in your descriptions. Since I always run the utility to grab new EPG data before running this, I don't have an issue with that.

FYI - if you dont like what this does, just remove it from your EPG processing steps and reimport. As long as your XMLTV plug in setting is "delete before reimport" it should clear out the old data.

And if you find an error or typo, or want to improve it, I am all for that. I'll update this if it needs for typos and such, but rewrites and enhancements are probably best tracked in subsequent posts.

Thanks again MP community, hope this helps some of you out.[DOUBLEPOST=1440520793][/DOUBLEPOST]Haha, I like the smilie the forum put in for the ParseExact command. I think I'll leave it.

Users who are viewing this thread

Top Bottom