Development 6
This commit is contained in:
@@ -16,11 +16,15 @@ using Windows.UI.Notifications;
|
||||
|
||||
namespace FoxTube.Background
|
||||
{
|
||||
public delegate void NotificationHandler(object sender, string xml);
|
||||
|
||||
public sealed class BackgroundProcessor : IBackgroundTask
|
||||
{
|
||||
public List<Notification> Notifications = new List<Notification>();
|
||||
public static event NotificationHandler NotificationRecieved;
|
||||
|
||||
public List<Notification> Notifications = new List<Notification>();
|
||||
private DateTime lastCheck = DateTime.Now;
|
||||
|
||||
private ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
|
||||
|
||||
private ClientSecrets Secrets => new ClientSecrets()
|
||||
@@ -29,11 +33,17 @@ namespace FoxTube.Background
|
||||
ClientSecret = "BkVZOAaCU2Zclf0Zlicg6y2_"
|
||||
};
|
||||
|
||||
private YouTubeService Service => new YouTubeService(new BaseClientService.Initializer()
|
||||
{
|
||||
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
||||
ApplicationName = "FoxTube"
|
||||
});
|
||||
|
||||
XmlDocument doc = new XmlDocument();
|
||||
BackgroundTaskDeferral def;
|
||||
public async void Run(IBackgroundTaskInstance taskInstance)
|
||||
public void Run(IBackgroundTaskInstance taskInstance)
|
||||
{
|
||||
def = taskInstance.GetDeferral();
|
||||
XmlDocument doc = new XmlDocument();
|
||||
if (settings.Values["lastCheck"] == null)
|
||||
settings.Values.Add("lastCheck", XmlConvert.ToString(DateTime.Now));
|
||||
else lastCheck = XmlConvert.ToDateTime(settings.Values["lastCheck"] as string, XmlDateTimeSerializationMode.Unspecified);
|
||||
@@ -43,14 +53,17 @@ namespace FoxTube.Background
|
||||
else
|
||||
{
|
||||
doc.AppendChild(doc.CreateXmlDeclaration("1.0", "utf-8", null));
|
||||
doc.AppendChild(doc.CreateElement("notifications"));
|
||||
doc.AppendChild(doc.CreateElement("history"));
|
||||
settings.Values.Add("notificationsHistory", doc.InnerXml);
|
||||
}
|
||||
|
||||
CheckAccount();
|
||||
if((bool)settings.Values["authorized"] == true)
|
||||
CheckAccount();
|
||||
|
||||
CheckAnnouncements();
|
||||
|
||||
SendNSave();
|
||||
|
||||
def.Complete();
|
||||
}
|
||||
|
||||
@@ -68,10 +81,10 @@ namespace FoxTube.Background
|
||||
subRequest.Mine = true;
|
||||
subRequest.MaxResults = 50;
|
||||
SubscriptionListResponse subResponse = await subRequest.ExecuteAsync();
|
||||
List<string> subs = new List<string>();
|
||||
List<KeyValuePair<string, string>> subs = new List<KeyValuePair<string, string>>();
|
||||
|
||||
foreach (Subscription s in subResponse.Items)
|
||||
subs.Add(s.Snippet.ResourceId.ChannelId);
|
||||
subs.Add(new KeyValuePair<string, string>(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url));
|
||||
|
||||
string nextToken = subResponse.NextPageToken;
|
||||
while (nextToken != null)
|
||||
@@ -79,26 +92,25 @@ namespace FoxTube.Background
|
||||
subRequest.PageToken = nextToken;
|
||||
subResponse = await subRequest.ExecuteAsync();
|
||||
foreach (Subscription s in subResponse.Items)
|
||||
subs.Add(s.Snippet.ResourceId.ChannelId);
|
||||
subs.Add(new KeyValuePair<string, string>(s.Snippet.ResourceId.ChannelId, s.Snippet.Thumbnails.Standard.Url));
|
||||
}
|
||||
|
||||
foreach(string s in subs)
|
||||
foreach(var s in subs)
|
||||
{
|
||||
var request = new YouTubeService(new BaseClientService.Initializer()
|
||||
{
|
||||
ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0",
|
||||
ApplicationName = "FoxTube"
|
||||
}).Activities.List("snippet");
|
||||
var request = Service.Search.List("snippet");
|
||||
request.PublishedAfter = lastCheck;
|
||||
request.ChannelId = s;
|
||||
request.MaxResults = 10;
|
||||
request.ChannelId = s.Key;
|
||||
request.Type = "video";
|
||||
var response = await request.ExecuteAsync();
|
||||
|
||||
if(response.Items.Count > 0)
|
||||
foreach (Activity a in response.Items)
|
||||
{
|
||||
if(a.Snippet.Type == "upload")
|
||||
}
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -107,11 +119,12 @@ namespace FoxTube.Background
|
||||
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)
|
||||
Add(new Notification(NotificationType.Internal,
|
||||
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.Local), (doc["posts"].FirstChild as XmlElement).GetAttribute("image")));
|
||||
XmlDateTimeSerializationMode.Unspecified),
|
||||
(doc["posts"].FirstChild as XmlElement).GetAttribute("image")));
|
||||
|
||||
return doc.InnerXml;
|
||||
}
|
||||
@@ -121,9 +134,29 @@ namespace FoxTube.Background
|
||||
ToastNotificationManager.CreateToastNotifier().Show(notification.GetToast(0));
|
||||
}
|
||||
|
||||
void Add(Notification notification)
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,26 +26,30 @@ namespace FoxTube.Background
|
||||
public NotificationType Type { get; set; }
|
||||
public string Avatar { get; set; }
|
||||
public string Thumbnail { get; set; }
|
||||
public string Id { get; set; }
|
||||
|
||||
public Notification(NotificationType type,
|
||||
public Notification(string type, string id,
|
||||
string channelName, string content, DateTime date,
|
||||
string thumbnailUrl, string avatarUrl = "ms-appx:///Assets/Icons/Profile.png")
|
||||
{
|
||||
Channel = channelName;
|
||||
Content = content;
|
||||
TimeStamp = date;
|
||||
Type = type;
|
||||
Type = TypeConversion(type);
|
||||
Avatar = avatarUrl;
|
||||
Thumbnail = thumbnailUrl;
|
||||
}
|
||||
|
||||
public string GetXml()
|
||||
{
|
||||
|
||||
return string.Empty;
|
||||
return $@"<item time='{TimeStamp.ToString("YYYY-MM-DDThh:mm:ss")}' type='{TypeConversion(Type)}' id='{Id}'>
|
||||
<images thumbnail='{Thumbnail}' avatar='{Avatar}'/>
|
||||
<channelName>{Channel}</channelName>
|
||||
<content>{Content}</content>
|
||||
</item>";
|
||||
}
|
||||
|
||||
public UIElement GetNotification()
|
||||
public Button GetNotification()
|
||||
{
|
||||
StackPanel stackPanel = new StackPanel() { Margin = new Thickness(10, 0, 0, 0) };
|
||||
|
||||
@@ -231,5 +235,35 @@ namespace FoxTube.Background
|
||||
|
||||
return new ToastNotification(template);
|
||||
}
|
||||
|
||||
private string TypeConversion(NotificationType type)
|
||||
{
|
||||
switch(type)
|
||||
{
|
||||
case NotificationType.Comment:
|
||||
return "comment";
|
||||
case NotificationType.Post:
|
||||
return "post";
|
||||
case NotificationType.Video:
|
||||
return "video";
|
||||
default:
|
||||
return "internal";
|
||||
}
|
||||
}
|
||||
|
||||
private NotificationType TypeConversion(string type)
|
||||
{
|
||||
switch (type)
|
||||
{
|
||||
case "comment":
|
||||
return NotificationType.Comment;
|
||||
case "post":
|
||||
return NotificationType.Post;
|
||||
case "video":
|
||||
return NotificationType.Video;
|
||||
default:
|
||||
return NotificationType.Internal;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user