121 lines
4.9 KiB
C#
121 lines
4.9 KiB
C#
using Google.Apis.Services;
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using Microsoft.AppCenter.Analytics;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
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 readonly ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
|
|
private YouTubeService Service => new YouTubeService(new BaseClientService.Initializer()
|
|
{
|
|
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
|
ApplicationName = "FoxTube"
|
|
});
|
|
BackgroundTaskDeferral def;
|
|
|
|
public async void Run(IBackgroundTaskInstance taskInstance)
|
|
{
|
|
try
|
|
{
|
|
def = taskInstance.GetDeferral();
|
|
taskInstance.Canceled += new BackgroundTaskCanceledEventHandler(OnCanceled);
|
|
|
|
if (settings.Values["lastCheck"] == null)
|
|
{
|
|
settings.Values.Add("lastCheck", DateTime.Now.ToString());
|
|
def.Complete();
|
|
return;
|
|
}
|
|
else
|
|
lastCheck = DateTime.Parse(settings.Values["lastCheck"] as string);
|
|
|
|
bool[] notificationsSettings = JsonConvert.DeserializeObject<bool[]>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("notifications.json")));
|
|
if (notificationsSettings[0])
|
|
CheckAnnouncements();
|
|
if (notificationsSettings[1])
|
|
await CheckAccount();
|
|
}
|
|
finally
|
|
{
|
|
settings.Values["lastCheck"] = DateTime.Now.ToString();
|
|
def.Complete();
|
|
}
|
|
}
|
|
|
|
private void OnCanceled(IBackgroundTaskInstance sender, BackgroundTaskCancellationReason reason)
|
|
{
|
|
Analytics.TrackEvent("Background task caneled", new Dictionary<string, string>()
|
|
{
|
|
{ "Reason", reason.ToString() }
|
|
});
|
|
settings.Values["lastCheck"] = DateTime.Now.ToString();
|
|
}
|
|
|
|
async Task CheckAccount()
|
|
{
|
|
try
|
|
{
|
|
Dictionary<string, string> subscriptions = JsonConvert.DeserializeObject<Dictionary<string, string>>(await FileIO.ReadTextAsync(await ApplicationData.Current.RoamingFolder.GetFileAsync("background.json")));
|
|
|
|
List<SearchResult> results = new List<SearchResult>();
|
|
|
|
foreach (var s in subscriptions)
|
|
{
|
|
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 (SearchResult i in response.Items)
|
|
{
|
|
results.Add(i);
|
|
|
|
ToastNotificationManager.CreateToastNotifier().Show(
|
|
Notification.GetVideoToast(i.Id.VideoId, i.Snippet.ChannelId, i.Snippet.Title, i.Snippet.ChannelTitle, i.Snippet.Thumbnails.Medium.Url, s.Value));
|
|
}
|
|
}
|
|
|
|
results.OrderBy(i => i.Snippet.PublishedAt);
|
|
|
|
TileUpdater updater = TileUpdateManager.CreateTileUpdaterForApplication();
|
|
updater.EnableNotificationQueue(true);
|
|
updater.Clear();
|
|
for (int i = 0; i < 5; i++)
|
|
updater.Update(Tiles.GetTileLayout(results[i].Snippet.Title, results[i].Snippet.ChannelTitle, results[i].Snippet.Thumbnails.Medium.Url, subscriptions[results[i].Snippet.ChannelId]));
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
void CheckAnnouncements()
|
|
{
|
|
try
|
|
{
|
|
XmlDocument doc = new XmlDocument();
|
|
doc.Load(XmlReader.Create("http://foxgame-studio.000webhostapp.com/foxtube-messages.xml"));
|
|
if ((DateTime.Parse((doc["posts"].FirstChild as XmlElement).GetAttribute("time")) - lastCheck).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 { }
|
|
}
|
|
}
|
|
}
|