Skip to content

Commit 965157d

Browse files
committed
Add login with cookies
1 parent 4eda961 commit 965157d

File tree

4 files changed

+96
-11
lines changed

4 files changed

+96
-11
lines changed

Console/Audio/PlayerController.cs

+10-10
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,12 @@ public class PlayerController : IAsyncDisposable
2323
private readonly ALContext _context;
2424
private readonly int _sourceId;
2525
private readonly ALFormat _targetFormat;
26+
private readonly YoutubeClient _youtubeClient;
2627
private Matroska? _matroskaPlayerBuffer = null;
2728
private AudioSender? _audioSender = null;
2829
private CancellationTokenSource _currentSongTokenSource = new();
2930
private bool _disposed = false;
3031

31-
private readonly YoutubeClient youtubeClient = new();
32-
3332
public event Action? StateChanged;
3433
public event Action<IEnumerable<IVideo>>? QueueChanged; //Maybe emit state to show a loading spinner
3534
public event Action? OnFinish;
@@ -54,8 +53,9 @@ public int Volume
5453
public IReadOnlyCollection<IVideo> Songs => _queue;
5554
public LoopState LoopState { get; set; }
5655

57-
public PlayerController()
56+
public PlayerController(YoutubeClient youtubeClient)
5857
{
58+
_youtubeClient = youtubeClient;
5959
_device = ALC.OpenDevice(Environment.GetEnvironmentVariable("DeviceName"));
6060
_context = ALC.CreateContext(_device, new ALContextAttributes());
6161
ALC.MakeContextCurrent(_context);
@@ -112,13 +112,13 @@ public async Task<List<ISearchResult>> SearchAsync(
112112
string query,
113113
CancellationToken token = default
114114
) =>
115-
await youtubeClient
115+
await _youtubeClient
116116
.Search.GetResultsAsync(query, token)
117117
.Take(50)
118118
.ToListAsync(cancellationToken: token);
119119

120120
public async Task<List<Recommendation>> GetRecommendationsAsync() =>
121-
await youtubeClient.Search.GetRecommendationsAsync().ToListAsync();
121+
await _youtubeClient.Search.GetRecommendationsAsync().ToListAsync();
122122

123123
public async Task SkipToAsync(IVideo video)
124124
{
@@ -139,10 +139,10 @@ public async Task SetAsync(Recommendation recommendation)
139139
_currentSongIndex = 0;
140140

141141
var firstVideo = recommendation.VideoId is not null
142-
? await youtubeClient.Videos.GetAsync(recommendation.VideoId.Value)
142+
? await _youtubeClient.Videos.GetAsync(recommendation.VideoId.Value)
143143
: null;
144144

145-
var playlist = await youtubeClient
145+
var playlist = await _youtubeClient
146146
.Playlists.GetVideosAsync(recommendation.PlaylistId)
147147
.ToListAsync();
148148

@@ -167,7 +167,7 @@ public async Task SetAsync(ISearchResult item)
167167

168168
if (item is PlaylistSearchResult playlistSearchResult)
169169
{
170-
var videos = await youtubeClient
170+
var videos = await _youtubeClient
171171
.Playlists.GetVideosAsync(playlistSearchResult.Id)
172172
.ToListAsync<IVideo>();
173173

@@ -176,7 +176,7 @@ public async Task SetAsync(ISearchResult item)
176176

177177
if (item is ChannelSearchResult channelSearchResult)
178178
{
179-
var videos = await youtubeClient
179+
var videos = await _youtubeClient
180180
.Channels.GetUploadsAsync(channelSearchResult.Id)
181181
.ToListAsync<IVideo>();
182182

@@ -224,7 +224,7 @@ public async Task PlayAsync()
224224
try
225225
{
226226
_matroskaPlayerBuffer = await Matroska.Create(
227-
new YtDownloadUrlHandler(youtubeClient, Song.Id),
227+
new YtDownloadUrlHandler(_youtubeClient, Song.Id),
228228
_audioSender,
229229
_currentSongTokenSource.Token
230230
);

Console/Cookies/Cookies.cs

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Globalization;
4+
using System.Net;
5+
6+
namespace Console.Cookies;
7+
8+
public static class Cookies
9+
{
10+
private static List<Cookie> ConvertToCookies(List<NetscapeCookie> netscapeCookies)
11+
{
12+
var cookies = new List<Cookie>();
13+
14+
foreach (var nc in netscapeCookies)
15+
{
16+
var cookie = new Cookie
17+
{
18+
Name = nc.Name,
19+
Value = nc.Value,
20+
Domain = nc.Domain,
21+
Path = nc.Path,
22+
Secure = nc.IsSecure,
23+
Expires = DateTimeOffset.FromUnixTimeSeconds(nc.Expiration).DateTime
24+
};
25+
26+
cookies.Add(cookie);
27+
}
28+
29+
return cookies;
30+
}
31+
32+
private static List<Cookie> ParseCookies(string filePath)
33+
{
34+
var cookies = new List<NetscapeCookie>();
35+
36+
foreach (var line in File.ReadLines(filePath))
37+
{
38+
// Skip comments and empty lines
39+
if (line.StartsWith("#") || string.IsNullOrWhiteSpace(line))
40+
continue;
41+
42+
// Split the line into parts
43+
var parts = line.Split('\t');
44+
if (parts.Length != 7)
45+
continue; // Invalid format, skip this line
46+
47+
var cookie = new NetscapeCookie(
48+
parts[0],
49+
parts[1] == "TRUE",
50+
parts[2],
51+
parts[3] == "TRUE",
52+
long.Parse(parts[4], NumberStyles.Integer, CultureInfo.InvariantCulture),
53+
parts[5],
54+
parts[6]
55+
);
56+
57+
cookies.Add(cookie);
58+
}
59+
60+
return ConvertToCookies(cookies);
61+
}
62+
63+
public static List<Cookie> GetCookies()
64+
{
65+
var cookiesPath = Environment.GetEnvironmentVariable("COOKIES_PATH");
66+
if (cookiesPath is null)
67+
return [];
68+
return ParseCookies(cookiesPath);
69+
}
70+
}

Console/Cookies/NetscapeCookie.cs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace Console.Cookies;
2+
3+
public record NetscapeCookie(
4+
string Domain,
5+
bool IsSecure,
6+
string Path,
7+
bool IsHttpOnly,
8+
long Expiration,
9+
string Name,
10+
string Value
11+
);

Console/Program.cs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
using System.Globalization;
22
using Console;
33
using Console.Audio;
4+
using Console.Cookies;
45
using Console.Views;
56
using Terminal.Gui;
7+
using YoutubeExplode;
68

79
Utils.ConfigurePlatformDependencies();
810

@@ -107,7 +109,9 @@
107109

108110
videosWin.Add(tabView);
109111

110-
await using var playerController = new PlayerController();
112+
var cookies = Cookies.GetCookies();
113+
var youtubeClient = new YoutubeClient(cookies);
114+
await using var playerController = new PlayerController(youtubeClient);
111115

112116
//TODO Add player shocuts here
113117
var statusBar = new StatusBar(

0 commit comments

Comments
 (0)