using Google.Apis.Auth.OAuth2; using Google.Apis.Services; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; using Newtonsoft.Json; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Xml; using Windows.ApplicationModel.Background; using Windows.Storage; using Windows.UI.Notifications; namespace FoxTube.Background { public sealed class BackgroundProcessor : IBackgroundTask { private DateTime lastCheck = DateTime.Now; private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; private ClientSecrets Secrets => new ClientSecrets() { ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com", ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_" }; private YouTubeService Service => new YouTubeService(new BaseClientService.Initializer() { ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", ApplicationName = "FoxTube" }); BackgroundTaskDeferral def; public async void Run(IBackgroundTaskInstance taskInstance) { def = taskInstance.GetDeferral(); try { if (settings.Values["lastCheck"] == null) settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now)); else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified); } catch { lastCheck = DateTime.Now; } bool[] notificationsSettings = JsonConvert.DeserializeObject(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json"))); if(notificationsSettings[0]) CheckAnnouncements(); if(notificationsSettings[1]) CheckAccount(); settings.Values["lastCheck"] = XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss"); def.Complete(); ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Background task complete", DateTime.Now.ToString(), null, null)); } async void CheckAccount() { try { ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Checking videos...", DateTime.Now.ToString(), null, null)); Dictionary subscriptions = JsonConvert.DeserializeObject>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("background.json"))); Debug.WriteLine("Subscriptions list"); foreach (var i in subscriptions) Debug.WriteLine($"{i.Key}: {i.Value}"); Debug.WriteLine("Subscriptions list end"); foreach (var s in subscriptions) { SearchResource.ListRequest request = Service.Search.List("snippet"); request.PublishedAfter = lastCheck; request.ChannelId = s.Key; request.Type = "video"; request.MaxResults = 5; SearchListResponse response = await request.ExecuteAsync(); foreach (var i in response.Items) ToastNotificationManager.CreateToastNotifier().Show( Notification.GetVideoToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title, i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, s.Value)); ToastNotificationManager.CreateToastNotifier().Show(Notification.GetVideoToast(null, s.Key, DateTime.Now.ToString(), s.Key, null, s.Value)); } } catch { } ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "New videos checked", DateTime.Now.ToString(), null, null)); } void CheckAnnouncements() { try { XmlDocument doc = new XmlDocument(); doc.Load(XmlReader.Create("http://foxgame.hol.es/foxtube-messages.xml")); if ((XmlConvert.ToDateTimeOffset((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), "YYYY-MM-DDThh:mm:ss") - lastCheck.ToUniversalTime()).TotalSeconds > 0) ToastNotificationManager.CreateToastNotifier().Show( Notification.GetInternalToast(doc["posts"].FirstChild["id"].InnerText, doc["posts"].FirstChild["header"].InnerText, doc["posts"].FirstChild["content"].InnerText, doc["posts"].FirstChild["thumbnail"].InnerText, doc["posts"].FirstChild["avatar"].InnerText)); } catch { } } } }