Radarr/NzbDrone.Core/Update/UpdatePackageProvider.cs

66 lines
2.3 KiB
C#
Raw Normal View History

2013-04-13 23:57:10 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
2013-04-13 23:57:10 +00:00
using System.Text.RegularExpressions;
using NLog;
2013-04-13 23:57:10 +00:00
using NzbDrone.Common;
using NzbDrone.Common.EnvironmentInfo;
2013-04-13 23:57:10 +00:00
using NzbDrone.Core.Configuration;
namespace NzbDrone.Core.Update
{
public interface IUpdatePackageProvider
2013-04-13 23:57:10 +00:00
{
IEnumerable<UpdatePackage> GetAvailablePackages();
UpdatePackage GetLatestUpdate();
2013-04-13 23:57:10 +00:00
}
public class UpdatePackageProvider : IUpdatePackageProvider
2013-04-13 23:57:10 +00:00
{
private readonly IConfigFileProvider _configService;
2013-04-13 23:57:10 +00:00
private readonly IHttpProvider _httpProvider;
private readonly Logger _logger;
2013-04-13 23:57:10 +00:00
private static readonly Regex ParseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>(?<=\.)\d+\.\d+\.\d+\.\d+).+?)(?:\<\/a\>)",
RegexOptions.IgnoreCase);
2013-04-13 23:57:10 +00:00
public UpdatePackageProvider(IConfigFileProvider configService, IHttpProvider httpProvider, Logger logger)
2013-04-13 23:57:10 +00:00
{
_configService = configService;
_httpProvider = httpProvider;
_logger = logger;
2013-04-13 23:57:10 +00:00
}
public IEnumerable<UpdatePackage> GetAvailablePackages()
{
var updateList = new List<UpdatePackage>();
var branch = _configService.Branch;
var version = BuildInfo.Version;
var updateUrl = String.Format("http://update.nzbdrone.com/v{0}/{1}/", version.Major, branch);
_logger.Debug("Getting a list of updates from {0}", updateUrl);
2013-04-13 23:57:10 +00: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);
}
_logger.Debug("Found {0} update packages", updateUrl.Length);
2013-04-13 23:57:10 +00:00
return updateList;
}
public UpdatePackage GetLatestUpdate()
{
return GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
}
2013-04-13 23:57:10 +00:00
}
}