Archived
1
0

Notificaions development 0

This commit is contained in:
Michael Gordeev
2018-10-24 16:46:22 +03:00
parent 4cb9bdd8cb
commit dc8afa2891
10 changed files with 165 additions and 335 deletions
+54 -90
View File
@@ -16,13 +16,8 @@ using Windows.UI.Notifications;
namespace FoxTube.Background
{
public delegate void NotificationHandler(object sender, Notification item);
public sealed class BackgroundProcessor : IBackgroundTask
{
public static event NotificationHandler NotificationRecieved;
List<Notification> Notifications = new List<Notification>();
private DateTime lastCheck = DateTime.Now;
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
@@ -37,8 +32,6 @@ namespace FoxTube.Background
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
ApplicationName = "FoxTube"
});
XmlDocument doc = new XmlDocument();
BackgroundTaskDeferral def;
public void Run(IBackgroundTaskInstance taskInstance)
@@ -47,110 +40,81 @@ namespace FoxTube.Background
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"])
CheckAnnouncements();
if((bool)settings.Values["devNews"])
CheckAccount();
try
{
doc.LoadXml(settings.Values["notificationsHistory"] as string);
}
catch
{
return;
}
CheckAnnouncements();
CheckAccount();
SendNSave();
settings.Values["lastCheck"] = XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss");
def.Complete();
}
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()
try
{
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>();
UserCredential credential = await GoogleWebAuthorizationBroker.AuthorizeAsync(Secrets, new[] { Google.Apis.Oauth2.v2.Oauth2Service.Scope.UserinfoProfile, YouTubeService.Scope.YoutubeForceSsl }, "user", CancellationToken.None);
if (credential == null)
return;
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;
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);
}
foreach(var s in subs)
{
SearchResource.ListRequest request = Service.Search.List("snippet");
request.PublishedAfter = lastCheck;
request.ChannelId = s.Key;
request.Type = "video";
SearchListResponse response = await request.ExecuteAsync();
string nextToken = subResponse.NextPageToken;
while (nextToken != null)
{
subRequest.PageToken = nextToken;
subResponse = await subRequest.ExecuteAsync();
nextToken = subResponse.NextPageToken;
foreach (var i in response.Items)
Notifications.Add(new Notification("video", i.Id.VideoId,
i.Snippet.ChannelTitle,
i.Snippet.Title,
i.Snippet.PublishedAt.Value,
i.Snippet.Thumbnails.Standard.Url,
s.Value));
foreach (Subscription s in subResponse.Items)
subs.Add(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url);
}
foreach (var s in subs)
{
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));
}
}
catch { }
}
void CheckAnnouncements()
{
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)
Notifications.Add(new Notification("internal", (doc["posts"].FirstChild as XmlElement).GetAttribute("id"),
doc["posts"].FirstChild["header"].InnerText,
doc["posts"].FirstChild["notificationHeader"].InnerText,
XmlConvert.ToDateTimeOffset((doc["posts"].FirstChild as XmlElement).GetAttribute("time"), "YYYY-MM-DDThh:mm:ss"),
(doc["posts"].FirstChild as XmlElement).GetAttribute("image"), (doc["posts"].FirstChild as XmlElement).GetAttribute("avatar")));
}
void SendNotification(Notification notification)
{
ToastNotificationManager.CreateToastNotifier().Show(notification.GetToast());
}
void SendNSave()
{
foreach (Notification n in Notifications)
try
{
NotificationRecieved.Invoke(this, n);
doc["history"].InnerXml += n.GetXml();
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));
}
settings.Values.Add("notificationsHistory", doc.InnerXml);
foreach (Notification n in Notifications)
switch (n.Type)
{
case NotificationType.Video:
if ((bool)settings.Values["newVideoNotification"])
SendNotification(n);
break;
case NotificationType.Internal:
SendNotification(n);
break;
}
catch { }
}
}
}