163 lines
6.6 KiB
C#
163 lines
6.6 KiB
C#
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;
|
|
using Windows.Storage;
|
|
using Windows.UI.Notifications;
|
|
|
|
namespace FoxTube.Background
|
|
{
|
|
public delegate void NotificationHandler(object sender, string xml);
|
|
|
|
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;
|
|
|
|
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"
|
|
});
|
|
|
|
XmlDocument doc = new XmlDocument();
|
|
BackgroundTaskDeferral def;
|
|
public void Run(IBackgroundTaskInstance taskInstance)
|
|
{
|
|
def = taskInstance.GetDeferral();
|
|
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);
|
|
else
|
|
{
|
|
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
|
|
doc.AppendChild(doc.CreateElement("history"));
|
|
settings.Values.Add("notificationsHistory", doc.InnerXml);
|
|
}
|
|
|
|
if((bool)settings.Values["authorized"] == true)
|
|
CheckAccount();
|
|
|
|
CheckAnnouncements();
|
|
|
|
SendNSave();
|
|
|
|
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()
|
|
{
|
|
HttpClientInitializer = credential,
|
|
ApplicationName = "FoxTube"
|
|
}).Subscriptions.List("snippet");
|
|
subRequest.Mine = true;
|
|
subRequest.MaxResults = 50;
|
|
SubscriptionListResponse subResponse = await subRequest.ExecuteAsync();
|
|
List<KeyValuePair<string, string>> subs = new List<KeyValuePair<string, string>>();
|
|
|
|
foreach (Subscription s in subResponse.Items)
|
|
subs.Add(new KeyValuePair<string, string>(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url));
|
|
|
|
string nextToken = subResponse.NextPageToken;
|
|
while (nextToken != null)
|
|
{
|
|
subRequest.PageToken = nextToken;
|
|
subResponse = await subRequest.ExecuteAsync();
|
|
foreach (Subscription s in subResponse.Items)
|
|
subs.Add(new KeyValuePair<string, string>(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url));
|
|
}
|
|
|
|
foreach(var s in subs)
|
|
{
|
|
var request = Service.Search.List("snippet");
|
|
request.PublishedAfter = lastCheck;
|
|
request.ChannelId = s.Key;
|
|
request.Type = "video";
|
|
var response = await request.ExecuteAsync();
|
|
|
|
if(response.Items.Count > 0)
|
|
foreach (var a in response.Items)
|
|
Notifications.Add(new Notification("video", a.Id.VideoId,
|
|
a.Snippet.ChannelTitle,
|
|
a.Snippet.Title,
|
|
a.Snippet.PublishedAt.Value,
|
|
a.Snippet.Thumbnails.Standard.Url,
|
|
s.Value));
|
|
}
|
|
}
|
|
|
|
string 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) - lastCheck.ToUniversalTime()).TotalSeconds > 0)
|
|
Notifications.Add(new Notification("internal", (doc["posts"].FirstChild as XmlElement).GetAttribute("id"),
|
|
doc["posts"].FirstChild["notificationHeader"].InnerText,
|
|
doc["posts"].FirstChild["content"].InnerText,
|
|
XmlConvert.ToDateTime((doc["posts"].FirstChild as XmlElement).GetAttribute("time"),
|
|
XmlDateTimeSerializationMode.Unspecified),
|
|
(doc["posts"].FirstChild as XmlElement).GetAttribute("image"), null));
|
|
|
|
return doc.InnerXml;
|
|
}
|
|
|
|
void SendNotification(Notification notification)
|
|
{
|
|
ToastNotificationManager.CreateToastNotifier().Show(notification.GetToast(0));
|
|
}
|
|
|
|
void SendNSave()
|
|
{
|
|
foreach (Notification n in Notifications)
|
|
{
|
|
NotificationRecieved.Invoke(this, n.GetXml());
|
|
doc["history"].InnerXml += n.GetXml();
|
|
}
|
|
settings.Values.Add("notificationsHistory", doc.InnerXml);
|
|
|
|
if ((bool)settings.Values["notifications"])
|
|
foreach (Notification n in Notifications)
|
|
switch (n.Type)
|
|
{
|
|
case NotificationType.Video:
|
|
if ((bool)settings.Values["newVideoNotification"])
|
|
SendNotification(n);
|
|
break;
|
|
|
|
case NotificationType.Internal:
|
|
if ((bool)settings.Values["newmessagesNotification"])
|
|
SendNotification(n);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|