use xml writer so strings are properly escaped (#4)
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Xml;
|
||||
using ErsatzTV.Core.Domain;
|
||||
using static LanguageExt.Prelude;
|
||||
|
||||
@@ -23,19 +24,31 @@ namespace ErsatzTV.Core.Iptv
|
||||
public string ToXml()
|
||||
{
|
||||
var sb = new StringBuilder();
|
||||
sb.AppendLine("<?xml version=\"1.0\"?>");
|
||||
sb.AppendLine("<tv generator-info-name=\"ersatztv\">");
|
||||
|
||||
using var xml = XmlWriter.Create(sb);
|
||||
xml.WriteStartDocument();
|
||||
|
||||
xml.WriteStartElement("tv");
|
||||
xml.WriteAttributeString("generator-info-name", "ersatztv");
|
||||
|
||||
foreach (Channel channel in _channels)
|
||||
{
|
||||
sb.AppendLine($"<channel id=\"{channel.Number}\">");
|
||||
sb.AppendLine($"<display-name lang=\"en\">{channel.Name}</display-name>");
|
||||
sb.AppendLine(
|
||||
!string.IsNullOrWhiteSpace(channel.Logo)
|
||||
? $"<icon src=\"{_scheme}://{_host}/iptv/images/{channel.Logo}\"/>"
|
||||
: $"<icon src=\"{_scheme}://{_host}/images/ersatztv-500.png\"/>");
|
||||
xml.WriteStartElement("channel");
|
||||
xml.WriteAttributeString("id", channel.Number.ToString());
|
||||
|
||||
xml.WriteStartElement("display-name");
|
||||
xml.WriteAttributeString("lang", "en");
|
||||
xml.WriteString(channel.Name);
|
||||
xml.WriteEndElement(); // display-name
|
||||
|
||||
sb.AppendLine("</channel>");
|
||||
xml.WriteStartElement("icon");
|
||||
xml.WriteAttributeString(
|
||||
"src",
|
||||
!string.IsNullOrWhiteSpace(channel.Logo)
|
||||
? $"{_scheme}://{_host}/iptv/images/{channel.Logo}"
|
||||
: $"{_scheme}://{_host}/images/ersatztv-500.png");
|
||||
xml.WriteEndElement(); // icon
|
||||
|
||||
xml.WriteEndElement(); // channel
|
||||
}
|
||||
|
||||
foreach (Channel channel in _channels)
|
||||
@@ -50,35 +63,55 @@ namespace ErsatzTV.Core.Iptv
|
||||
Title = Path.GetFileName(playoutItem.MediaItem.Path)
|
||||
});
|
||||
|
||||
sb.AppendLine(
|
||||
$"<programme start=\"{start}\" stop=\"{stop}\" channel=\"{channel.Number}\">");
|
||||
sb.AppendLine($"<title lang=\"en\">{metadata.Title}</title>");
|
||||
sb.AppendLine("<previously-shown/>");
|
||||
sb.AppendLine("<sub-title lang=\"en\"></sub-title>");
|
||||
xml.WriteStartElement("programme");
|
||||
xml.WriteAttributeString("start", start);
|
||||
xml.WriteAttributeString("stop", stop);
|
||||
xml.WriteAttributeString("channel", channel.Number.ToString());
|
||||
|
||||
xml.WriteStartElement("title");
|
||||
xml.WriteAttributeString("lang", "en");
|
||||
xml.WriteString(metadata.Title);
|
||||
xml.WriteEndElement(); // title
|
||||
|
||||
xml.WriteStartElement("previously-shown");
|
||||
xml.WriteEndElement(); // previously-shown
|
||||
|
||||
xml.WriteStartElement("sub-title");
|
||||
xml.WriteAttributeString("lang", "en");
|
||||
xml.WriteEndElement(); // sub-title
|
||||
|
||||
int season = Optional(metadata.SeasonNumber).IfNone(0);
|
||||
int episode = Optional(metadata.EpisodeNumber).IfNone(0);
|
||||
if (season > 0 && episode > 0)
|
||||
{
|
||||
sb.AppendLine($"<episode-num system=\"xmltv_ns\">{season - 1}.{episode - 1}.0/1</episode-num>");
|
||||
xml.WriteStartElement("episode-num");
|
||||
xml.WriteAttributeString("system", "xmltv_ns");
|
||||
xml.WriteString($"{season - 1}.{episode - 1}.0/1");
|
||||
xml.WriteEndElement(); // episode-num
|
||||
}
|
||||
|
||||
// sb.AppendLine("<icon src=\"\"/>");
|
||||
sb.AppendLine($"<desc lang=\"en\">{metadata.Description}</desc>");
|
||||
xml.WriteStartElement("desc");
|
||||
xml.WriteAttributeString("lang", "en");
|
||||
xml.WriteString(metadata.Description);
|
||||
xml.WriteEndElement(); // desc
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(metadata.ContentRating))
|
||||
{
|
||||
sb.AppendLine("<rating system=\"MPAA\">");
|
||||
sb.AppendLine($"<value>{metadata.ContentRating}</value>");
|
||||
sb.AppendLine("</rating>");
|
||||
xml.WriteStartElement("rating");
|
||||
xml.WriteAttributeString("system", "MPAA");
|
||||
xml.WriteStartElement("value");
|
||||
xml.WriteString(metadata.ContentRating);
|
||||
xml.WriteEndElement(); // value
|
||||
xml.WriteEndElement(); // rating
|
||||
}
|
||||
|
||||
sb.AppendLine("</programme>");
|
||||
xml.WriteEndElement(); // programme
|
||||
}
|
||||
}
|
||||
|
||||
sb.AppendLine("</tv>");
|
||||
|
||||
xml.WriteEndElement(); // tv
|
||||
xml.WriteEndDocument();
|
||||
|
||||
return sb.ToString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user