223 lines
8.8 KiB
C#
223 lines
8.8 KiB
C#
using Google.Apis.YouTube.v3;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.IO;
|
|
using System.Text.RegularExpressions;
|
|
using System.Web;
|
|
using Windows.ApplicationModel.Core;
|
|
using Windows.System;
|
|
using Windows.UI;
|
|
using Windows.UI.Xaml;
|
|
using Windows.UI.Xaml.Controls;
|
|
using Windows.UI.Xaml.Documents;
|
|
using Windows.UI.Xaml.Media;
|
|
|
|
namespace FoxTube
|
|
{
|
|
public static class Methods
|
|
{
|
|
public static bool NeedToResponse { get; set; } = false;
|
|
public static MainPage MainPage
|
|
{
|
|
get { return (Window.Current.Content as Frame).Content as MainPage; }
|
|
}
|
|
|
|
public static void CloseApp()
|
|
{
|
|
CoreApplication.Exit();
|
|
}
|
|
|
|
public static Uri ToUri(this string url)
|
|
{
|
|
return new Uri(url);
|
|
}
|
|
|
|
public static string ReplaceInvalidChars(this string str, char newValue)
|
|
{
|
|
foreach (char i in Path.GetInvalidFileNameChars())
|
|
str = str.Replace(i, newValue);
|
|
return str;
|
|
}
|
|
|
|
public static string Last(this string[] arr)
|
|
{
|
|
return arr[arr.Length - 1];
|
|
}
|
|
|
|
public static string GetAgo(DateTime dateTime)
|
|
{
|
|
TimeSpan span = DateTime.Now - dateTime;
|
|
|
|
if (span.TotalMinutes < 1)
|
|
return "Just now";
|
|
else if (Math.Round(span.TotalMinutes) == 1)
|
|
return "1 minute ago";
|
|
else if (span.TotalMinutes < 60)
|
|
return Math.Round(span.TotalMinutes) + " minutes ago";
|
|
else if (Math.Round(span.TotalHours) == 1)
|
|
return "1 hour ago";
|
|
else if (span.TotalHours < 24)
|
|
return Math.Round(span.TotalHours) + " hours ago";
|
|
else if (Math.Round(span.TotalDays) == 1)
|
|
return "1 day ago";
|
|
else if (span.TotalDays < 7)
|
|
return Math.Round(span.TotalDays) + " days ago";
|
|
else if (Math.Round(span.TotalDays) == 7)
|
|
return "1 week ago";
|
|
else if (span.TotalDays < 30)
|
|
return Math.Round(span.TotalDays / 7) + " weeks ago";
|
|
else if (Math.Round(span.TotalDays) == 30)
|
|
return "1 month ago";
|
|
else if (Math.Round(span.TotalDays) < 365)
|
|
return Math.Round(span.TotalDays / 30) + " months ago";
|
|
else if (Math.Round(span.TotalDays / 365) == 365)
|
|
return "1 year ago";
|
|
else
|
|
return Math.Round(span.TotalDays / 365) + " years ago";
|
|
}
|
|
|
|
public static void FormatText(ref TextBlock block, string text)
|
|
{
|
|
block.Inlines.Clear();
|
|
|
|
Regex regx = new Regex(@"(http(s)?://[\S]+|www.[\S]+|[\S]+@[\S]+)", RegexOptions.IgnoreCase);
|
|
Regex isWWW = new Regex(@"(http[s]?://[\S]+|www.[\S]+)");
|
|
Regex isEmail = new Regex(@"[\S]+@[\S]+");
|
|
foreach (string item in regx.Split(text))
|
|
{
|
|
if (isWWW.IsMatch(item))
|
|
{
|
|
try
|
|
{
|
|
Hyperlink link = new Hyperlink();
|
|
link.Click += (s, arg) => { ProcessLink(item); };
|
|
link.Inlines.Add(new Run { Text = item });
|
|
block.Inlines.Add(link);
|
|
}
|
|
catch
|
|
{
|
|
block.Inlines.Add(new Run { Text = item });
|
|
}
|
|
}
|
|
else if (isEmail.IsMatch(item))
|
|
{
|
|
try
|
|
{
|
|
Hyperlink link = new Hyperlink { NavigateUri = new Uri($"mailto:{item}"), Foreground = new SolidColorBrush(Colors.Red) };
|
|
link.Inlines.Add(new Run { Text = item });
|
|
block.Inlines.Add(link);
|
|
}
|
|
catch
|
|
{
|
|
block.Inlines.Add(new Run { Text = item });
|
|
}
|
|
}
|
|
else if (item == "s")
|
|
continue;
|
|
else
|
|
block.Inlines.Add(new Run { Text = item });
|
|
}
|
|
}
|
|
|
|
/*public static string QualityToString(YouTubeQuality quality)
|
|
{
|
|
switch(quality)
|
|
{
|
|
case YouTubeQuality.NotAvailable:
|
|
return "N/A";
|
|
case YouTubeQuality.Quality1080P:
|
|
return "1080p";
|
|
case YouTubeQuality.Quality144P:
|
|
return "144p";
|
|
case YouTubeQuality.Quality2160P:
|
|
return "2160p";
|
|
case YouTubeQuality.Quality240P:
|
|
return "240p";
|
|
case YouTubeQuality.Quality270P:
|
|
return "270p";
|
|
case YouTubeQuality.Quality360P:
|
|
return "360p";
|
|
case YouTubeQuality.Quality480P:
|
|
return "480p";
|
|
case YouTubeQuality.Quality520P:
|
|
return "520p";
|
|
case YouTubeQuality.Quality720P:
|
|
return "720p";
|
|
case YouTubeQuality.QualityHigh:
|
|
return "[Audio only] High quality";
|
|
case YouTubeQuality.QualityLow:
|
|
return "[Audio only] Low quality";
|
|
case YouTubeQuality.QualityMedium:
|
|
return "[Audio only] Medium quality";
|
|
default:
|
|
return "Unknown";
|
|
}
|
|
}*/
|
|
|
|
public async static void ProcessLink(string url)
|
|
{
|
|
try
|
|
{
|
|
Debug.WriteLine($"Processing link: {url}");
|
|
if (url.Contains("youtube.com/") || url.Contains("youtu.be/"))
|
|
{
|
|
Debug.WriteLine("This is an internal youtube link");
|
|
url = url.Replace("https://", "").Replace("http://", "").Replace("wwww.", "").Replace("//", "");
|
|
Debug.WriteLine($"Prepared link: {url}");
|
|
|
|
if (url.Contains("/playlist"))
|
|
{
|
|
Debug.WriteLine($"This is a playlist link. ID: {HttpUtility.ParseQueryString(url).Get("list")}");
|
|
MainPage.GoToPlaylist(HttpUtility.ParseQueryString(url).Get("list"));
|
|
}
|
|
else if (url.Contains("youtu.be/"))
|
|
{
|
|
Debug.WriteLine($"This is obfuscated video link. Video ID: {url.Split('/')[1]}");
|
|
MainPage.GoToVideo(url.Split('/')[1]);
|
|
}
|
|
else if (url.Contains("/watch"))
|
|
{
|
|
Debug.WriteLine($"This is regular video link. Video ID: {HttpUtility.ParseQueryString(url).Get("v")}");
|
|
MainPage.GoToVideo(HttpUtility.ParseQueryString(url).Get(0), HttpUtility.ParseQueryString(url).Get("list"));
|
|
}
|
|
else if (url.Contains("/v/"))
|
|
{
|
|
Debug.WriteLine($"This is video link. ID: {url.Split('/')[2].Split('?')[0]}");
|
|
MainPage.GoToVideo(url.Split('/')[2].Split('?')[0]);
|
|
}
|
|
else if (url.Contains("/channel/"))
|
|
{
|
|
Debug.WriteLine($"This is channel link. ID: {url.Split('/')[2]}");
|
|
MainPage.GoToChannel(url.Split('/')[2]);
|
|
}
|
|
else if (url.Contains("/user/"))
|
|
{
|
|
Debug.WriteLine($"This is channel link with username. Username: {url.Split('/')[2]}");
|
|
ChannelsResource.ListRequest request = SecretsVault.Service.Channels.List("id");
|
|
Debug.WriteLine(request.ForUsername = url.Split('/')[2]);
|
|
request.MaxResults = 1;
|
|
MainPage.GoToChannel((await request.ExecuteAsync()).Items[0].Id);
|
|
}
|
|
else if (url.Contains("/c/"))
|
|
{
|
|
Debug.WriteLine($"This is channel link with custom url. Custom name: {url.Split('/')[2]}");
|
|
SearchResource.ListRequest request = SecretsVault.Service.Search.List("id");
|
|
Debug.WriteLine(request.Q = url.Split('/')[2]);
|
|
request.MaxResults = 1;
|
|
MainPage.GoToChannel((await request.ExecuteAsync()).Items[0].Id.ChannelId);
|
|
}
|
|
else
|
|
throw new Exception();
|
|
}
|
|
else
|
|
throw new Exception();
|
|
}
|
|
catch
|
|
{
|
|
await Launcher.LaunchUriAsync(new Uri(url));
|
|
}
|
|
}
|
|
}
|
|
}
|