using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using System.Threading.Tasks; using System.Xml; using Windows.ApplicationModel.Background; using Windows.Storage; using Windows.UI.Notifications; using YoutubeExplode.Models; namespace FoxTube.Background { public sealed class BackgroundProcessor : IBackgroundTask { private DateTime lastCheck; private readonly ApplicationDataContainer settings = ApplicationData.Current.RoamingSettings; dynamic prefs; BackgroundTaskDeferral def; public async void Run(IBackgroundTaskInstance taskInstance) { try { def = taskInstance.GetDeferral(); if (settings.Values["lastCheck"] == null) { settings.Values["lastCheck"] = DateTime.Now.ToString(); def.Complete(); return; } else lastCheck = DateTime.Parse(settings.Values["lastCheck"] as string); prefs = JsonConvert.DeserializeObject(settings.Values["settings"] as string); if ((bool)prefs.devNotifications) CheckAnnouncements(); if ((bool)prefs.videoNotifications) await CheckAccount(); } finally { settings.Values["lastCheck"] = DateTime.Now.ToString(); def.Complete(); } } async Task CheckAccount() { try { Dictionary subscriptions = JsonConvert.DeserializeObject>(settings.Values["subscriptions"] as string); List results = new List(); foreach (var s in subscriptions) { XmlDocument doc = new XmlDocument(); doc.LoadXml(await new HttpClient().GetStringAsync($"https://www.youtube.com/feeds/videos.xml?channel_id={s.Key}")); List items = new List(); foreach (XmlElement i in doc["feed"].ChildNodes) if (i.Name == "entry" && DateTime.Parse(i["published"].InnerText) > lastCheck) items.Add(i); items.OrderByDescending(i => DateTime.Parse(i["published"].InnerText)); foreach(XmlElement i in items) { results.Add(i); ToastNotificationManager.CreateToastNotifier().Show( Notification.GetVideoToast(i["yt:videoId"].InnerText, s.Key, i["title"].InnerText, i["author"]["name"].InnerText, (await new YoutubeExplode.YoutubeClient().GetVideoAsync(i["yt:videoId"].InnerText)).Thumbnails.MediumResUrl, DateTime.Parse(i["published"].InnerText), s.Value)); } } TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication(); updater.EnableNotificationQueue(true); updater.Clear(); for (int i = 0; i < 5 && i < results.Count; i++) updater.Update(Tiles.GetTileLayout(System.Security.SecurityElement.Escape(results[i]["title"].InnerText), System.Security.SecurityElement.Escape(results[i]["author"]["name"].InnerText), (await new YoutubeExplode.YoutubeClient().GetVideoAsync(results[i]["yt:videoId"].InnerText)).Thumbnails.MediumResUrl.Replace("&", "%26"), subscriptions[results[i]["author"]["url"].InnerText.Split('/').Last()])); } catch { } } async void CheckAnnouncements() { try { XmlDocument doc = new XmlDocument(); doc.LoadXml(await new HttpClient().GetStringAsync("http://foxgame-studio.000webhostapp.com/foxtube-messages.xml")); XmlElement item = doc["posts"].FirstChild as XmlElement; DateTime date = DateTime.Parse(item.GetAttribute("time")); if (date > lastCheck && date < DateTime.Now) ToastNotificationManager.CreateToastNotifier().Show( Notification.GetInternalToast(item["id"].InnerText, item["header"][(string)prefs.language].InnerText, item["content"][(string)prefs.language].InnerText, item["thumbnail"].InnerText, item["avatar"].InnerText)); } catch { } } } }