sonarr-repo-only/NzbDrone.Core/Providers/UpdateProvider.cs

82 lines
2.9 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NLog;
using Ninject;
using NzbDrone.Common;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
2011-11-13 04:07:06 +00:00
namespace NzbDrone.Core.Providers
{
2011-11-14 02:54:09 +00:00
public class UpdateProvider
{
private readonly HttpProvider _httpProvider;
private readonly ConfigProvider _configProvider;
private readonly ConfigFileProvider _configFileProvider;
private readonly EnviromentProvider _enviromentProvider;
2011-11-13 04:07:06 +00:00
private readonly ArchiveProvider _archiveProvider;
private readonly ProcessProvider _processProvider;
2011-11-14 01:27:11 +00:00
private readonly DiskProvider _diskProvider;
2011-11-13 07:27:16 +00:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2011-11-13 07:27:16 +00:00
private static readonly Regex parseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
[Inject]
public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider, ConfigFileProvider configFileProvider,
2011-11-14 01:27:11 +00:00
EnviromentProvider enviromentProvider, ArchiveProvider archiveProvider, ProcessProvider processProvider, DiskProvider diskProvider)
{
_httpProvider = httpProvider;
_configProvider = configProvider;
_configFileProvider = configFileProvider;
_enviromentProvider = enviromentProvider;
2011-11-13 04:07:06 +00:00
_archiveProvider = archiveProvider;
_processProvider = processProvider;
2011-11-14 01:27:11 +00:00
_diskProvider = diskProvider;
}
public UpdateProvider()
{
}
private List<UpdatePackage> GetAvailablePackages()
{
var updateList = new List<UpdatePackage>();
var rawUpdateList = _httpProvider.DownloadString(_configProvider.UpdateUrl);
2011-11-13 07:27:16 +00:00
var matches = parseRegex.Matches(rawUpdateList);
foreach (Match match in matches)
{
var updatePackage = new UpdatePackage();
updatePackage.FileName = match.Groups["filename"].Value;
updatePackage.Url = _configProvider.UpdateUrl + updatePackage.FileName;
updatePackage.Version = new Version(match.Groups["version"].Value);
updateList.Add(updatePackage);
}
return updateList;
}
public virtual UpdatePackage GetAvilableUpdate()
{
var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
if (latestAvailable != null && latestAvailable.Version > _enviromentProvider.Version)
{
2011-11-13 07:27:16 +00:00
logger.Debug("An update is available ({0}) => ({1})", _enviromentProvider.Version, latestAvailable.Version);
return latestAvailable;
}
2011-11-13 07:27:16 +00:00
logger.Trace("No updates available");
return null;
}
}
}