From 9163fcf2ae70c7849f9308a804942f55c3b7908e Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Thu, 12 Jul 2018 10:57:33 +0300 Subject: [PATCH] DownloadAgent fixes and improvements --- FoxTube.Background/BackgroundProcessor.cs | 57 ++++++++++++++-- FoxTube.Background/FoxTube.Background.csproj | 16 ++++- FoxTube.Background/Notification.cs | 66 ++++++++++++++++++- FoxTube.Background/ToastTemplates.cs | 45 ------------- .../{Controls => Classes}/DownloadAgent.cs | 19 ++++-- FoxTube/Classes/ObjectEventArgs.cs | 4 +- FoxTube/Classes/SecretsVault.cs | 34 +++------- FoxTube/Controls/DownloadItem.xaml.cs | 9 ++- FoxTube/FoxTube.csproj | 2 +- FoxTube/Pages/Downloads.xaml.cs | 14 +++- FoxTube/Pages/MainPage.xaml.cs | 4 -- 11 files changed, 172 insertions(+), 98 deletions(-) delete mode 100644 FoxTube.Background/ToastTemplates.cs rename FoxTube/{Controls => Classes}/DownloadAgent.cs (66%) diff --git a/FoxTube.Background/BackgroundProcessor.cs b/FoxTube.Background/BackgroundProcessor.cs index 152477a..8f44e93 100644 --- a/FoxTube.Background/BackgroundProcessor.cs +++ b/FoxTube.Background/BackgroundProcessor.cs @@ -1,8 +1,13 @@ -using System; +using Google.Apis.Auth.OAuth2; +using Google.Apis.Services; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; +using System.Threading; using System.Threading.Tasks; using System.Xml; using Windows.ApplicationModel.Background; @@ -15,14 +20,23 @@ namespace FoxTube.Background { public List Notifications = new List(); - private DateTime lastCheck; + private DateTime lastCheck = DateTime.Now; private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; + private ClientSecrets Secrets => new ClientSecrets() + { + ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com", + ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_" + }; + BackgroundTaskDeferral def; public async void Run(IBackgroundTaskInstance taskInstance) { - XmlDocument doc = new XmlDocument(); def = taskInstance.GetDeferral(); + XmlDocument doc = new XmlDocument(); + if (settings.Values["lastCheck"] == null) + settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now)); + else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified); if (settings.Values["notificationsHistory"] != null) doc.LoadXml(settings.Values["notificationsHistory"] as string); @@ -40,16 +54,49 @@ namespace FoxTube.Background def.Complete(); } - void CheckAccount() + async void CheckAccount() { + UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None); + if (credential == null) + return; + SubscriptionsResource.ListRequest subRequest = new YouTubeService(new BaseClientService.Initializer() + { + HttpClientInitializer = credential, + ApplicationName = "FoxTube" + }).Subscriptions.List("snippet"); + subRequest.Mine = true; + subRequest.MaxResults = 50; + SubscriptionListResponse subResponse = await subRequest.ExecuteAsync(); + List subs = new List(); + + foreach (Subscription s in subResponse.Items) + subs.Add(s.Snippet.ResourceId.ChannelId); + + string nextToken = subResponse.NextPageToken; + while (nextToken != null) + { + subRequest.PageToken = nextToken; + subResponse = await subRequest.ExecuteAsync(); + foreach (Subscription s in subResponse.Items) + subs.Add(s.Snippet.ResourceId.ChannelId); + } + + foreach(string s in subs) + { + var request = new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", + ApplicationName = "FoxTube" + }).Activities.List("snippet"); + } } void CheckAnnouncements() { XmlDocument doc = new XmlDocument(); doc.Load(XmlReader.Create("http://foxgame.hol.es/ftp.xml")); - if ((XmlConvert.ToDateTime((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), XmlDateTimeSerializationMode.Utc) - DateTime.UtcNow).TotalSeconds > 0) + if ((XmlConvert.ToDateTime((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), XmlDateTimeSerializationMode.Utc) - lastCheck).TotalSeconds > 0) Notifications.Add(new Notification(NotificationType.Internal, doc["posts"].FirstChild["notificationHeader"].InnerText, doc["posts"].FirstChild["content"].InnerText, diff --git a/FoxTube.Background/FoxTube.Background.csproj b/FoxTube.Background/FoxTube.Background.csproj index 1793119..d6f90da 100644 --- a/FoxTube.Background/FoxTube.Background.csproj +++ b/FoxTube.Background/FoxTube.Background.csproj @@ -110,9 +110,23 @@ - + + 1.30.0-beta02 + + + 1.30.0-beta02 + + + 1.30.0-beta02 + + + 1.29.2.994 + + + 1.29.2.1006 + 6.1.5 diff --git a/FoxTube.Background/Notification.cs b/FoxTube.Background/Notification.cs index eccc230..2e2ac0d 100644 --- a/FoxTube.Background/Notification.cs +++ b/FoxTube.Background/Notification.cs @@ -39,6 +39,12 @@ namespace FoxTube.Background Thumbnail = thumbnailUrl; } + public string GetXml() + { + + return string.Empty; + } + public UIElement GetNotification() { StackPanel stackPanel = new StackPanel() { Margin = new Thickness(10, 0, 0, 0) }; @@ -155,17 +161,71 @@ namespace FoxTube.Background break; case NotificationType.Video: - template.LoadXml($@" {Channel} uploaded a new video {Content} "); + template.LoadXml($@" + + + + {Channel} uploaded a new video + {Content} + + + + + + + + + "); break; case NotificationType.Internal: string thumb1 = string.IsNullOrWhiteSpace(Thumbnail) ? "Assets/AnnouncementThumb.png" : Thumbnail; - template.LoadXml($@" {Channel} {Content} "); + template.LoadXml($@" + + + + + {Channel} + {Content} + + + + + + + + "); break; case NotificationType.Post: string thumb2 = string.IsNullOrWhiteSpace(Thumbnail) ? "" : $""; - template.LoadXml($@" {thumb2} {Channel} {Content} "); + template.LoadXml($@" + + + {thumb2} + + {Channel} + {Content} + + + + + + + + + + "); break; } diff --git a/FoxTube.Background/ToastTemplates.cs b/FoxTube.Background/ToastTemplates.cs deleted file mode 100644 index cf6483a..0000000 --- a/FoxTube.Background/ToastTemplates.cs +++ /dev/null @@ -1,45 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace FoxTube.Background -{ - public static class ToastTemplates - { - public static string Comment = - @" - - - - [ChannelName] posted a new comment - [VideoName] - - - - - - - - - - - - "; - public static string Video = - @" [ChannelName] uploaded a new video [VideoName] "; - public static string Post = - @" [ChannelName] [PostContent] "; - public static string Internal = - @" [Header] "; - public static string Download = - @" Downloading a video... [VideoName] "; - public static string DownloadComplete = - @" Download complete Subnautica - SAY GOODBYE TO SUBNAUTICA! We're Back Home! - Subnautica Ending (Full Release Gameplay) "; - } -} diff --git a/FoxTube/Controls/DownloadAgent.cs b/FoxTube/Classes/DownloadAgent.cs similarity index 66% rename from FoxTube/Controls/DownloadAgent.cs rename to FoxTube/Classes/DownloadAgent.cs index 511fdb3..2e0ca64 100644 --- a/FoxTube/Controls/DownloadAgent.cs +++ b/FoxTube/Classes/DownloadAgent.cs @@ -6,24 +6,26 @@ using System.Threading.Tasks; using System.Xml; using System.IO; using MyToolkit.Multimedia; +using Windows.Storage; namespace FoxTube.Controls { public class DownloadAgent { public List Items = new List(); + public event ObjectEventHandler ListChanged; + private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; XmlDocument doc = new XmlDocument(); - string path = $@"{Directory.GetCurrentDirectory()}\DownloadHistory.xml"; public DownloadAgent() { - if (File.Exists(path)) - doc.Load(path); + if (settings.Values["downloadHistory"] != null) + doc.LoadXml(settings.Values["downloadHistory"] as string); else { doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null)); doc.AppendChild(doc.CreateElement("downloads")); - doc.Save(path); + settings.Values.Add("downloadHistory", doc.InnerXml); } foreach(XmlElement e in doc["downloads"].ChildNodes) @@ -48,16 +50,19 @@ namespace FoxTube.Controls item.DownloadComplete += Item_DownloadComplete; Items.Add(item); + ListChanged.Invoke(item, new ObjectEventArgs("add")); } - private void Item_DownloadComplete(object sender, EventArgs e) + private void Item_DownloadComplete(object sender, ObjectEventArgs e) { - doc["downloads"].AppendChild((e as ObjectEventArgs).Parameters[0] as XmlElement); + doc["downloads"].InnerXml += e.Parameters[0]; + settings.Values["downloadHistory"] = doc.InnerXml; } - private void Item_DownloadCanceled(object sender, EventArgs e) + private void Item_DownloadCanceled(object sender, ObjectEventArgs e) { Items.Remove(sender as DownloadItem); + ListChanged.Invoke(sender, new ObjectEventArgs("remove")); } } } diff --git a/FoxTube/Classes/ObjectEventArgs.cs b/FoxTube/Classes/ObjectEventArgs.cs index d5716c0..8f80950 100644 --- a/FoxTube/Classes/ObjectEventArgs.cs +++ b/FoxTube/Classes/ObjectEventArgs.cs @@ -6,7 +6,9 @@ using System.Threading.Tasks; namespace FoxTube { - public class ObjectEventArgs : EventArgs + public delegate void ObjectEventHandler(object sender, ObjectEventArgs args); + + public class ObjectEventArgs { public List Parameters = new List(); public ObjectEventArgs(params object[] args) diff --git a/FoxTube/Classes/SecretsVault.cs b/FoxTube/Classes/SecretsVault.cs index 332c55f..14cbb6b 100644 --- a/FoxTube/Classes/SecretsVault.cs +++ b/FoxTube/Classes/SecretsVault.cs @@ -1,30 +1,14 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.UI.Xaml; -using Windows.UI.Xaml.Controls; -using System.Diagnostics; -using System.Text.RegularExpressions; -using Windows.UI.Xaml.Media; -using Windows.UI; -using Windows.UI.Xaml.Documents; using System.Net; using System.Threading; -using Windows.Data.Json; -using Windows.Storage.Streams; -using Windows.Security.Cryptography; -using Windows.Security.Cryptography.Core; using Google.Apis.Auth.OAuth2; using Google.Apis.Auth.OAuth2.Flows; using Google.Apis.Services; -using Google.Apis.Util.Store; using Google.Apis.YouTube.v3; -using Google.Apis.Auth.OAuth2.Responses; -using Windows.Storage; using Google.Apis.YouTube.v3.Data; +using Windows.Storage; namespace FoxTube { @@ -32,7 +16,7 @@ namespace FoxTube { #region Static Information public static NetworkCredential EmailCredential => new NetworkCredential("youwillneverknowthisadress@gmail.com", "thisisthepassword12345"); - private static ClientSecrets Secrets => new ClientSecrets() + public static ClientSecrets Secrets => new ClientSecrets() { ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com", ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_" @@ -70,6 +54,7 @@ namespace FoxTube public Google.Apis.YouTube.v3.Data.Channel channel; public UserCredential Credential; public event EventHandler AuthorizationStateChanged; + private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; public async void Authorize() { @@ -77,6 +62,9 @@ namespace FoxTube catch { } if(Credential != null) { + if (settings.Values["authorized"] == null) + settings.Values.Add("authorized", true); + else settings.Values["authorized"] = true; IsLoged = true; AuthorizationStateChanged.Invoke(this, null); } @@ -139,17 +127,13 @@ namespace FoxTube { Credential = null; AuthorizationStateChanged.Invoke(this, null); + settings.Values["authorized"] = false; } } - public async void CheckAuthorization() + public void CheckAuthorization() { - var token = await new AuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer() - { - ClientSecrets = Secrets, - DataStore = null - }).LoadTokenAsync("user", CancellationToken.None); - if (token == null) + if (settings.Values["authorized"] == null || !(bool)settings.Values["authorized"]) IsLoged = false; else Authorize(); diff --git a/FoxTube/Controls/DownloadItem.xaml.cs b/FoxTube/Controls/DownloadItem.xaml.cs index b5d114f..c782c96 100644 --- a/FoxTube/Controls/DownloadItem.xaml.cs +++ b/FoxTube/Controls/DownloadItem.xaml.cs @@ -31,8 +31,8 @@ namespace FoxTube.Controls string uri; string Id; - public event EventHandler DownloadCanceled; - public event EventHandler DownloadComplete; + public event ObjectEventHandler DownloadCanceled; + public event ObjectEventHandler DownloadComplete; WebClient client = new WebClient(); @@ -91,9 +91,8 @@ namespace FoxTube.Controls { progressPanel.Visibility = Visibility.Collapsed; donePanel.Visibility = Visibility.Visible; - - XmlElement node = new XmlDocument().CreateElement("item"); - node.InnerXml = $@" + + string node = $@" {quality.Text.Split(' ')[1]} diff --git a/FoxTube/FoxTube.csproj b/FoxTube/FoxTube.csproj index 0d81508..53fc06e 100644 --- a/FoxTube/FoxTube.csproj +++ b/FoxTube/FoxTube.csproj @@ -101,7 +101,7 @@ CommentCard.xaml - + DownloadItem.xaml diff --git a/FoxTube/Pages/Downloads.xaml.cs b/FoxTube/Pages/Downloads.xaml.cs index 9330036..b10cc65 100644 --- a/FoxTube/Pages/Downloads.xaml.cs +++ b/FoxTube/Pages/Downloads.xaml.cs @@ -18,6 +18,7 @@ using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Navigation; // The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238 +#pragma warning disable CS0252 // Possible unintended reference comparison; left hand side needs cast namespace FoxTube.Pages { @@ -27,15 +28,26 @@ namespace FoxTube.Pages public sealed partial class Downloads : Page { ApplicationDataContainer settings = ApplicationData.Current.LocalSettings; + DownloadAgent agent = Methods.MainPage.Agent; public Downloads() { this.InitializeComponent(); path.Text = settings.Values["defaultDownload"] as string; - foreach (DownloadItem item in Methods.MainPage.Agent.Items) + agent.ListChanged += UpdateList; + + foreach (DownloadItem item in agent.Items) stack.Children.Add(item); } + private void UpdateList(object sender, ObjectEventArgs e) + { + if (e.Parameters[0] == "remove") + stack.Children.Remove(sender as DownloadItem); + else if (e.Parameters[0] == "add") + stack.Children.Add(sender as DownloadItem); + } + private async void changePath_Click(object sender, RoutedEventArgs e) { FolderPicker picker = new FolderPicker() diff --git a/FoxTube/Pages/MainPage.xaml.cs b/FoxTube/Pages/MainPage.xaml.cs index 5f48d9b..f7e14da 100644 --- a/FoxTube/Pages/MainPage.xaml.cs +++ b/FoxTube/Pages/MainPage.xaml.cs @@ -47,10 +47,6 @@ namespace FoxTube /// public enum RightPaneState { Full, Collapsed, Hidden } - public interface IPageNesting - { - void GoToSettings(int pageIndex = 0); - } public sealed partial class MainPage : Page {