Archived
1
0

Development 110218

This commit is contained in:
Michael Gordeev
2018-11-02 10:33:34 +03:00
parent 3126b9cf19
commit bba4fe4f00
16 changed files with 400 additions and 125 deletions
+29 -36
View File
@@ -2,8 +2,10 @@
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;
@@ -34,56 +36,43 @@ namespace FoxTube.Background
});
BackgroundTaskDeferral def;
public void Run(IBackgroundTaskInstance taskInstance)
public async void Run(IBackgroundTaskInstance taskInstance)
{
def = taskInstance.GetDeferral();
if (settings.Values["lastCheck"] == null)
settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss"));
else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified);
if((bool)settings.Values["newVideoNotification"])
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<bool[]>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json")));
if(notificationsSettings[0])
CheckAnnouncements();
if((bool)settings.Values["devNews"])
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
{
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();
Dictionary<string, string> subs = new Dictionary<string, string>();
foreach (Subscription s in subResponse.Items)
subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url);
string nextToken = subResponse.NextPageToken;
while (nextToken != null)
{
subRequest.PageToken = nextToken;
subResponse = await subRequest.ExecuteAsync();
nextToken = subResponse.NextPageToken;
foreach (Subscription s in subResponse.Items)
subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url);
}
foreach (var s in subs)
ToastNotificationManager.CreateToastNotifier().Show(Notification.GetInternalToast(null, "Checking videos...", DateTime.Now.ToString(), null, null));
Dictionary<string, string> subscriptions = JsonConvert.DeserializeObject<Dictionary<string, string>>(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;
@@ -95,9 +84,13 @@ namespace FoxTube.Background
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()