Archived
1
0
This repository has been archived on 2026-04-22. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
FoxTube/FoxTube.Background/BackgroundProcessor.cs
T
2018-10-24 16:46:22 +03:00

121 lines
5.0 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 sealed class BackgroundProcessor : IBackgroundTask
{
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"
});
BackgroundTaskDeferral def;
public 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"])
CheckAnnouncements();
if((bool)settings.Values["devNews"])
CheckAccount();
settings.Values["lastCheck"] = XmlConvert.ToString(DateTime.Now, "YYYY-MM-DDThh:mm:ss");
def.Complete();
}
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)
{
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()
{
try
{
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));
}
catch { }
}
}
}