-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
260 lines (241 loc) · 10.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System.Text.RegularExpressions;
using Newtonsoft.Json.Linq;
using System.Diagnostics;
class Program
{
static string X_Authentication = "";
static long totalBytesDownloaded = 0;
static double totalElapsedTime = 0;
static async Task Main(string[] args)
{
Console.Clear();
Console.Title = "Medal.tv Clip Downloader";
string[] options = new string[] { "[1] Download All Profile Clips", "[2] Download a Clip" };
PrintMenu(options);
string choice = Console.ReadKey().KeyChar.ToString();
Console.WriteLine();
switch (choice)
{
case "1":
await DownloadAllClips();
break;
case "2":
await DownloadClip();
break;
default:
Console.WriteLine("Invalid choice.");
break;
}
}
static async Task DownloadAllClips()
{
Console.Clear();
PrintMenu(new string[] { "Enter the profile link" });
string profileLink = Console.ReadLine();
PrintMenu(new string[] { "Enter your X-Authentication Token" });
X_Authentication = Console.ReadLine();
var client = new HttpClient();
string html = await client.GetStringAsync(profileLink);
var regex = new Regex(@"(?<=""userId"":"")\d+");
var match = regex.Match(html);
string userId = match.Value;
long offset = 0;
bool finished = false;
try
{
while (!finished)
{
var request = new HttpRequestMessage
{
Method = HttpMethod.Get,
Headers =
{
{ "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0" },
{ "X-Authentication", X_Authentication },
},
};
request.RequestUri = new Uri($"https://medal.tv/api/content?userId={userId}&offset={offset}&sortBy=publishedAt&sortDirection=DESC");
var response = await client.SendAsync(request);
var body = await response.Content.ReadAsStringAsync();
var json = JArray.Parse(body);
offset = json.Count;
int i = 0;
if (json.Count == 0)
finished = true;
foreach (var clip in json)
{
i++;
var contentUrl1080p = clip["contentUrl1080p"].ToString();
var contentUrl720p = clip["contentUrl720p"].ToString();
var contentUrl480p = clip["contentUrl480p"].ToString();
var videoLengthSeconds = clip["videoLengthSeconds"].ToString();
string contentId = clip["contentId"].ToString();
string contentTitle = clip["contentTitle"].ToString().Replace(" ", "_").Replace(@"""", "");
if (contentTitle.Contains("Instant_Screenshot") && videoLengthSeconds == "1")
{
contentUrl1080p = clip["thumbnail1080p"].ToString();
contentUrl720p = clip["thumbnail720p"].ToString();
contentUrl480p = clip["thumbnail480p"].ToString();
}
if (await DownloadURL(contentUrl1080p, contentTitle, contentId, "1080p", i, offset))
continue;
else if (await DownloadURL(contentUrl720p, contentTitle, contentId, "720p", i, offset))
continue;
else if (await DownloadURL(contentUrl480p, contentTitle, contentId, "480p", i, offset))
continue;
}
}
}
catch (Exception e)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine(e.Message);
Console.ResetColor();
}
Console.Title = $"Downloaded {totalBytesDownloaded / 1024 / 1024} MB in {totalElapsedTime:0.00} seconds with an average download speed of {(totalBytesDownloaded / 1024d / 1024d) / totalElapsedTime:0.00} MB/s";
Thread.Sleep(1000);
await Main(null);
}
static async Task DownloadClip()
{
Console.Clear();
PrintMenu(new string[] { "Enter the clip link" });
string clipLink = Console.ReadLine();
var client = new HttpClient();
string html = await client.GetStringAsync(clipLink);
var regex = new Regex(@"(?<=var hydrationData=)[\s\S]*?(?=</script>)");
var match = regex.Match(html);
regex = new Regex(@"(?<=""contentId"":"")[\s\S]*?(?="")");
var cidmatch = regex.Match(match.Value);
string contentIdd = cidmatch.Value;
string json = match.Value;
JObject jjson = JObject.Parse(json);
JObject clips = JObject.Parse(jjson["clips"].ToString());
JObject clip = JObject.Parse(clips[contentIdd].ToString());
string contentUrl1080p = clip["contentUrl1080p"].ToString();
string contentUrl720p = clip["contentUrl720p"].ToString();
string contentUrl480p = clip["contentUrl480p"].ToString();
string videoLengthSeconds = clip["videoLengthSeconds"].ToString();
string contentId = clip["contentId"].ToString();
string contentTitle = clip["contentTitle"].ToString().Replace(" ", "_").Replace(@"""", "");
if (contentTitle.Contains("Instant_Screenshot") && videoLengthSeconds == "1")
{
contentUrl1080p = clip["thumbnail1080p"].ToString();
contentUrl720p = clip["thumbnail720p"].ToString();
contentUrl480p = clip["thumbnail480p"].ToString();
}
if (await DownloadURL(contentUrl1080p, contentTitle, contentId, "1080p", 1, 1))
{
Thread.Sleep(1000);
await Main(null);
}
else if (await DownloadURL(contentUrl720p, contentTitle, contentId, "720p", 1, 1))
{
Thread.Sleep(1000);
await Main(null);
}
else if (await DownloadURL(contentUrl480p, contentTitle, contentId, "480p", 1, 1))
{
Thread.Sleep(1000);
await Main(null);
}
}
static async Task<bool> DownloadURL(string url, string contentTitle, string contentId, string quality, long index, long max)
{
if (!Directory.Exists("Clips"))
Directory.CreateDirectory("Clips");
try
{
string fileextension = url.Split('.').Last();
fileextension = fileextension.Substring(0, fileextension.IndexOf("?"));
var client = new HttpClient();
var response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
var totalBytes = response.Content.Headers.ContentLength ?? -1L;
var canReportProgress = totalBytes != -1;
using (var stream = await response.Content.ReadAsStreamAsync())
{
var progress = new Progress<(long bytesDownloaded, double speed)>(report =>
{
var (bytesDownloaded, speed) = report;
if (canReportProgress)
{
Console.Write($"\r{contentTitle}.{fileextension}({index}/{max}): {bytesDownloaded}/{totalBytes} bytes ({(bytesDownloaded / (double)totalBytes) * 100:0.00}%) - Speed: {speed:0.00} MB/s ");
}
else
{
Console.Write($"\r{contentTitle}.{fileextension}({index}/{max}): {bytesDownloaded} bytes - Speed: {speed:0.00} MB/s ");
}
});
using (var fileStream = new FileStream($"Clips/{contentTitle}_{contentId}_{quality}.{fileextension}", FileMode.Create, FileAccess.Write, FileShare.None))
{
var stopwatch = Stopwatch.StartNew();
await CopyToAsync(stream, fileStream, 81920, progress);
stopwatch.Stop();
totalBytesDownloaded += totalBytes;
totalElapsedTime += stopwatch.Elapsed.TotalSeconds;
}
}
Console.WriteLine();
return true;
}
catch (Exception e)
{
Console.BackgroundColor = ConsoleColor.Red;
Console.ForegroundColor = ConsoleColor.Black;
Console.WriteLine($"Failed to download {contentTitle}_{contentId}_{quality}.mp4\nReason: {e.Message}");
Console.ResetColor();
return false;
}
}
static async Task CopyToAsync(Stream source, Stream destination, int bufferSize, IProgress<(long, double)> progress = null)
{
var buffer = new byte[bufferSize];
long totalBytesRead = 0;
int bytesRead;
var stopwatch = Stopwatch.StartNew();
while ((bytesRead = await source.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await destination.WriteAsync(buffer, 0, bytesRead);
totalBytesRead += bytesRead;
var elapsedSeconds = stopwatch.Elapsed.TotalSeconds;
var speed = (totalBytesRead / 1024d / 1024d) / elapsedSeconds;
progress?.Report((totalBytesRead, speed));
}
}
static void PrintMenu(string[] options)
{
// ┌┐└┘├┤┬┴┼│─» ►
string menu = "";
int longestoption = 0;
for (int i = 0; i < options.Length; i++)
{
if (options[i].Length > longestoption)
longestoption = options[i].Length;
}
menu += "┌";
for (int i = 0; i < longestoption + 2; i++)
{
menu += "─";
}
menu += "┐\n";
for (int i = 0; i < options.Length; i++)
{
menu += "│ ";
menu += options[i];
for (int j = 0; j < longestoption - options[i].Length; j++)
{
menu += " ";
}
menu += " │\n";
}
menu += "├";
for (int i = 0; i < longestoption + 2; i++)
{
menu += "─";
}
menu += "┘\n";
menu += "└[»] ";
Console.Write(menu);
}
}