Skip to content

Instantly share code, notes, and snippets.

@isaaclyman
Last active February 25, 2021 20:06
Show Gist options
  • Save isaaclyman/033c29948a524e2f7a3a5a4f3b168023 to your computer and use it in GitHub Desktop.
Save isaaclyman/033c29948a524e2f7a3a5a4f3b168023 to your computer and use it in GitHub Desktop.
Zero-validation XML builder for when everything else won't do what you want
string Tag(string name, IEnumerable<Tuple<string, string>> attributes, string contents = "")
{
var attributeString = attributes.Any() ? " " + string.Join(' ', attributes.Select(a => @$"{a.Item1}=""{a.Item2}""")) : string.Empty;
var hasContents = !string.IsNullOrEmpty(contents);
if (!hasContents) {
return
@$"<{name}{attributeString}/>";
}
return
@$"<{name}{attributeString}>
{contents}
</{name}>";
}
string Tag(string name, Tuple<string, string> attribute, string contents = "")
{
var attributeString = @$" {attribute.Item1}=""{attribute.Item2}""";
var hasContents = !string.IsNullOrEmpty(contents);
if (!hasContents)
{
return
@$"<{name}{attributeString}/>";
}
var longContents = contents.Length > 50;
return
@$"<{name}{attributeString}>{(longContents ? "\n" : "")}{contents}{(longContents ? "\n" : "")}</{name}>";
}
string Tag(string name, string contents = "")
{
var longContents = contents.Length > 50;
return
@$"<{name}>{(longContents ? "\n" : "")}{contents}{(longContents ? "\n" : "")}</{name}>";
}
string TagSet(params string[] tags) {
return String.Join("\n", tags);
}
string TagSet(IEnumerable<string> tags) {
return String.Join("\n", tags);
}
Tuple<string, string> Attribute(string name, string value) {
return new Tuple<string, string>(name, value);
}
IEnumerable<Tuple<string, string>> AttributeSet(params Tuple<string, string>[] attributes) {
return attributes.AsEnumerable();
}
IEnumerable<Tuple<string, string>> AttributeSet(IEnumerable<Tuple<string, string>> attributes)
{
return attributes.AsEnumerable();
}
var currentFolder = Path.GetDirectoryName(Util.CurrentQueryPath);
foreach (var show in shows) {
var sw = new StringWriterWithEncoding(Encoding.UTF8);
sw.Write(
Tag("rss", AttributeSet(Attribute("xmlns:itunes", "http://www.itunes.com/dtds/podcast-1.0.dtd"), Attribute("version", "2.0")),
Tag("channel", TagSet(
Tag("title", show.display),
Tag("description", show.description),
Tag("copyright", "2021 Original author"),
Tag("language", "en-US"),
Tag("itunes:subtitle"),
Tag("itunes:author", "Original author"),
Tag("itunes:block", "yes"),
Tag("itunes:owner", TagSet(
Tag("itunes:email", "[email protected]"),
Tag("itunes:name", "Original author")
)),
Tag("itunes:category", Attribute("text", "Radio")),
Tag("itunes:explicit", "no"),
Tag("itunes:image", Attribute("href", "https://example.cloudfront.net/thumbnail/" + show.thumbnail)),
Tag("itunes:type", "episodic"),
Tag("image", "https://example.cloudfront.net/thumbnail/" + show.thumbnail),
Tag("link", "https://example.com/shows/" + show.internalName),
TagSet(show.seasons.SelectMany(season => season.Select(episode =>
Tag("item", TagSet(
Tag("title", episode.display),
Tag("description"),
Tag("link", $"https://example.s3-us-west-1.amazonaws.com/shows/{show.internalName}/{episode.file}"),
Tag("itunes:title", episode.display),
Tag("itunes:subtitle"),
Tag("itunes:image", Attribute("href", "https://example.cloudfront.net/thumbnail/" + show.thumbnail)),
Tag("itunes:explicit", "no"),
Tag("itunes:episodeType", "full"),
Tag("itunes:block", "yes"),
Tag("enclosure", AttributeSet(
Attribute("url", $"https://example.amazonaws.com/shows/{show.internalName}/{episode.file}"),
Attribute("type", "audio/mpeg")
))
))
)))
))
)
);
using (StreamWriter outputFile = new StreamWriter(Path.Combine(currentFolder, $"{show.internalName}.xml")))
{
await outputFile.WriteAsync(sw.ToString());
$"{show.internalName}.xml written.".Dump();
}
}
class StringWriterWithEncoding : StringWriter
{
private readonly Encoding _encoding;
public StringWriterWithEncoding(Encoding encoding)
{
this._encoding = encoding;
}
public override Encoding Encoding
{
get { return _encoding; }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment