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