namespace CodeHollow.FeedReader.Feeds.Itunes
{
using System;
using System.Linq;
using System.Xml.Linq;
///
/// The basic itunes: elements that are part of the channel xml element of an rss2.0 feed
///
public class ItunesChannel
{
internal const string NAMESPACEPREFIX = "itunes";
///
/// Initializes a new instance of the class.
///
///
public ItunesChannel(XElement channelElement)
{
Author = channelElement.GetValue(NAMESPACEPREFIX, "author");
Block = channelElement.GetValue(NAMESPACEPREFIX, "block").EqualsIgnoreCase("yes");
Categories = GetItunesCategories(channelElement);
var imageElement = channelElement.GetElement(NAMESPACEPREFIX, "image");
if (imageElement != null)
{
Image = new ItunesImage(imageElement);
}
var explicitValue = channelElement.GetValue(NAMESPACEPREFIX, "explicit");
Explicit = explicitValue.EqualsIgnoreCase("yes", "explicit", "true");
Complete = channelElement.GetValue(NAMESPACEPREFIX, "complete").EqualsIgnoreCase("yes");
if (Uri.TryCreate(channelElement.GetValue(NAMESPACEPREFIX, "new-feed-url"), UriKind.Absolute, out var newFeedUrl))
{
NewFeedUrl = newFeedUrl;
}
var ownerElement = channelElement.GetElement(NAMESPACEPREFIX, "owner");
if (ownerElement != null)
{
Owner = new ItunesOwner(ownerElement);
}
Subtitle = channelElement.GetValue(NAMESPACEPREFIX, "subtitle");
Summary = channelElement.GetValue(NAMESPACEPREFIX, "summary");
Type = channelElement.GetValue(NAMESPACEPREFIX, "type");
}
///
/// The itunes:author element
///
public string Author { get; }
///
/// The itunes:block element
///
public bool Block { get; }
///
/// The itunes:category elements
///
public ItunesCategory[] Categories { get; }
///
/// The itunes:image element
///
public ItunesImage Image { get; }
///
/// The itunes:explicit element
///
public bool Explicit { get; }
///
/// The itunes:complete element
///
public bool Complete { get; }
///
/// The itunes:new-feed-url element
///
public Uri NewFeedUrl { get; }
///
/// The itunes:owner element
///
public ItunesOwner Owner { get; }
///
/// The itunes:subtitle element
///
public string Subtitle { get; }
///
/// The itunes:summary element
///
public string Summary { get; }
///
/// The itunes:type element
///
public string Type { get; }
///
/// Sets the itunes categories
///
/// the channel element
/// the itunes:categries
private ItunesCategory[] GetItunesCategories(XElement element)
{
var categoryElements = element.GetElements(NAMESPACEPREFIX, "category");
if (categoryElements == null)
return new ItunesCategory[0];
var query = from categoryElement in categoryElements
let children = GetItunesCategories(categoryElement)
select new ItunesCategory(categoryElement.GetAttributeValue("text"), children);
return query.ToArray();
}
}
}