This repository has been archived by the owner on Aug 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
184 lines (160 loc) · 5.68 KB
/
Program.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
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json.Linq;
namespace YA4KRGDLTool
{
class Program
{
static async Task Main(string[] args)
{
OnlineDLState app = new OnlineDLState();
await app.Run();
}
}
class OnlineDLState
{
private List<string> files;
private int curSelected = 0;
private string selectedFile;
private List<string> fileTexts;
public async Task Run()
{
Console.Title = "Moon4K Song Downloader - made by Celestial Studios";
Console.WriteLine("Choose a song to download:");
await FetchDirectoryListing("https://raw.githubusercontent.com/Celestial-Studioz/Moon4k-OnlineMaps/main/maps.json");
while (true)
{
Update();
await Task.Delay(100);
}
}
public void Update()
{
if (Console.KeyAvailable)
{
var key = Console.ReadKey(true).Key;
if (key == ConsoleKey.UpArrow || key == ConsoleKey.DownArrow)
{
ChangeSelection(key == ConsoleKey.UpArrow ? -1 : 1);
}
if (key == ConsoleKey.Enter && selectedFile != null)
{
DownloadFile(selectedFile);
}
}
}
public async Task FetchDirectoryListing(string url)
{
using (HttpClient client = new HttpClient())
{
try
{
var response = await client.GetStringAsync(url);
Console.WriteLine("Data received: " + response);
ParseDirectoryListing(response);
}
catch (Exception e)
{
Console.WriteLine("Failed to fetch directory listing: " + e.Message);
}
}
}
public void ParseDirectoryListing(string data)
{
var maps = JArray.Parse(data);
files = new List<string>();
fileTexts = new List<string>();
foreach (var map in maps)
{
var name = map["name"].ToString();
var downloadUrl = map["download"].ToString();
files.Add(downloadUrl);
fileTexts.Add(name);
Console.WriteLine(name);
}
if (files.Count > 0)
{
selectedFile = files[0];
ChangeSelection(0);
}
}
public void ChangeSelection(int change)
{
curSelected += change;
if (curSelected < 0) curSelected = 0;
if (curSelected >= files.Count) curSelected = files.Count - 1;
ClearConsole();
for (int i = 0; i < fileTexts.Count; i++)
{
Console.ForegroundColor = i == curSelected ? ConsoleColor.Red : ConsoleColor.White;
Console.WriteLine(fileTexts[i]);
}
selectedFile = files[curSelected];
}
public void ClearConsole()
{
Console.Clear();
Console.WriteLine("Choose a song to download:");
}
public async void DownloadFile(string fileUrl)
{
using (HttpClient client = new HttpClient())
{
client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
client.DefaultRequestHeaders.Add("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
client.DefaultRequestHeaders.Add("Accept-Language", "en-US,en;q=0.5");
client.DefaultRequestHeaders.Add("Connection", "keep-alive");
try
{
var data = await client.GetByteArrayAsync(fileUrl);
Console.WriteLine("Download complete: " + fileUrl);
SaveFile(fileUrl, data);
}
catch (Exception e)
{
Console.WriteLine("Failed to download file: " + e.Message);
}
}
}
public void SaveFile(string fileUrl, byte[] data)
{
var fileName = fileUrl.Substring(fileUrl.LastIndexOf("/") + 1);
var directoryPath = "assets/downloads/";
if (!Directory.Exists(directoryPath))
{
Directory.CreateDirectory(directoryPath);
}
var filePath = Path.Combine(directoryPath, fileName);
try
{
File.WriteAllBytes(filePath, data);
Console.WriteLine("File saved to: " + filePath);
ExtractZipFile(filePath, "assets/charts/");
}
catch (Exception e)
{
Console.WriteLine("Failed to save file: " + filePath + " - " + e.Message);
}
}
public void ExtractZipFile(string zipFilePath, string extractPath)
{
try
{
if (!Directory.Exists(extractPath))
{
Directory.CreateDirectory(extractPath);
}
ZipFile.ExtractToDirectory(zipFilePath, extractPath);
Console.WriteLine($"File extracted to: {extractPath}");
}
catch (Exception e)
{
Console.WriteLine($"Failed to extract file: {e.Message}");
}
}
}
}