2013-04-14 02:57:10 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-04-15 04:41:39 +03:00
|
|
|
using System.Linq;
|
2013-04-14 02:57:10 +03:00
|
|
|
using System.Text.RegularExpressions;
|
2013-04-15 04:41:39 +03:00
|
|
|
using NLog;
|
2013-04-14 02:57:10 +03:00
|
|
|
using NzbDrone.Common;
|
|
|
|
using NzbDrone.Core.Configuration;
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Update
|
|
|
|
{
|
2013-04-15 04:41:39 +03:00
|
|
|
public interface IUpdatePackageProvider
|
2013-04-14 02:57:10 +03:00
|
|
|
{
|
|
|
|
IEnumerable<UpdatePackage> GetAvailablePackages();
|
2013-04-15 04:41:39 +03:00
|
|
|
UpdatePackage GetLatestUpdate();
|
2013-04-14 02:57:10 +03:00
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
public class UpdatePackageProvider : IUpdatePackageProvider
|
2013-04-14 02:57:10 +03:00
|
|
|
{
|
|
|
|
private readonly IConfigService _configService;
|
|
|
|
private readonly IHttpProvider _httpProvider;
|
2013-04-15 04:41:39 +03:00
|
|
|
private readonly Logger _logger;
|
2013-04-14 02:57:10 +03:00
|
|
|
|
|
|
|
private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
public UpdatePackageProvider(IConfigService configService, IHttpProvider httpProvider, Logger logger)
|
2013-04-14 02:57:10 +03:00
|
|
|
{
|
|
|
|
_configService = configService;
|
|
|
|
_httpProvider = httpProvider;
|
2013-04-15 04:41:39 +03:00
|
|
|
_logger = logger;
|
2013-04-14 02:57:10 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<UpdatePackage> GetAvailablePackages()
|
|
|
|
{
|
|
|
|
var updateList = new List<UpdatePackage>();
|
|
|
|
var updateUrl = _configService.UpdateUrl;
|
2013-04-15 04:41:39 +03:00
|
|
|
|
|
|
|
_logger.Debug("Getting a list of updates from {0}", updateUrl);
|
|
|
|
|
2013-04-14 02:57:10 +03:00
|
|
|
var rawUpdateList = _httpProvider.DownloadString(updateUrl);
|
|
|
|
var matches = ParseRegex.Matches(rawUpdateList);
|
|
|
|
|
|
|
|
foreach (Match match in matches)
|
|
|
|
{
|
|
|
|
var updatePackage = new UpdatePackage();
|
|
|
|
updatePackage.FileName = match.Groups["filename"].Value;
|
|
|
|
updatePackage.Url = updateUrl + updatePackage.FileName;
|
|
|
|
updatePackage.Version = new Version(match.Groups["version"].Value);
|
|
|
|
updateList.Add(updatePackage);
|
|
|
|
}
|
|
|
|
|
2013-04-15 04:41:39 +03:00
|
|
|
_logger.Debug("Found {0} update packages", updateUrl.Length);
|
|
|
|
|
2013-04-14 02:57:10 +03:00
|
|
|
return updateList;
|
|
|
|
}
|
2013-04-15 04:41:39 +03:00
|
|
|
|
|
|
|
public UpdatePackage GetLatestUpdate()
|
|
|
|
{
|
|
|
|
return GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
|
|
|
|
}
|
2013-04-14 02:57:10 +03:00
|
|
|
}
|
|
|
|
}
|