From 7ee8d9dd18b5042fc693479097ba540368a1da59 Mon Sep 17 00:00:00 2001 From: Michael Gordeev Date: Wed, 9 May 2018 17:16:06 +0300 Subject: [PATCH] Search --- FoxTube/ChannelCard.xaml.cs | 30 +++++++- FoxTube/ChannelVideos.xaml | 2 +- FoxTube/FoxTube.csproj | 7 ++ FoxTube/MainPage.xaml | 121 +++++++++++++++++++------------ FoxTube/MainPage.xaml.cs | 39 +++++++++- FoxTube/PlaylistCardWide.xaml | 51 +++++++++++++ FoxTube/PlaylistCardWide.xaml.cs | 67 +++++++++++++++++ FoxTube/Search.xaml.cs | 52 +++---------- FoxTube/Settings.xaml | 33 +++++++-- FoxTube/VideoCardWide.xaml.cs | 47 +++++++++--- FoxTube/VideoGrid.xaml | 64 ++++++++-------- FoxTube/VideoPlayer.xaml | 38 +++++++--- 12 files changed, 398 insertions(+), 153 deletions(-) create mode 100644 FoxTube/PlaylistCardWide.xaml create mode 100644 FoxTube/PlaylistCardWide.xaml.cs diff --git a/FoxTube/ChannelCard.xaml.cs b/FoxTube/ChannelCard.xaml.cs index 1da04fc..601bc8e 100644 --- a/FoxTube/ChannelCard.xaml.cs +++ b/FoxTube/ChannelCard.xaml.cs @@ -15,6 +15,10 @@ using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; +using Google.Apis.Services; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; + // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace FoxTube @@ -22,9 +26,33 @@ namespace FoxTube public sealed partial class ChannelCard : UserControl { string channelId; - public ChannelCard() + public ChannelCard(string id, string live) { this.InitializeComponent(); + Initialize(id, live); + } + + public async void Initialize(string id, string live) + { + YouTubeService ytService = new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", + ApplicationName = this.GetType().ToString() + }); + + ChannelsResource.ListRequest request = ytService.Channels.List("snippet,contentDetails,statistics"); + request.Id = id; + ChannelListResponse response = await request.ExecuteAsync(); + + var item = response.Items[0]; + + channelName.Text = item.Snippet.Title; + subscribers.Text = string.Format("{0} subscribers", item.Statistics.SubscriberCount); + videoCount.Text = string.Format("{0} videos", item.Statistics.VideoCount); + + avatar.ProfilePicture = new BitmapImage(new Uri(item.Snippet.Thumbnails.Medium.Url)); + if (live == "live") + liveTag.Visibility = Visibility.Visible; } public void AddInfo(string name, int videos, string avatarUrl, string channelUrl, int subs, Visibility live, bool logged) diff --git a/FoxTube/ChannelVideos.xaml b/FoxTube/ChannelVideos.xaml index ed5d222..6d2d1ee 100644 --- a/FoxTube/ChannelVideos.xaml +++ b/FoxTube/ChannelVideos.xaml @@ -29,7 +29,7 @@ - + diff --git a/FoxTube/FoxTube.csproj b/FoxTube/FoxTube.csproj index 369842c..9348a4f 100644 --- a/FoxTube/FoxTube.csproj +++ b/FoxTube/FoxTube.csproj @@ -123,6 +123,9 @@ MainPage.xaml + + PlaylistCardWide.xaml + Search.xaml @@ -255,6 +258,10 @@ MSBuild:Compile Designer + + Designer + MSBuild:Compile + Designer MSBuild:Compile diff --git a/FoxTube/MainPage.xaml b/FoxTube/MainPage.xaml index b5d8b8e..88a35d8 100644 --- a/FoxTube/MainPage.xaml +++ b/FoxTube/MainPage.xaml @@ -106,54 +106,81 @@ Width="42" Height="42" Margin="4" RelativePanel.AlignRightWithPanel="True" Background="Transparent" - FontFamily="Segoe MDL2 Assets" Content="" FontSize="20" FocusVisualSecondaryBrush="#66FFFFFF" FocusVisualPrimaryBrush="White" Foreground="Black"/> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + FontFamily="Segoe MDL2 Assets" Content="" FontSize="20" Foreground="Black"/> + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FoxTube/MainPage.xaml.cs b/FoxTube/MainPage.xaml.cs index 2687f96..311beb7 100644 --- a/FoxTube/MainPage.xaml.cs +++ b/FoxTube/MainPage.xaml.cs @@ -19,6 +19,7 @@ using Microsoft.Toolkit.Uwp.Notifications; using Windows.UI.Notifications; using Windows.UI.Xaml.Media.Imaging; using Windows.Storage; +using System.Xml; using Google.Apis.Auth.OAuth2; using Google.Apis.Services; @@ -39,6 +40,7 @@ namespace FoxTube public sealed partial class MainPage : Page { private bool loggedIn = false; + UserCredential credential; public bool Logged { get @@ -331,7 +333,6 @@ namespace FoxTube private async Task LogIn() { - UserCredential credential; StorageFile file = await StorageFile.GetFileFromApplicationUriAsync(new Uri(@"ms-appx:///client_secrets.json")); Debug.WriteLine("Exception accured after opening a stream"); @@ -344,7 +345,7 @@ namespace FoxTube new FileDataStore(this.GetType().ToString()) ); Debug.WriteLine("200 OK"); - /*using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) + using (var stream = new FileStream("client_secrets.json", FileMode.Open, FileAccess.Read)) { credential = await GoogleWebAuthorizationBroker.AuthorizeAsync( GoogleClientSecrets.Load(stream).Secrets, @@ -353,12 +354,12 @@ namespace FoxTube CancellationToken.None, new FileDataStore(this.GetType().ToString()) ); - }*/ + } } private void searchField_TextChanged(object sender, TextChangedEventArgs e) { - if (searchField.Text != "") + if (searchField.Text.Length > 2) { searchSuggestions.Visibility = Visibility.Visible; buildSearchSuggestionsTree(searchField.Text); @@ -419,5 +420,35 @@ namespace FoxTube searchSuggestions.Visibility = Visibility.Collapsed; } } + + public static string NormalizeDuration(string code) + { + Debug.WriteLine(string.Format("Source string: '{0}'", code)); + string temp = code.Trim('P', 'T', 'S'); + Debug.WriteLine(string.Format("Trimmed string: '{0}'", temp)); + temp = temp.Replace('H', '|'); + temp = temp.Replace('M', '|'); + if (temp.StartsWith('|')) + temp.TrimStart('|'); + if (temp.EndsWith('|')) + temp.TrimEnd('|'); + Debug.WriteLine(string.Format("End string: '{0}'", temp)); + string[] arr = temp.Split('|'); + Debug.WriteLine(string.Format("Array length: {0}", arr.Length)); + foreach (string str in arr) + Debug.WriteLine(str); + + switch(arr.Length) + { + case 3: + return new TimeSpan(Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1]), Convert.ToInt32(arr[2])).ToString(); + case 2: + return new TimeSpan(0, Convert.ToInt32(arr[0]), Convert.ToInt32(arr[1])).ToString(); + case 1: + return new TimeSpan(0, 0, Convert.ToInt32(arr[0])).ToString(); + default: + return ""; + } + } } } diff --git a/FoxTube/PlaylistCardWide.xaml b/FoxTube/PlaylistCardWide.xaml new file mode 100644 index 0000000..1b9b78a --- /dev/null +++ b/FoxTube/PlaylistCardWide.xaml @@ -0,0 +1,51 @@ + + + + diff --git a/FoxTube/PlaylistCardWide.xaml.cs b/FoxTube/PlaylistCardWide.xaml.cs new file mode 100644 index 0000000..74f42a5 --- /dev/null +++ b/FoxTube/PlaylistCardWide.xaml.cs @@ -0,0 +1,67 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices.WindowsRuntime; +using Windows.Foundation; +using Windows.Foundation.Collections; +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; +using Windows.UI.Xaml.Controls.Primitives; +using Windows.UI.Xaml.Data; +using Windows.UI.Xaml.Input; +using Windows.UI.Xaml.Media; +using Windows.UI.Xaml.Media.Imaging; +using Windows.UI.Xaml.Navigation; + +using Google.Apis.Services; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using System.Threading.Tasks; +using System.Threading; +using Google.Apis.Util.Store; + +// The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 + +namespace FoxTube +{ + public sealed partial class PlaylistCardWide : UserControl + { + public PlaylistCardWide(string id) + { + this.InitializeComponent(); + Initialize(id); + } + + public async void Initialize(string id) + { + YouTubeService ytService = new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", + ApplicationName = this.GetType().ToString() + }); + + PlaylistsResource.ListRequest request = ytService.Playlists.List("snippet,contentDetails"); + request.Id = id; + PlaylistListResponse response = await request.ExecuteAsync(); + + var item = response.Items[0]; + + title.Text = item.Snippet.Title; + info.Text = string.Format("{0} | {1} videos", item.Snippet.PublishedAt, item.ContentDetails.ItemCount); + thumbCount.Text = item.ContentDetails.ItemCount.ToString(); + thumbnail.Source = new BitmapImage(new Uri(item.Snippet.Thumbnails.Medium.Url)); + + var request1 = ytService.Channels.List("snippet,contentDetails,statistics"); + request1.Id = item.Snippet.ChannelId; + ChannelListResponse response1 = await request1.ExecuteAsync(); + + var item1 = response1.Items[0]; + + avatar.ProfilePicture = new BitmapImage(new Uri(item1.Snippet.Thumbnails.Medium.Url)); + channelName.Text = item1.Snippet.Title; + channelSubs.Text = string.Format("{0} subscribers", item1.Statistics.SubscriberCount); + channelLink.NavigateUri = new Uri("https://www.youtube.com/channel/" + item.Snippet.ChannelId); + } + } +} diff --git a/FoxTube/Search.xaml.cs b/FoxTube/Search.xaml.cs index 535d615..5af2c7b 100644 --- a/FoxTube/Search.xaml.cs +++ b/FoxTube/Search.xaml.cs @@ -47,58 +47,24 @@ namespace FoxTube switch (result.Id.Kind) { case "youtube#video": - AddVideo(result.Snippet.Title, - "null", "null", result.Snippet.PublishedAt, - result.Snippet.ChannelTitle, "null", - result.Snippet.ChannelId, result.Snippet.Thumbnails.Medium.Url, "null", result.Snippet.LiveBroadcastContent); + VideoCardWide vCard = new VideoCardWide(result.Id.VideoId); + resultsList.Children.Add(vCard); break; case "youtube#channel": - AddChannel(result.Snippet.Title, 0, 0, - result.Snippet.ChannelId, result.Snippet.Thumbnails.Medium.Url, result.Snippet.LiveBroadcastContent, logged); + ChannelCard cCard = new ChannelCard(result.Id.ChannelId, result.Snippet.LiveBroadcastContent); + resultsList.Children.Add(cCard); + break; + + case "youtube#playlist": + PlaylistCardWide pCard = new PlaylistCardWide(result.Id.PlaylistId); + resultsList.Children.Add(pCard); break; default: Debug.WriteLine("Skipped"); break; } - Google.Apis.YouTube.v3.Data.Video videItem = new Google.Apis.YouTube.v3.Data.Video(); - } - - void AddVideo(string title, - string duration, string views, DateTime? publicDate, - string channelName, string subs, - string channelId, string thumbUrl, string avatarUrl, string liveBroadcast) - { - Visibility live; - if (liveBroadcast != "live") - live = Visibility.Collapsed; - else live = Visibility.Visible; - - VideoCardWide card = new VideoCardWide(); - card.AddInfo(title, - string.Format("{0} | {1} | {2}", duration, views, publicDate), - thumbUrl, avatarUrl, string.Format("https://www.youtube.com/channel/{0}", channelId), - channelName, subs, live); - - resultsList.Children.Add(card); - Debug.WriteLine("result item added"); - } - - void AddChannel(string name, int followers, int uploads, - string url, string avatar, string liveBroadcast, bool logged) - { - Visibility live; - if (liveBroadcast != "live") - live = Visibility.Collapsed; - else live = Visibility.Visible; - - ChannelCard card = new ChannelCard(); - card.AddInfo(name, uploads, avatar, - "https://www.youtube.com/channel/" + url, followers, live, logged); - - resultsList.Children.Add(card); - Debug.WriteLine("result item added"); } } } diff --git a/FoxTube/Settings.xaml b/FoxTube/Settings.xaml index 0f27805..a6d1ba1 100644 --- a/FoxTube/Settings.xaml +++ b/FoxTube/Settings.xaml @@ -9,10 +9,33 @@ - - + + - + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FoxTube/VideoCardWide.xaml.cs b/FoxTube/VideoCardWide.xaml.cs index 20ae434..50ecdf5 100644 --- a/FoxTube/VideoCardWide.xaml.cs +++ b/FoxTube/VideoCardWide.xaml.cs @@ -14,28 +14,55 @@ using Windows.UI.Xaml.Media; using Windows.UI.Xaml.Media.Imaging; using Windows.UI.Xaml.Navigation; +using Google.Apis.Services; +using Google.Apis.YouTube.v3; +using Google.Apis.YouTube.v3.Data; +using System.Threading.Tasks; +using System.Threading; +using Google.Apis.Util.Store; + // The User Control item template is documented at https://go.microsoft.com/fwlink/?LinkId=234236 namespace FoxTube { public sealed partial class VideoCardWide : UserControl { - public VideoCardWide() + public VideoCardWide(string id) { this.InitializeComponent(); + Initialize(id); } - public void AddInfo(string name, string description, string thumbUrl, string avatarUrl, string channelUrl, string channel, string subs, Visibility live) + + public async void Initialize(string id) { - title.Text = name; - info.Text = description; + YouTubeService ytService = new YouTubeService(new BaseClientService.Initializer() + { + ApiKey = "AIzaSyBgHrCnrlzlVmk0cJKL8RqP9Y8x6XSuk_0", + ApplicationName = this.GetType().ToString() + }); - thumbnail.Source = new BitmapImage(new Uri(thumbUrl)); - //avatar.ProfilePicture = new BitmapImage(new Uri(avatarUrl)); + VideosResource.ListRequest request = ytService.Videos.List("snippet,contentDetails,statistics"); + request.Id = id; + VideoListResponse response = await request.ExecuteAsync(); - channelName.Text = channel; - channelSubs.Text = string.Format("{0} subscribers", subs); - channelLink.NavigateUri = new Uri(channelUrl); - liveTag.Visibility = live; + var item = response.Items[0]; + + title.Text = item.Snippet.Title; + info.Text = string.Format("{0} | {1} | {2} views", item.ContentDetails.Duration, item.Snippet.PublishedAt, item.Statistics.ViewCount); + thumbnail.Source = new BitmapImage(new Uri(item.Snippet.Thumbnails.Medium.Url)); + if (item.Snippet.LiveBroadcastContent == "live") + liveTag.Visibility = Visibility.Visible; + + var request1 = ytService.Channels.List("snippet,contentDetails,statistics"); + request1.Id = item.Snippet.ChannelId; + ChannelListResponse response1 = await request1.ExecuteAsync(); + + var item1 = response1.Items[0]; + + avatar.ProfilePicture = new BitmapImage(new Uri(item1.Snippet.Thumbnails.Medium.Url)); + channelName.Text = item1.Snippet.Title; + channelSubs.Text = string.Format("{0} subscribers", item1.Statistics.SubscriberCount); + channelLink.NavigateUri = new Uri("https://www.youtube.com/channel/" + item.Snippet.ChannelId); } } } diff --git a/FoxTube/VideoGrid.xaml b/FoxTube/VideoGrid.xaml index e7bfaf9..0ac9ead 100644 --- a/FoxTube/VideoGrid.xaml +++ b/FoxTube/VideoGrid.xaml @@ -8,35 +8,39 @@ mc:Ignorable="d"> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/FoxTube/VideoPlayer.xaml b/FoxTube/VideoPlayer.xaml index d0b9738..e139fa7 100644 --- a/FoxTube/VideoPlayer.xaml +++ b/FoxTube/VideoPlayer.xaml @@ -120,33 +120,47 @@ - - - - + + + + + + + + + + + + + + - +