1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-10-31 16:02:29 +01:00
Sonarr/NzbDrone.Core/Providers/UpdateProvider.cs

95 lines
3.4 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Diagnostics;
2011-11-22 07:55:09 +01:00
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using NLog;
using NzbDrone.Common;
using NzbDrone.Core.Model;
using NzbDrone.Core.Providers.Core;
2011-11-13 05:07:06 +01:00
namespace NzbDrone.Core.Providers
{
2011-11-14 03:54:09 +01:00
public class UpdateProvider
{
private readonly HttpProvider _httpProvider;
private readonly ConfigProvider _configProvider;
private readonly EnvironmentProvider _environmentProvider;
2011-11-22 07:55:09 +01:00
2011-11-14 02:27:11 +01:00
private readonly DiskProvider _diskProvider;
2011-11-13 08:27:16 +01:00
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
2011-11-13 08:27:16 +01:00
private static readonly Regex parseRegex = new Regex(@"(?:\>)(?<filename>NzbDrone.+?(?<version>\d+\.\d+\.\d+\.\d+).+?)(?:\<\/A\>)", RegexOptions.IgnoreCase);
2012-09-09 02:32:21 +02:00
public const string DEFAULT_UPDATE_URL = @"http://update.nzbdrone.com/_release/";
2011-11-22 07:55:09 +01:00
public UpdateProvider(HttpProvider httpProvider, ConfigProvider configProvider,
EnvironmentProvider environmentProvider, DiskProvider diskProvider)
{
_httpProvider = httpProvider;
_configProvider = configProvider;
_environmentProvider = environmentProvider;
2011-11-14 02:27:11 +01:00
_diskProvider = diskProvider;
}
public UpdateProvider()
{
}
private List<UpdatePackage> GetAvailablePackages()
{
var updateList = new List<UpdatePackage>();
var updateUrl = _configProvider.UpdateUrl;
var rawUpdateList = _httpProvider.DownloadString(updateUrl);
2011-11-13 08:27:16 +01:00
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);
}
return updateList;
}
public virtual UpdatePackage GetAvilableUpdate(Version currentVersion)
{
var latestAvailable = GetAvailablePackages().OrderByDescending(c => c.Version).FirstOrDefault();
if (latestAvailable != null && latestAvailable.Version > currentVersion)
{
logger.Debug("An update is available ({0}) => ({1})", currentVersion, latestAvailable.Version);
return latestAvailable;
}
2011-11-13 08:27:16 +01:00
logger.Trace("No updates available");
return null;
}
2011-11-22 07:55:09 +01:00
public virtual Dictionary<DateTime, string> UpdateLogFile()
{
var list = new Dictionary<DateTime, string>();
CultureInfo provider = CultureInfo.InvariantCulture;
if (_diskProvider.FolderExists(_environmentProvider.GetUpdateLogFolder()))
2011-11-22 07:55:09 +01:00
{
var files = _diskProvider.GetFiles(_environmentProvider.GetUpdateLogFolder(), SearchOption.TopDirectoryOnly).ToList();
2011-11-22 07:55:09 +01:00
foreach (var file in files.Select(c => new FileInfo(c)).OrderByDescending(c => c.Name))
2011-11-22 07:55:09 +01:00
{
list.Add(DateTime.ParseExact(file.Name.Replace(file.Extension, string.Empty), "yyyy.MM.dd-H-mm", provider), file.FullName);
}
}
return list;
}
}
}