This repository was archived by the owner on Sep 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathGetUser.cs
More file actions
402 lines (333 loc) · 21.5 KB
/
Copy pathGetUser.cs
File metadata and controls
402 lines (333 loc) · 21.5 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
using System.Diagnostics;
using System.Net;
using System.Security;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using Namotion.Reflection;
using YoutubeDLSharp;
using YoutubeDLSharp.Options;
#pragma warning disable CS8602
#pragma warning disable CS8603
#pragma warning disable CS8604
#pragma warning disable CS0618
namespace Lincon
{
public static class UserFeeds
{
public static void HandleFeed(WebApplication app)
{
YoutubeDL ytdlp = new();
HttpClient client = new();
Dictionary<string, string> videoDict = [];
async Task<string> UseYTDlP(string query)
{
var options = new OptionSet
{
DumpSingleJson = true,
SkipDownload = true,
FlatPlaylist = true,
PlaylistStart = 1,
PlaylistEnd = 1
};
var res = await ytdlp.RunVideoDataFetch(query, overrideOptions: options);
if (res == null || res.Data == null)
{
Console.Error.WriteLine("YoutubeDL returned null or empty data.");
return "";
}
return res.Data.ToString();
}
Tuple<string, string, string, string, string, string, string> ExtractData(string data, HttpRequest request)
{
string id = "", channel_name = "", title = "", description = "", channel_follower_count = "", avatar_url = "", banner_url = "";
var res = JsonDocument.Parse(data);
var root = res.RootElement;
id = root.GetProperty("id").GetString() ?? "";
channel_name = root.GetProperty("channel").GetString() ?? "";
title = root.GetProperty("title").GetString() ?? "";
description = root.GetProperty("description").GetString() ?? "";
channel_follower_count = root.GetProperty("channel_follower_count").GetInt32().ToString() ?? "";
if (root.TryGetProperty("thumbnails", out JsonElement thumbnails) && thumbnails.ValueKind == JsonValueKind.Array && thumbnails.GetArrayLength() > 0)
{
var lastThumbnail = thumbnails[thumbnails.GetArrayLength() - 1];
if (lastThumbnail.TryGetProperty("url", out JsonElement urlProp))
{
avatar_url = urlProp.GetString() ?? "";
}
var firstThumbnail = thumbnails[0];
if (firstThumbnail.TryGetProperty("url", out JsonElement bannerProp))
{
banner_url = bannerProp.GetString() ?? "";
}
}
return Tuple.Create(id, channel_name, title, avatar_url, description, channel_follower_count, banner_url);
}
app.MapGet(@"/feeds/api/users/{channel_id}", async (string channel_id, HttpRequest request) => // needs to be ?q at some point for real hardware
{
try
{
if (String.IsNullOrEmpty(channel_id))
{
return Results.StatusCode(500);
}
var json = await UseYTDlP($"https://www.youtube.com/channel/{channel_id}");
var data = ExtractData(json, request);
var base_url = $"{request.Scheme}://{request.Host}{request.PathBase}";
var template = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
<entry
xmlns=""http://www.w3.org/2005/Atom""
xmlns:media=""http://search.yahoo.com/mrss/""
xmlns:gd=""http://schemas.google.com/g/2005""
xmlns:yt=""http://gdata.youtube.com/schemas/2007"">
<id>{base_url}/feeds/api/users/{data.Item1}</id>
<published>{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}</published>
<updated>{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}</updated>
<category scheme=""http://schemas.google.com/g/2005#kind"" term=""http://gdata.youtube.com/schemas/2007#userProfile""/>
<title type=""text"">{data.Item2} Channel</title>
<content type=""text""></content>
<link rel=""self"" type=""application/atom+xml"" href=""{base_url}/feeds/api/users/{data.Item1}""/>
<link rel=""alternate"" type=""text/html"" href=""https://www.youtube.com/user/{data.Item1}""/>
<author>
<name>{SecurityElement.Escape(data.Item1)}</name>
<uri>{base_url}/feeds/api/users/{data.Item1}</uri>
</author>
<yt:age>1</yt:age>
<yt:description></yt:description>
<yt:channelId>{channel_id}</yt:channelId>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.uploads"" href=""{base_url}/feeds/api/users/{data.Item1}/uploads"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.activities"" href=""{base_url}/feeds/api/users/{data.Item1}/activities"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.playlists"" href=""{base_url}/feeds/api/users/{data.Item1}/playlists"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.subscriptions"" href=""{base_url}/feeds/api/users/{data.Item1}/subscriptions"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.favorites"" href=""{base_url}/feeds/api/users/{data.Item1}/favorites"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.contacts"" href=""{base_url}/feeds/api/users/{data.Item1}/contacts"" countHint=""0""/>
<yt:statistics lastWebAccess=""{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}"" subscriberCount=""{data.Item6}"" videoWatchCount=""0"" viewCount=""0"" totalUploadViews=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#channel.content"" href=""{base_url}/feeds/api/users/webauditors/uploads?v=2"" countHint=""0""/>
<media:thumbnail url=""{data.Item4}""/>
<yt:username>{data.Item1}</yt:username>
</entry>";
return Results.Content(template, "application/xml"); // broken on firefox
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return Results.StatusCode(500);
}
});
app.MapGet(@"/feeds/api/channels/{channel_id}", async (string channel_id, HttpRequest request) => // needs to be ?q at some point for real hardware
{
try
{
if (String.IsNullOrEmpty(channel_id))
{
return Results.StatusCode(500);
}
var json = await UseYTDlP($"https://www.youtube.com/channel/{channel_id}");
var data = ExtractData(json, request);
var base_url = $"{request.Scheme}://{request.Host}{request.PathBase}";
var template = $@"<entry gd:etag='W/"Ck8GRH47eCp7I2A9XRdTGEQ."'>
<id>tag:youtube.com,2008:channel:{data.Item1}</id>
<updated>{DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ")}</updated>
<category scheme='http://schemas.google.com/g/2005#kind' term='/schemas/2007#channel'/>
<title>{data.Item2}</title>
<summary>{data.Item5}</summary>
<link rel='/schemas/2007#featured-video' type='application/atom+xml' href='/feeds/api/videos/YM582qGZHLI?v=2'/>
<link rel='alternate' type='text/html' href='https://www.youtube.com/channel/{data.Item1}'/>
<link rel='self' type='application/atom+xml' href='/feeds/api/channels/{data.Item1}?v=2'/>
<author>
<name>{data.Item1}</name>
<uri>/feeds/api/users/webauditors</uri>
<yt:userId>{data.Item1}</yt:userId>
</author>
<yt:channelId>{data.Item1}</yt:channelId>
<yt:channelStatistics subscriberCount='{data.Item6}' viewCount='0'/>
<gd:feedLink rel='/schemas/2007#channel.content' href='/feeds/api/users/webauditors/uploads?v=2' countHint='0'/>
<media:thumbnail url='{data.Item4}'/>
</entry>";
return Results.Content(template, "application/xml"); // broken on firefox
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return Results.StatusCode(500);
}
});
app.MapGet(@"/feeds/api/partners/{channel_id}/branding/default", async (string channel_id, HttpRequest request) => // needs to be ?q at some point for real hardware
{
try
{
if (String.IsNullOrEmpty(channel_id))
{
return Results.StatusCode(500);
}
var json = await UseYTDlP($"https://www.youtube.com/channel/{channel_id}");
var data = ExtractData(json, request);
var base_url = $"{request.Scheme}://{request.Host}{request.PathBase}";
var template = $@"<?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom'
xmlns:app='http://www.w3.org/2007/app'
xmlns:gd='http://schemas.google.com/g/2005'
xmlns:yt='http://gdata.youtube.com/schemas/2007'
gd:etag='W/""D0IDR347eCp7ImA9WxBbGU4.""'>
<id>tag:youtube.com,2008:partner:USERNAME:branding:default</id>
<published>2010-03-18T18:06:16.000Z</published>
<updated>2010-03-18T18:06:16.000Z</updated>
<app:edited>2010-03-18T18:06:16.000Z</app:edited>
<link rel='self' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/partners/USERNAME/branding/default?v=2'/>
<link rel='edit' type='application/atom+xml'
href='http://gdata.youtube.com/feeds/api/partners/USERNAME/branding/default?v=2'/>
<yt:option name='channel.global.title.string'>My title</yt:option>
<yt:option name='channel.global.description.string'>About my channel.</yt:option>
<yt:option name='channel.global.keywords.string'>some,channel,tags</yt:option>
<yt:option name='channel.background.image.url'>{data.Item6}</yt:option>
<yt:option name='channel.banner.image_height.int'>150</yt:option>
</entry>";
return Results.Content(template, "application/xml"); // broken on firefox
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return Results.StatusCode(500);
}
});
// this is used by newer IOS clients (the user doesn't notice)
// 1.00 to 1.1 versions do not use this
// and to pick between accounts on android
app.MapGet(@"/feeds/api/users", async (HttpRequest request) =>
{
try
{
var base_url = $"{request.Scheme}://{request.Host}{request.PathBase}";
var now = DateTime.UtcNow.ToString("yyyy-MM-ddTHH:mm:ss.fffZ");
string? device_id = null;
device_id = HandleLogin.ExtractDeviceIDFromRequest(request);
if (String.IsNullOrEmpty(device_id))
{
return Results.Problem("You must link your android device 3:", statusCode: 403);
}
var access_token = await HandleLogin.GetValidAccessTokenAsync(device_id);
var login_data = await HandleLogin.GetLoggedInAccountInfoAsync(access_token);
// YOU NEED TWO ENTRIES (otherwise the app will crash)
var template = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
<feed xmlns=""http://www.w3.org/2005/Atom""
xmlns:media=""http://search.yahoo.com/mrss/""
xmlns:gd=""http://schemas.google.com/g/2005""
xmlns:yt=""http://gdata.youtube.com/schemas/2007"">
<entry>
<id>{base_url}/feeds/api/users/default</id>
<published>{now}</published>
<updated>{now}</updated>
<category scheme=""http://schemas.google.com/g/2005#kind"" term=""http://gdata.youtube.com/schemas/2007#userProfile""/>
<title type=""text"">{login_data.Item1}</title>
<content type=""text"">{login_data.Item1} YouTube user profile.</content>
<link rel=""self"" type=""application/atom+xml"" href=""{base_url}/feeds/api/users/{login_data.Item2}""/>
<link rel=""alternate"" type=""text/html"" href=""https://www.youtube.com/user/{login_data.Item2}""/>
<author>
<name>{SecurityElement.Escape(login_data.Item1)}</name>
<uri>{base_url}/feeds/api/users/default</uri>
<email>default@example.com</email>
</author>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.uploads"" href=""{base_url}/feeds/api/users/{login_data.Item2}/uploads"" countHint=""0""/>
<yt:username>{login_data.Item1}</yt:username>
<yt:channelId>UC0000000000000000000000</yt:channelId>
<yt:googlePlusUserId>123456789012345678901</yt:googlePlusUserId>
<yt:age>1</yt:age>
<yt:location>Earth</yt:location>
<yt:gender>m</yt:gender>
<yt:incomplete>false</yt:incomplete>
<yt:eligibleForChannel>true</yt:eligibleForChannel>
<yt:statistics lastWebAccess=""{now}"" subscriberCount=""0"" videoWatchCount=""0"" viewCount=""0"" totalUploadViews=""0""/>
<media:thumbnail url=""{login_data.Item3}""/>
<yt:description>This is a YouTube user.</yt:description>
</entry>
<entry>
<id>{base_url}/feeds/api/users/fallback</id>
<published>{now}</published>
<updated>{now}</updated>
<category scheme=""http://schemas.google.com/g/2005#kind"" term=""http://gdata.youtube.com/schemas/2007#userProfile""/>
<title type=""text"">Fallback</title>
<content type=""text"">Second profile to satisfy app.</content>
<link rel=""self"" type=""application/atom+xml"" href=""{base_url}/feeds/api/users/fallback""/>
<link rel=""alternate"" type=""text/html"" href=""https://www.youtube.com/user/fallback""/>
<author>
<name>Fallback (Do Not Use)</name>
<uri>{base_url}/feeds/api/users/fallback</uri>
<email>fallback@example.com</email>
</author>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.uploads"" href=""{base_url}/feeds/api/users/fallback/uploads"" countHint=""0""/>
<yt:username>fallback</yt:username>
<yt:channelId>UC1111111111111111111111</yt:channelId>
<yt:googlePlusUserId>987654321098765432109</yt:googlePlusUserId>
<yt:age>2</yt:age>
<yt:location>Mars</yt:location>
<yt:gender>f</yt:gender>
<yt:incomplete>false</yt:incomplete>
<yt:eligibleForChannel>true</yt:eligibleForChannel>
<yt:statistics lastWebAccess=""{now}"" subscriberCount=""1"" videoWatchCount=""1"" viewCount=""1"" totalUploadViews=""1""/>
<media:thumbnail url=""https://yt3.ggpht.com/yti/ANjgQV8y24P02td9Sd_Xf1-bVBRdqqm_U00zYGqY6x43YrQ=s108-c-k-c0x00ffffff-no-rj""/>
<yt:description>Backup profile to stop crash.</yt:description>
</entry>
</feed>";
return Results.Content(template, "application/xml");
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return Results.StatusCode(500);
}
});
app.MapGet(@"/feeds/api/users/default", async (HttpRequest request) => // needs to be ?q at some point for real hardware
{
try
{
string? device_id = null;
device_id = HandleLogin.ExtractDeviceIDFromRequest(request);
if (string.IsNullOrEmpty(device_id))
return Results.Problem("Invalid device id header", statusCode: 403);
var access_token = await HandleLogin.GetValidAccessTokenAsync(device_id);
var login_data = await HandleLogin.GetLoggedInAccountInfoAsync(access_token);
Console.WriteLine("\nData: " + login_data.ToString());
var base_url = $"{request.Scheme}://{request.Host}{request.PathBase}";
var template = $@"<?xml version=""1.0"" encoding=""UTF-8""?>
<entry
xmlns=""http://www.w3.org/2005/Atom""
xmlns:media=""http://search.yahoo.com/mrss/""
xmlns:gd=""http://schemas.google.com/g/2005""
xmlns:yt=""http://gdata.youtube.com/schemas/2007"">
<id>{base_url}/feeds/api/users/default</id>
<published>{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}</published>
<updated>{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}</updated>
<category scheme=""http://schemas.google.com/g/2005#kind"" term=""http://gdata.youtube.com/schemas/2007#userProfile""/>
<title type=""text"">Default Channel</title>
<content type=""text""></content>
<link rel=""self"" type=""application/atom+xml"" href=""{base_url}/feeds/api/users/{login_data.Item2}""/>
<link rel=""alternate"" type=""text/html"" href=""https://www.youtube.com/user/default""/>
<author>
<name>{login_data.Item1}</name>
<uri>{base_url}/feeds/api/users/default</uri>
</author>
<yt:age>1</yt:age>
<yt:description></yt:description>
<yt:channelId>{login_data.Item3}</yt:channelId>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.subscriptions"" href=""{base_url}/feeds/api/users/default/subscriptions"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.contacts"" href=""{base_url}/feeds/api/users/default/contacts"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.inbox"" href=""{base_url}/feeds/api/users/default/inbox"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.playlists"" href=""{base_url}/feeds/api/users/default/playlists"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#user.uploads"" href=""{base_url}/feeds/api/users/default/uploads"" countHint=""0""/>
<gd:feedLink rel=""http://gdata.youtube.com/schemas/2007#channel.content"" href=""{base_url}/feeds/api/users/default/uploads?v=2"" countHint=""0""/>
<yt:statistics lastWebAccess=""{DateTime.UtcNow:yyyy-MM-ddTHH:mm:ss.fffZ}"" subscriberCount=""0"" videoWatchCount=""0"" viewCount=""0"" totalUploadViews=""0""/>
<media:thumbnail url=""{login_data.Item3}""/>
<yt:username>{login_data.Item1}</yt:username>
</entry>";
return Results.Content(template, "application/xml"); // broken on firefox
}
catch (Exception ex)
{
Console.Error.WriteLine(ex);
return Results.StatusCode(500);
}
});
}
}
}