-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
114 lines (95 loc) · 3.97 KB
/
Program.cs
File metadata and controls
114 lines (95 loc) · 3.97 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
using System;
using System.Threading.Tasks;
using YoutubeExplode;
namespace YoutubeMp3Downloader
{
internal class Program
{
private const string OutputFolder = "DownloadedMusic";
static async Task Main(string[] args)
{
ILogger logger = new ConsoleLogger();
var youtubeClient = new YoutubeClient();
IYoutubeAudioDownloader downloader = new YoutubeAudioDownloader(youtubeClient, logger);
ShowMenu();
string choice = Console.ReadLine()?.Trim();
try
{
switch (choice)
{
case "1":
await HandleSingleDownload(downloader, logger);
break;
case "2":
await HandlePlaylistDownload(downloader, logger);
break;
case "3":
await HandleFileDownload(downloader, logger);
break;
default:
logger.Error("Invalid option. Please select 1, 2, or 3.");
break;
}
}
catch (Exception ex)
{
logger.Error($"An error occurred: {ex.Message}");
}
WaitForExit();
}
private static void ShowMenu()
{
Console.ForegroundColor = ConsoleColor.Magenta;
Console.WriteLine("╔══════════════════════════════════════╗");
Console.WriteLine("║ YouTube Mp3 Downloader v1.0 ║");
Console.WriteLine("╠══════════════════════════════════════╣");
Console.WriteLine("║ [1] Download single video ║");
Console.WriteLine("║ [2] Download playlist ║");
Console.WriteLine("║ [3] Download from .txt file ║");
Console.WriteLine("╚══════════════════════════════════════╝");
Console.ResetColor();
Console.Write("\nSelect an option: ");
}
private static async Task HandleSingleDownload(IYoutubeAudioDownloader downloader, ILogger logger)
{
Console.Write("Enter video URL: ");
string url = Console.ReadLine()?.Trim();
if (string.IsNullOrWhiteSpace(url))
{
logger.Error("URL cannot be empty.");
return;
}
await downloader.DownloadSingleAsync(url, OutputFolder);
logger.Success("Download completed.");
}
private static async Task HandlePlaylistDownload(IYoutubeAudioDownloader downloader, ILogger logger)
{
Console.Write("Enter playlist URL: ");
string url = Console.ReadLine()?.Trim();
if (string.IsNullOrWhiteSpace(url))
{
logger.Error("URL cannot be empty.");
return;
}
await downloader.DownloadPlaylistAsync(url, OutputFolder);
logger.Success("All downloads completed.");
}
private static async Task HandleFileDownload(IYoutubeAudioDownloader downloader, ILogger logger)
{
Console.Write("Enter .txt file path: ");
string filePath = Console.ReadLine()?.Trim();
if (string.IsNullOrWhiteSpace(filePath))
{
logger.Error("File path cannot be empty.");
return;
}
await downloader.DownloadFromFileAsync(filePath, OutputFolder);
logger.Success("All downloads completed.");
}
private static void WaitForExit()
{
Console.WriteLine("\nPress any key to exit...");
Console.ReadKey();
}
}
}