1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
|
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Library;
using MediaBrowser.Controller.Notifications;
using MediaBrowser.Controller.Plugins;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.News;
using MediaBrowser.Model.Notifications;
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;
using CommonIO;
using MediaBrowser.Common.Threading;
namespace MediaBrowser.Server.Implementations.News
{
public class NewsEntryPoint : IServerEntryPoint
{
private PeriodicTimer _timer;
private readonly IHttpClient _httpClient;
private readonly IApplicationPaths _appPaths;
private readonly IFileSystem _fileSystem;
private readonly ILogger _logger;
private readonly IJsonSerializer _json;
private readonly INotificationManager _notifications;
private readonly IUserManager _userManager;
private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
public NewsEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IJsonSerializer json, INotificationManager notifications, IUserManager userManager)
{
_httpClient = httpClient;
_appPaths = appPaths;
_fileSystem = fileSystem;
_logger = logger;
_json = json;
_notifications = notifications;
_userManager = userManager;
}
public void Run()
{
_timer = new PeriodicTimer(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)
{
DateTime? lastUpdate = null;
if (_fileSystem.FileExists(path))
{
lastUpdate = _fileSystem.GetLastWriteTimeUtc(path);
}
var requestOptions = new HttpRequestOptions
{
Url = "http://emby.media/community/index.php?/blog/rss/1-media-browser-developers-blog",
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"
};
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);
await CreateNotifications(news, lastUpdate, CancellationToken.None).ConfigureAwait(false);
}
}
private Task CreateNotifications(List<NewsItem> items, DateTime? lastUpdate, CancellationToken cancellationToken)
{
if (lastUpdate.HasValue)
{
items = items.Where(i => i.Date.ToUniversalTime() >= lastUpdate.Value)
.ToList();
}
var tasks = items.Select(i => _notifications.SendNotification(new NotificationRequest
{
Date = i.Date,
Name = i.Title,
Description = i.Description,
Url = i.Link,
UserIds = _userManager.Users.Select(u => u.Id.ToString("N")).ToList()
}, cancellationToken));
return Task.WhenAll(tasks);
}
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;
}
}
}
}
|