-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainPage.xaml.cs
66 lines (57 loc) · 2.18 KB
/
MainPage.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
using YoutubeExplode;
namespace DownTube
{
public partial class MainPage : ContentPage
{
public MainPage()
{
InitializeComponent();
}
// The event handler for the download button
private async void OnLoadClicked(object sender, EventArgs e)
{
// Getting the video url entry
string enteredUrl = videoUrlEntry.Text;
// Disabling the load button to prevent overuse
loadButton.IsEnabled = false;
// Checing if the entry is even a URL
if (!Uri.TryCreate(enteredUrl, UriKind.Absolute, out _))
{
await DisplayAlert("Invalid URL", "Please enter a valid url", "OK");
loadButton.IsEnabled = true;
return;
}
// Event handler
try
{
// Creating a new youtube client
YoutubeClient youtubeClient = new YoutubeClient();
// Getting the video object
YoutubeExplode.Videos.Video video = await youtubeClient.Videos.GetAsync(enteredUrl);
// Opening a new video page with the video object
await Navigation.PushAsync(new VideoPage(video));
}
catch (YoutubeExplode.Exceptions.RequestLimitExceededException)
{
await DisplayAlert("Error", "You have exceeded the youtube rate limit. Possible causes:\n\n1) Using this app too much\n2) Using a VPN", "OK");
}
catch (System.Net.Http.HttpRequestException)
{
await DisplayAlert("Error", "Please check your internet connection.", "OK");
}catch (System.ArgumentException)
{
await DisplayAlert("Error", "Your entered URL was not a Youtube video.", "OK");
}
catch (Exception exception)
{
await DisplayAlert("Error", "We couldn't open the URL", "OK");
Console.WriteLine(exception);
}
finally
{
// Resetting the load button
loadButton.IsEnabled = true;
}
}
}
}