2013-08-06 04:45:57 +02:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2013-08-08 04:07:07 +02:00
|
|
|
|
using System.Globalization;
|
2013-08-06 04:45:57 +02:00
|
|
|
|
using System.Linq;
|
2013-08-08 04:07:07 +02:00
|
|
|
|
using System.Text.RegularExpressions;
|
2013-08-06 04:45:57 +02:00
|
|
|
|
using System.Xml.Linq;
|
2013-08-07 08:58:31 +02:00
|
|
|
|
using NLog;
|
2013-08-06 04:45:57 +02:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
|
{
|
|
|
|
|
public static class XElementExtensions
|
|
|
|
|
{
|
2013-08-07 08:58:31 +02:00
|
|
|
|
|
|
|
|
|
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
|
|
|
|
|
|
2013-08-08 04:07:07 +02:00
|
|
|
|
private static readonly Regex RemoveTimeZoneRegex = new Regex(@"\s[A-Z]{2,4}$", RegexOptions.Compiled);
|
|
|
|
|
|
2013-08-06 04:45:57 +02:00
|
|
|
|
public static string Title(this XElement item)
|
|
|
|
|
{
|
2013-08-06 07:06:58 +02:00
|
|
|
|
return item.TryGetValue("title", "Unknown");
|
2013-08-06 04:45:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static DateTime PublishDate(this XElement item)
|
|
|
|
|
{
|
2013-08-07 08:58:31 +02:00
|
|
|
|
string dateString = item.TryGetValue("pubDate");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2013-08-08 04:07:07 +02:00
|
|
|
|
DateTime result;
|
|
|
|
|
if (!DateTime.TryParse(dateString, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal, out result))
|
|
|
|
|
{
|
|
|
|
|
dateString = RemoveTimeZoneRegex.Replace(dateString, "");
|
|
|
|
|
result = DateTime.Parse(dateString, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.AdjustToUniversal);
|
|
|
|
|
}
|
|
|
|
|
return result.ToUniversalTime();
|
2013-08-07 08:58:31 +02:00
|
|
|
|
}
|
|
|
|
|
catch (FormatException e)
|
|
|
|
|
{
|
2013-08-08 02:57:45 +02:00
|
|
|
|
Logger.WarnException("Unable to parse " + dateString, e);
|
2013-08-07 08:58:31 +02:00
|
|
|
|
throw;
|
|
|
|
|
}
|
2013-08-06 04:45:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static List<String> Links(this XElement item)
|
|
|
|
|
{
|
|
|
|
|
var elements = item.Elements("link");
|
|
|
|
|
|
2013-08-06 07:06:58 +02:00
|
|
|
|
return elements.Select(link => link.Value).ToList();
|
2013-08-06 04:45:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Description(this XElement item)
|
|
|
|
|
{
|
2013-08-06 07:06:58 +02:00
|
|
|
|
return item.TryGetValue("description");
|
2013-08-06 04:45:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static string Comments(this XElement item)
|
|
|
|
|
{
|
2013-08-06 07:06:58 +02:00
|
|
|
|
return item.TryGetValue("comments");
|
2013-08-06 04:45:57 +02:00
|
|
|
|
}
|
|
|
|
|
|
2013-08-06 07:06:58 +02:00
|
|
|
|
private static string TryGetValue(this XElement item, string elementName, string defaultValue = "")
|
2013-08-06 04:45:57 +02:00
|
|
|
|
{
|
|
|
|
|
var element = item.Element(elementName);
|
|
|
|
|
|
|
|
|
|
return element != null ? element.Value : defaultValue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|