128 lines
3.4 KiB
C#
128 lines
3.4 KiB
C#
using FoxTube.Utils;
|
|
using Google.Apis.Auth.OAuth2;
|
|
using Google.Apis.Oauth2.v2;
|
|
using Google.Apis.Oauth2.v2.Data;
|
|
using Google.Apis.Services;
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using Newtonsoft.Json;
|
|
using System;
|
|
using System.Linq;
|
|
using System.Collections.Generic;
|
|
using System.Threading.Tasks;
|
|
using Windows.Storage;
|
|
using YouTube;
|
|
using YoutubeExplode;
|
|
|
|
namespace FoxTube.Models
|
|
{
|
|
public class User
|
|
{
|
|
public Userinfoplus UserInfo { get; private set; }
|
|
public UserCredential Credential { get; private set; }
|
|
public Channel Channel { get; private set; }
|
|
public List<Subscription> Subscriptions { get; } = new List<Subscription>();
|
|
|
|
public ExtendedYouTubeService Service { get; private set; }
|
|
public YoutubeClient Client { get; private set; }
|
|
|
|
private User() { }
|
|
|
|
public async Task<bool> UpdateSubscriptionState(string channelId)
|
|
{
|
|
if (Subscriptions.Exists(x => x.Snippet.ResourceId.ChannelId == channelId))
|
|
{
|
|
Subscription s = Subscriptions.Find(x => x.Snippet.ResourceId.ChannelId == channelId);
|
|
|
|
try { await Service.Subscriptions.Delete(s.Id).ExecuteAsync(); }
|
|
catch (Exception e)
|
|
{
|
|
Metrics.SendReport(new Exception("Failed to unsubscribe", e));
|
|
return true;
|
|
}
|
|
|
|
UserManagement.SubscriptionsChangedInvoker(this, s);
|
|
Subscriptions.Remove(s);
|
|
|
|
SaveSubscriptions();
|
|
return false;
|
|
}
|
|
else
|
|
{
|
|
var request = Service.Subscriptions.Insert(new Subscription()
|
|
{
|
|
Snippet = new SubscriptionSnippet()
|
|
{
|
|
ResourceId = new ResourceId()
|
|
{
|
|
ChannelId = channelId,
|
|
Kind = "youtube#channel"
|
|
}
|
|
}
|
|
}, "snippet");
|
|
|
|
Subscription s = await request.ExecuteAsync();
|
|
if (s == null)
|
|
return false;
|
|
Subscriptions.Add(s);
|
|
|
|
UserManagement.SubscriptionsChangedInvoker(this, s);
|
|
|
|
SaveSubscriptions();
|
|
return true;
|
|
}
|
|
}
|
|
|
|
private void SaveSubscriptions()
|
|
{
|
|
Dictionary<string, string> subs = Subscriptions.Select(i =>
|
|
new KeyValuePair<string, string>(i.Snippet.ResourceId.ChannelId, i.Snippet.Thumbnails.Default__?.Url))
|
|
as Dictionary<string, string>;
|
|
|
|
ApplicationData.Current.RoamingSettings.Values[$"Subscriptions.{UserInfo.Id}"] = JsonConvert.SerializeObject(subs);
|
|
}
|
|
|
|
public static async Task<User> GetUser(UserCredential credential)
|
|
{
|
|
User user = new User
|
|
{
|
|
Credential = credential
|
|
};
|
|
|
|
BaseClientService.Initializer initializer = new BaseClientService.Initializer
|
|
{
|
|
ApplicationName = "FoxTube",
|
|
HttpClientInitializer = credential
|
|
};
|
|
|
|
user.Service = new ExtendedYouTubeService(initializer);
|
|
|
|
user.UserInfo = await new Oauth2Service(initializer).Userinfo.Get().ExecuteAsync();
|
|
|
|
// TODO: Retrieve history and WL
|
|
|
|
SubscriptionsResource.ListRequest subRequest = user.Service.Subscriptions.List("snippet");
|
|
subRequest.Mine = true;
|
|
subRequest.MaxResults = 50;
|
|
subRequest.Order = SubscriptionsResource.ListRequest.OrderEnum.Relevance;
|
|
SubscriptionListResponse subResponse;
|
|
|
|
do
|
|
{
|
|
subResponse = await subRequest.ExecuteAsync();
|
|
subRequest.PageToken = subResponse.NextPageToken;
|
|
|
|
user.Subscriptions.AddRange(subResponse.Items);
|
|
|
|
} while (!string.IsNullOrWhiteSpace(subRequest.PageToken));
|
|
|
|
|
|
var request = user.Service.Channels.List("snippet,contentDetails,brandingSettings");
|
|
request.Mine = true;
|
|
user.Channel = request.Execute().Items[0];
|
|
|
|
return user;
|
|
}
|
|
}
|
|
}
|