2014-01-19 04:25:01 +00:00
|
|
|
|
using MediaBrowser.Common.Configuration;
|
|
|
|
|
using MediaBrowser.Common.Extensions;
|
|
|
|
|
using MediaBrowser.Common.IO;
|
|
|
|
|
using MediaBrowser.Common.Net;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Controller.Library;
|
|
|
|
|
using MediaBrowser.Controller.Notifications;
|
2014-01-19 04:25:01 +00:00
|
|
|
|
using MediaBrowser.Controller.Plugins;
|
|
|
|
|
using MediaBrowser.Model.Logging;
|
|
|
|
|
using MediaBrowser.Model.News;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
using MediaBrowser.Model.Notifications;
|
2014-01-19 04:25:01 +00:00
|
|
|
|
using MediaBrowser.Model.Serialization;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using System.Xml;
|
|
|
|
|
|
|
|
|
|
namespace MediaBrowser.Server.Implementations.News
|
|
|
|
|
{
|
|
|
|
|
public class NewsEntryPoint : IServerEntryPoint
|
|
|
|
|
{
|
|
|
|
|
private Timer _timer;
|
|
|
|
|
private readonly IHttpClient _httpClient;
|
|
|
|
|
private readonly IApplicationPaths _appPaths;
|
|
|
|
|
private readonly IFileSystem _fileSystem;
|
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
|
private readonly IJsonSerializer _json;
|
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
private readonly INotificationManager _notifications;
|
|
|
|
|
private readonly IUserManager _userManager;
|
|
|
|
|
|
2014-01-19 04:25:01 +00:00
|
|
|
|
private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
|
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
public NewsEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IJsonSerializer json, INotificationManager notifications, IUserManager userManager)
|
2014-01-19 04:25:01 +00:00
|
|
|
|
{
|
|
|
|
|
_httpClient = httpClient;
|
|
|
|
|
_appPaths = appPaths;
|
|
|
|
|
_fileSystem = fileSystem;
|
|
|
|
|
_logger = logger;
|
|
|
|
|
_json = json;
|
2014-04-27 03:42:05 +00:00
|
|
|
|
_notifications = notifications;
|
|
|
|
|
_userManager = userManager;
|
2014-01-19 04:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Run()
|
|
|
|
|
{
|
|
|
|
|
_timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Called when [timer fired].
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="state">The state.</param>
|
|
|
|
|
private async void OnTimerFired(object state)
|
|
|
|
|
{
|
|
|
|
|
var path = Path.Combine(_appPaths.CachePath, "news.json");
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
await DownloadNews(path).ConfigureAwait(false);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.ErrorException("Error downloading news", ex);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task DownloadNews(string path)
|
|
|
|
|
{
|
2014-04-27 03:42:05 +00:00
|
|
|
|
DateTime? lastUpdate = null;
|
|
|
|
|
|
2015-09-13 21:32:02 +00:00
|
|
|
|
if (_fileSystem.FileExists(path))
|
2014-04-27 03:42:05 +00:00
|
|
|
|
{
|
|
|
|
|
lastUpdate = _fileSystem.GetLastWriteTimeUtc(path);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-19 04:25:01 +00:00
|
|
|
|
var requestOptions = new HttpRequestOptions
|
|
|
|
|
{
|
2015-03-21 20:04:00 +00:00
|
|
|
|
Url = "http://emby.media/community/index.php?/blog/rss/1-media-browser-developers-blog",
|
2015-10-02 06:14:04 +00:00
|
|
|
|
Progress = new Progress<double>(),
|
|
|
|
|
UserAgent = "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.42 Safari/537.36"
|
2014-01-19 04:25:01 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
using (var stream = await _httpClient.Get(requestOptions).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
var doc = new XmlDocument();
|
|
|
|
|
doc.Load(stream);
|
|
|
|
|
|
|
|
|
|
var news = ParseRssItems(doc).ToList();
|
|
|
|
|
|
|
|
|
|
_json.SerializeToFile(news, path);
|
2014-04-27 03:42:05 +00:00
|
|
|
|
|
|
|
|
|
await CreateNotifications(news, lastUpdate, CancellationToken.None).ConfigureAwait(false);
|
2014-01-19 04:25:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-27 03:42:05 +00:00
|
|
|
|
private Task CreateNotifications(List<NewsItem> items, DateTime? lastUpdate, CancellationToken cancellationToken)
|
|
|
|
|
{
|
|
|
|
|
if (lastUpdate.HasValue)
|
|
|
|
|
{
|
2014-04-30 15:07:02 +00:00
|
|
|
|
items = items.Where(i => i.Date.ToUniversalTime() >= lastUpdate.Value)
|
2014-04-27 03:42:05 +00:00
|
|
|
|
.ToList();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var tasks = items.Select(i => _notifications.SendNotification(new NotificationRequest
|
|
|
|
|
{
|
|
|
|
|
Date = i.Date,
|
|
|
|
|
Name = i.Title,
|
2015-01-19 20:53:02 +00:00
|
|
|
|
Description = i.Description,
|
|
|
|
|
Url = i.Link,
|
2014-04-27 03:42:05 +00:00
|
|
|
|
UserIds = _userManager.Users.Select(u => u.Id.ToString("N")).ToList()
|
|
|
|
|
|
|
|
|
|
}, cancellationToken));
|
|
|
|
|
|
|
|
|
|
return Task.WhenAll(tasks);
|
|
|
|
|
}
|
|
|
|
|
|
2014-01-19 04:25:01 +00:00
|
|
|
|
private IEnumerable<NewsItem> ParseRssItems(XmlDocument xmlDoc)
|
|
|
|
|
{
|
|
|
|
|
var nodes = xmlDoc.SelectNodes("rss/channel/item");
|
|
|
|
|
|
|
|
|
|
if (nodes != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (XmlNode node in nodes)
|
|
|
|
|
{
|
|
|
|
|
var newsItem = new NewsItem();
|
|
|
|
|
|
|
|
|
|
newsItem.Title = ParseDocElements(node, "title");
|
|
|
|
|
|
|
|
|
|
newsItem.DescriptionHtml = ParseDocElements(node, "description");
|
|
|
|
|
newsItem.Description = newsItem.DescriptionHtml.StripHtml();
|
|
|
|
|
|
|
|
|
|
newsItem.Link = ParseDocElements(node, "link");
|
|
|
|
|
|
|
|
|
|
var date = ParseDocElements(node, "pubDate");
|
|
|
|
|
DateTime parsedDate;
|
|
|
|
|
|
|
|
|
|
if (DateTime.TryParse(date, out parsedDate))
|
|
|
|
|
{
|
|
|
|
|
newsItem.Date = parsedDate;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
yield return newsItem;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string ParseDocElements(XmlNode parent, string xPath)
|
|
|
|
|
{
|
|
|
|
|
var node = parent.SelectSingleNode(xPath);
|
|
|
|
|
|
|
|
|
|
return node != null ? node.InnerText : string.Empty;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
|
|
|
|
if (_timer != null)
|
|
|
|
|
{
|
|
|
|
|
_timer.Dispose();
|
|
|
|
|
_timer = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|