diff --git a/FoxTube.Background/BackgroundProcessor.cs b/FoxTube.Background/BackgroundProcessor.cs index 3819038..34b55d2 100644 --- a/FoxTube.Background/BackgroundProcessor.cs +++ b/FoxTube.Background/BackgroundProcessor.cs @@ -1,4 +1,6 @@ -using Google.Apis.Services; +using Google.Apis.Auth.OAuth2; +using Google.Apis.Oauth2.v2; +using Google.Apis.Services; using Google.Apis.YouTube.v3; using Google.Apis.YouTube.v3.Data; using Newtonsoft.Json; @@ -6,6 +8,7 @@ using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; +using System.Threading; using System.Threading.Tasks; using System.Xml; using Windows.ApplicationModel.Background; @@ -19,11 +22,6 @@ namespace FoxTube.Background private DateTime lastCheck; private readonly ApplicationDataContainer settings = ApplicationData.Current.RoamingSettings; dynamic prefs; - private readonly YouTubeService Service = new YouTubeService(new BaseClientService.Initializer() - { - ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", - ApplicationName = "FoxTube" - }); BackgroundTaskDeferral def; public async void Run(IBackgroundTaskInstance taskInstance) @@ -44,7 +42,7 @@ namespace FoxTube.Background prefs = JsonConvert.DeserializeObject(settings.Values["settings"] as string); if ((bool)prefs.devNotifications) CheckAnnouncements(); - if ((bool)prefs.videoNotifications) + if ((bool)prefs.videoNotifications && (bool)prefs.hasAccount) await CheckAccount(); } finally @@ -58,15 +56,59 @@ namespace FoxTube.Background { try { - Dictionary subscriptions = JsonConvert.DeserializeObject>(settings.Values["subscriptions"] as string); + UserCredential Credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( + new ClientSecrets + { + ClientId = "349735264870-2ekqlm0a4mkg3mmrfcv90s3qp3o15dq0.apps.googleusercontent.com", + ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_" + }, + new[] + { + Oauth2Service.Scope.UserinfoProfile, + Oauth2Service.Scope.UserinfoEmail, + YouTubeService.Scope.YoutubeForceSsl, + YouTubeService.Scope.Youtube, + YouTubeService.Scope.YoutubeUpload, + YouTubeService.Scope.YoutubeReadonly, + YouTubeService.Scope.Youtubepartner + }, + "user", + CancellationToken.None); + if (Credential == null) + return; + + YouTubeService service = new YouTubeService(new BaseClientService.Initializer + { + HttpClientInitializer = Credential, + ApplicationName = "FoxTube" + }); + + List subscriptions = new List(); List results = new List(); - foreach (var s in subscriptions) + SubscriptionsResource.ListRequest subRequest = service.Subscriptions.List("snippet"); + subRequest.Mine = true; + subRequest.MaxResults = 50; + subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance; + SubscriptionListResponse subResponse; + string nextToken = null; + + do { - SearchResource.ListRequest request = Service.Search.List("snippet"); - request.PublishedAfter = lastCheck; - request.ChannelId = s.Key; + subRequest.PageToken = nextToken; + subResponse = await subRequest.ExecuteAsync(); + foreach (Subscription s in subResponse.Items) + subscriptions.Add(s); + nextToken = subResponse.NextPageToken; + + } while (!string.IsNullOrWhiteSpace(nextToken)); + + foreach (Subscription item in subscriptions) + { + SearchResource.ListRequest request = service.Search.List("snippet"); + request.PublishedAfter = lastCheck.Subtract(TimeSpan.FromDays(1)); + request.ChannelId = item.Snippet.ResourceId.ChannelId; request.Type = "video"; request.MaxResults = 5; SearchListResponse response = await request.ExecuteAsync(); @@ -75,15 +117,15 @@ namespace FoxTube.Background { results.Add(i); - if(i.Snippet.LiveBroadcastContent == "live") + if (i.Snippet.LiveBroadcastContent == "live") ToastNotificationManager.CreateToastNotifier().Show( - Notification.GetStreamToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, s.Value)); - else if(i.Snippet.LiveBroadcastContent == "upcoming") + Notification.GetStreamToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, item.Snippet.Thumbnails.Default__.Url)); + else if (i.Snippet.LiveBroadcastContent == "upcoming") ToastNotificationManager.CreateToastNotifier().Show( - Notification.GetUpcomingToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, s.Value)); + Notification.GetUpcomingToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, item.Snippet.Thumbnails.Default__.Url)); else ToastNotificationManager.CreateToastNotifier().Show( - Notification.GetVideoToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, s.Value)); + Notification.GetVideoToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title.ConvertEscapeSymbols(), i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, i.Snippet.PublishedAt.Value, item.Snippet.Thumbnails.Default__.Url)); } } @@ -93,7 +135,7 @@ namespace FoxTube.Background 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].Snippet.Title.ConvertEscapeSymbols()), System.Security.SecurityElement.Escape(results[i].Snippet.ChannelTitle), results[i].Snippet.Thumbnails.Medium.Url.Replace("&", "%26"), subscriptions[results[i].Snippet.ChannelId])); + updater.Update(Tiles.GetTileLayout(System.Security.SecurityElement.Escape(results[i].Snippet.Title.ConvertEscapeSymbols()), System.Security.SecurityElement.Escape(results[i].Snippet.ChannelTitle), results[i].Snippet.Thumbnails.Medium.Url.Replace("&", "%26"), subscriptions.Find(x => x.Snippet.ResourceId.ChannelId == results[i].Snippet.ChannelId).Snippet.Thumbnails.Medium.Url)); } catch { } } diff --git a/FoxTube.Background/ResourceCreators.cs b/FoxTube.Background/ResourceCreators.cs index 254f1ff..b5fd2a0 100644 --- a/FoxTube.Background/ResourceCreators.cs +++ b/FoxTube.Background/ResourceCreators.cs @@ -16,7 +16,8 @@ namespace FoxTube.Background private static Dictionary LoadPack() { dynamic saved = JsonConvert.DeserializeObject(ApplicationData.Current.RoamingSettings.Values["settings"] as string); - if (saved.language as string == "ru-RU") + string hl = saved.language; + if (hl == "ru-RU") return new Dictionary() { { "addLater", "Посмотреть позже" },