NewsEntryPoint.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using MediaBrowser.Common.Configuration;
  2. using MediaBrowser.Common.Extensions;
  3. using MediaBrowser.Common.IO;
  4. using MediaBrowser.Common.Net;
  5. using MediaBrowser.Controller.Library;
  6. using MediaBrowser.Controller.Notifications;
  7. using MediaBrowser.Controller.Plugins;
  8. using MediaBrowser.Model.Logging;
  9. using MediaBrowser.Model.News;
  10. using MediaBrowser.Model.Notifications;
  11. using MediaBrowser.Model.Serialization;
  12. using System;
  13. using System.Collections.Generic;
  14. using System.IO;
  15. using System.Linq;
  16. using System.Threading;
  17. using System.Threading.Tasks;
  18. using System.Xml;
  19. namespace MediaBrowser.Server.Implementations.News
  20. {
  21. public class NewsEntryPoint : IServerEntryPoint
  22. {
  23. private Timer _timer;
  24. private readonly IHttpClient _httpClient;
  25. private readonly IApplicationPaths _appPaths;
  26. private readonly IFileSystem _fileSystem;
  27. private readonly ILogger _logger;
  28. private readonly IJsonSerializer _json;
  29. private readonly INotificationManager _notifications;
  30. private readonly IUserManager _userManager;
  31. private readonly TimeSpan _frequency = TimeSpan.FromHours(24);
  32. public NewsEntryPoint(IHttpClient httpClient, IApplicationPaths appPaths, IFileSystem fileSystem, ILogger logger, IJsonSerializer json, INotificationManager notifications, IUserManager userManager)
  33. {
  34. _httpClient = httpClient;
  35. _appPaths = appPaths;
  36. _fileSystem = fileSystem;
  37. _logger = logger;
  38. _json = json;
  39. _notifications = notifications;
  40. _userManager = userManager;
  41. }
  42. public void Run()
  43. {
  44. _timer = new Timer(OnTimerFired, null, TimeSpan.FromMilliseconds(500), _frequency);
  45. }
  46. /// <summary>
  47. /// Called when [timer fired].
  48. /// </summary>
  49. /// <param name="state">The state.</param>
  50. private async void OnTimerFired(object state)
  51. {
  52. var path = Path.Combine(_appPaths.CachePath, "news.json");
  53. try
  54. {
  55. await DownloadNews(path).ConfigureAwait(false);
  56. }
  57. catch (Exception ex)
  58. {
  59. _logger.ErrorException("Error downloading news", ex);
  60. }
  61. }
  62. private async Task DownloadNews(string path)
  63. {
  64. DateTime? lastUpdate = null;
  65. if (File.Exists(path))
  66. {
  67. lastUpdate = _fileSystem.GetLastWriteTimeUtc(path);
  68. }
  69. var requestOptions = new HttpRequestOptions
  70. {
  71. Url = "http://emby.media/community/index.php?/blog/rss/1-media-browser-developers-blog",
  72. Progress = new Progress<double>()
  73. };
  74. using (var stream = await _httpClient.Get(requestOptions).ConfigureAwait(false))
  75. {
  76. var doc = new XmlDocument();
  77. doc.Load(stream);
  78. var news = ParseRssItems(doc).ToList();
  79. _json.SerializeToFile(news, path);
  80. await CreateNotifications(news, lastUpdate, CancellationToken.None).ConfigureAwait(false);
  81. }
  82. }
  83. private Task CreateNotifications(List<NewsItem> items, DateTime? lastUpdate, CancellationToken cancellationToken)
  84. {
  85. if (lastUpdate.HasValue)
  86. {
  87. items = items.Where(i => i.Date.ToUniversalTime() >= lastUpdate.Value)
  88. .ToList();
  89. }
  90. var tasks = items.Select(i => _notifications.SendNotification(new NotificationRequest
  91. {
  92. Date = i.Date,
  93. Name = i.Title,
  94. Description = i.Description,
  95. Url = i.Link,
  96. UserIds = _userManager.Users.Select(u => u.Id.ToString("N")).ToList()
  97. }, cancellationToken));
  98. return Task.WhenAll(tasks);
  99. }
  100. private IEnumerable<NewsItem> ParseRssItems(XmlDocument xmlDoc)
  101. {
  102. var nodes = xmlDoc.SelectNodes("rss/channel/item");
  103. if (nodes != null)
  104. {
  105. foreach (XmlNode node in nodes)
  106. {
  107. var newsItem = new NewsItem();
  108. newsItem.Title = ParseDocElements(node, "title");
  109. newsItem.DescriptionHtml = ParseDocElements(node, "description");
  110. newsItem.Description = newsItem.DescriptionHtml.StripHtml();
  111. newsItem.Link = ParseDocElements(node, "link");
  112. var date = ParseDocElements(node, "pubDate");
  113. DateTime parsedDate;
  114. if (DateTime.TryParse(date, out parsedDate))
  115. {
  116. newsItem.Date = parsedDate;
  117. }
  118. yield return newsItem;
  119. }
  120. }
  121. }
  122. private string ParseDocElements(XmlNode parent, string xPath)
  123. {
  124. var node = parent.SelectSingleNode(xPath);
  125. return node != null ? node.InnerText : string.Empty;
  126. }
  127. public void Dispose()
  128. {
  129. if (_timer != null)
  130. {
  131. _timer.Dispose();
  132. _timer = null;
  133. }
  134. }
  135. }
  136. }