308 lines
11 KiB
C#
308 lines
11 KiB
C#
using FoxTube.Controls;
|
|
using FoxTube.Pages;
|
|
using Google.Apis.YouTube.v3;
|
|
using Google.Apis.YouTube.v3.Data;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Runtime.InteropServices.WindowsRuntime;
|
|
using Windows.Foundation;
|
|
using Windows.Foundation.Collections;
|
|
using Windows.Storage;
|
|
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.Navigation;
|
|
|
|
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
|
|
|
|
namespace FoxTube
|
|
{
|
|
/// <summary>
|
|
/// An empty page that can be used on its own or navigated to within a Frame.
|
|
/// </summary>
|
|
public sealed partial class Search : Page
|
|
{
|
|
public string Term;
|
|
SearchResource.ListRequest request;
|
|
string nextToken;
|
|
ApplicationDataContainer settings = ApplicationData.Current.LocalSettings;
|
|
|
|
VideoGrid list;
|
|
LoadingPage loading;
|
|
ShowMore more;
|
|
|
|
public Search()
|
|
{
|
|
this.InitializeComponent();
|
|
loading = grid.Children[2] as LoadingPage;
|
|
list = ((grid.Children[0] as ScrollViewer).Content as StackPanel).Children[4] as VideoGrid;
|
|
more = ((grid.Children[0] as ScrollViewer).Content as StackPanel).Children[5] as ShowMore;
|
|
}
|
|
|
|
public string SetResults(int? count)
|
|
{
|
|
if (count == 1000000)
|
|
return count + "+";
|
|
else if (count == null)
|
|
return "0";
|
|
else
|
|
return count.ToString();
|
|
}
|
|
|
|
public void AddItem(SearchResult result)
|
|
{
|
|
switch (result.Id.Kind)
|
|
{
|
|
case "youtube#video":
|
|
VideoCard vCard = new VideoCard(result.Id.VideoId);
|
|
list.Add(vCard);
|
|
break;
|
|
|
|
case "youtube#channel":
|
|
ChannelCard cCard = new ChannelCard(result.Id.ChannelId/*, result.Snippet.LiveBroadcastContent*/);
|
|
list.Add(cCard);
|
|
break;
|
|
|
|
case "youtube#playlist":
|
|
PlaylistCard pCard = new PlaylistCard(result.Id.PlaylistId);
|
|
list.Add(pCard);
|
|
break;
|
|
}
|
|
}
|
|
|
|
protected override void OnNavigatedTo(NavigationEventArgs e)
|
|
{
|
|
base.OnNavigatedTo(e);
|
|
if (e.Parameter == null)
|
|
loading.Error("NullReferenceException", "Unable to initialize search. Search term is not stated.");
|
|
else
|
|
{
|
|
if (e.Parameter is string)
|
|
Initialize(e.Parameter as string);
|
|
else if (e.Parameter is SearchParameters)
|
|
Initialize(e.Parameter as SearchParameters);
|
|
else
|
|
loading.Error("ArgumentException", "Wrong search parameters");
|
|
}
|
|
|
|
}
|
|
|
|
public void Initialize(SearchParameters arg)
|
|
{
|
|
Initialize(arg.Term, channelId: arg.ChannelId);
|
|
}
|
|
|
|
public async void Initialize(string term, bool forceInitialization = false, string channelId = null)
|
|
{
|
|
if (term == Term && !forceInitialization)
|
|
return;
|
|
loading.Refresh();
|
|
|
|
try
|
|
{
|
|
Term = term;
|
|
request = SecretsVault.Service.Search.List("id");
|
|
if (!string.IsNullOrWhiteSpace(channelId))
|
|
{
|
|
request.ChannelId = channelId;
|
|
(grid.Children[1] as CommandBar).Visibility = Visibility.Collapsed;
|
|
}
|
|
request.Q = term;
|
|
request.SafeSearch = (SearchResource.ListRequest.SafeSearchEnum)(int)settings.Values["safeSearch"];
|
|
try
|
|
{
|
|
request.RelevanceLanguage = settings.Values["region"].ToString().Remove(2).ToLower();
|
|
}
|
|
catch (ArgumentOutOfRangeException)
|
|
{
|
|
request.RelevanceLanguage = settings.Values["region"].ToString().ToLower();
|
|
}
|
|
request.MaxResults = 48;
|
|
request.Order = Order;
|
|
request.Type = Type;
|
|
request.PublishedAfter = Date;
|
|
|
|
if(type.SelectedIndex == 1)
|
|
{
|
|
request.VideoDuration = Duration;
|
|
if (features.SelectedItems.Count > 0)
|
|
{
|
|
request.Type = "video";
|
|
type.SelectedIndex = 1;
|
|
if (features.SelectedItems.Contains(features.Items[0]))
|
|
request.VideoDefinition = SearchResource.ListRequest.VideoDefinitionEnum.High;
|
|
if (features.SelectedItems.Contains(features.Items[1]))
|
|
request.VideoDimension = SearchResource.ListRequest.VideoDimensionEnum.Value3d;
|
|
if (features.SelectedItems.Contains(features.Items[2]))
|
|
request.VideoCaption = SearchResource.ListRequest.VideoCaptionEnum.ClosedCaption;
|
|
if (features.SelectedItems.Contains(features.Items[3]))
|
|
request.EventType = SearchResource.ListRequest.EventTypeEnum.Live;
|
|
if (features.SelectedItems.Contains(features.Items[4]))
|
|
request.VideoLicense = SearchResource.ListRequest.VideoLicenseEnum.CreativeCommon;
|
|
}
|
|
}
|
|
|
|
SearchListResponse response = await request.ExecuteAsync();
|
|
searchTerm.Text = $"Search results for: {Term}";
|
|
resultsCount.Text = $"Found: {SetResults(response.PageInfo.TotalResults)} item(s)";
|
|
if (!string.IsNullOrWhiteSpace(response.NextPageToken))
|
|
nextToken = response.NextPageToken;
|
|
else
|
|
more.Complete(true);
|
|
|
|
list.Clear();
|
|
foreach (SearchResult item in response.Items)
|
|
AddItem(item);
|
|
|
|
loading.Close();
|
|
}
|
|
catch
|
|
{
|
|
loading.Error();
|
|
}
|
|
}
|
|
|
|
private void toggleFilters_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
if(filters.Visibility == Visibility.Collapsed)
|
|
{
|
|
filters.Visibility = Visibility.Visible;
|
|
toggleFilters.Content = "Hide filters ";
|
|
}
|
|
else
|
|
{
|
|
filters.Visibility = Visibility.Collapsed;
|
|
toggleFilters.Content = "Show filters ";
|
|
}
|
|
}
|
|
|
|
SearchResource.ListRequest.OrderEnum Order
|
|
{
|
|
get
|
|
{
|
|
switch (order.SelectedIndex)
|
|
{
|
|
case 1:
|
|
return SearchResource.ListRequest.OrderEnum.Date;
|
|
case 2:
|
|
return SearchResource.ListRequest.OrderEnum.ViewCount;
|
|
case 3:
|
|
return SearchResource.ListRequest.OrderEnum.Rating;
|
|
case 4:
|
|
return SearchResource.ListRequest.OrderEnum.Title;
|
|
default:
|
|
return SearchResource.ListRequest.OrderEnum.Relevance;
|
|
}
|
|
}
|
|
}
|
|
|
|
string Type
|
|
{
|
|
get
|
|
{
|
|
switch(type.SelectedIndex)
|
|
{
|
|
case 1:
|
|
return "video";
|
|
case 2:
|
|
return "channel";
|
|
case 3:
|
|
return "playlist";
|
|
default:
|
|
return "video,channel,playlist";
|
|
}
|
|
}
|
|
}
|
|
|
|
DateTime? Date
|
|
{
|
|
get
|
|
{
|
|
switch(date.SelectedIndex)
|
|
{
|
|
case 1:
|
|
return DateTime.Now.Subtract(TimeSpan.FromHours(1));
|
|
case 2:
|
|
return DateTime.Now.Subtract(TimeSpan.FromDays(1));
|
|
case 3:
|
|
return DateTime.Now.Subtract(TimeSpan.FromDays(7));
|
|
case 4:
|
|
return DateTime.Now.Subtract(TimeSpan.FromDays(31));
|
|
case 5:
|
|
return DateTime.Now.Subtract(TimeSpan.FromDays(365));
|
|
default:
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
|
|
SearchResource.ListRequest.VideoDurationEnum Duration
|
|
{
|
|
get
|
|
{
|
|
switch (type.SelectedIndex)
|
|
{
|
|
case 1:
|
|
return SearchResource.ListRequest.VideoDurationEnum.Short__;
|
|
case 2:
|
|
return SearchResource.ListRequest.VideoDurationEnum.Medium;
|
|
case 3:
|
|
return SearchResource.ListRequest.VideoDurationEnum.Long__;
|
|
default:
|
|
return SearchResource.ListRequest.VideoDurationEnum.Any;
|
|
}
|
|
}
|
|
}
|
|
|
|
private void AppBarButton_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
Initialize(Term, true);
|
|
}
|
|
|
|
private async void more_Clicked()
|
|
{
|
|
request.PageToken = nextToken;
|
|
SearchListResponse response = await request.ExecuteAsync();
|
|
foreach (SearchResult item in response.Items)
|
|
AddItem(item);
|
|
|
|
if (response.NextPageToken != null)
|
|
{
|
|
nextToken = response.NextPageToken;
|
|
more.Complete();
|
|
}
|
|
else
|
|
more.Complete(true);
|
|
}
|
|
|
|
private void type_SelectionChanged(object sender, SelectionChangedEventArgs e)
|
|
{
|
|
try
|
|
{
|
|
if (type.SelectedIndex == 1)
|
|
{
|
|
duration.Visibility = Visibility.Visible;
|
|
featBtn.Visibility = Visibility.Visible;
|
|
}
|
|
else
|
|
{
|
|
duration.Visibility = Visibility.Collapsed;
|
|
featBtn.Visibility = Visibility.Collapsed;
|
|
}
|
|
}
|
|
catch (NullReferenceException) { }
|
|
}
|
|
|
|
private void inBrowser_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
|
|
}
|
|
}
|
|
}
|