2022-04-10 00:19:07 +00:00
|
|
|
using Newtonsoft.Json;
|
|
|
|
using YoutubeExplode.Videos.Streams;
|
|
|
|
using System.Linq;
|
|
|
|
using System;
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
using YoutubeExplode.Videos;
|
|
|
|
using System.Threading;
|
|
|
|
using YoutubeExplode.Exceptions;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.IO;
|
|
|
|
using YoutubeExplode.Channels;
|
|
|
|
using YoutubeExplode.Playlists;
|
|
|
|
using System.Net.Http;
|
2022-05-10 12:57:52 +00:00
|
|
|
using System.Net;
|
2022-06-24 23:02:51 +00:00
|
|
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
using YoutubeExplode.Utils.Extensions;
|
|
|
|
using System.Net.Http.Headers;
|
2022-11-01 08:55:28 +00:00
|
|
|
using System.Web;
|
2022-04-10 00:19:07 +00:00
|
|
|
|
|
|
|
namespace Tesses.YouTubeDownloader
|
|
|
|
{
|
2022-06-24 23:02:51 +00:00
|
|
|
|
|
|
|
//From YouTubeExplode
|
|
|
|
internal static class Helpers
|
|
|
|
{
|
|
|
|
public static async ValueTask<HttpResponseMessage> HeadAsync(
|
|
|
|
this HttpClient http,
|
|
|
|
string requestUri,
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Head, requestUri);
|
|
|
|
return await http.SendAsync(
|
|
|
|
request,
|
|
|
|
HttpCompletionOption.ResponseHeadersRead,
|
|
|
|
cancellationToken
|
|
|
|
);
|
|
|
|
}
|
|
|
|
public static async ValueTask<Stream> GetStreamAsync(
|
|
|
|
this HttpClient http,
|
|
|
|
string requestUri,
|
|
|
|
long? from = null,
|
|
|
|
long? to = null,
|
|
|
|
bool ensureSuccess = true,
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
using var request = new HttpRequestMessage(HttpMethod.Get, requestUri);
|
|
|
|
request.Headers.Range = new RangeHeaderValue(from, to);
|
|
|
|
|
|
|
|
var response = await http.SendAsync(
|
|
|
|
request,
|
|
|
|
HttpCompletionOption.ResponseHeadersRead,
|
|
|
|
cancellationToken
|
|
|
|
);
|
|
|
|
|
|
|
|
if (ensureSuccess)
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
return await response.Content.ReadAsStreamAsync();
|
|
|
|
}
|
|
|
|
|
|
|
|
public static async ValueTask<long?> TryGetContentLengthAsync(
|
|
|
|
this HttpClient http,
|
|
|
|
string requestUri,
|
|
|
|
bool ensureSuccess = true,
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
using var response = await http.HeadAsync(requestUri, cancellationToken);
|
|
|
|
|
|
|
|
if (ensureSuccess)
|
|
|
|
response.EnsureSuccessStatusCode();
|
|
|
|
|
|
|
|
return response.Content.Headers.ContentLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Special abstraction that works around YouTube's stream throttling
|
|
|
|
// and provides seeking support.
|
|
|
|
// From YouTubeExplode
|
|
|
|
internal class SegmentedHttpStream : Stream
|
|
|
|
{
|
|
|
|
private readonly HttpClient _http;
|
|
|
|
private readonly string _url;
|
|
|
|
private readonly long? _segmentSize;
|
|
|
|
|
|
|
|
private Stream _segmentStream;
|
|
|
|
private long _actualPosition;
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override bool CanRead => true;
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override bool CanSeek => true;
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override bool CanWrite => false;
|
|
|
|
|
|
|
|
public override long Length { get; }
|
|
|
|
|
|
|
|
public override long Position { get; set; }
|
|
|
|
|
|
|
|
public SegmentedHttpStream(HttpClient http, string url, long length, long? segmentSize)
|
|
|
|
{
|
|
|
|
_url = url;
|
|
|
|
_http = http;
|
|
|
|
Length = length;
|
|
|
|
_segmentSize = segmentSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
private void ResetSegmentStream()
|
|
|
|
{
|
|
|
|
_segmentStream?.Dispose();
|
|
|
|
_segmentStream = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
private async ValueTask<Stream> ResolveSegmentStreamAsync(
|
|
|
|
CancellationToken cancellationToken = default)
|
|
|
|
{
|
|
|
|
if (_segmentStream != null)
|
|
|
|
return _segmentStream;
|
|
|
|
|
|
|
|
var from = Position;
|
|
|
|
|
|
|
|
var to = _segmentSize != null
|
|
|
|
? Position + _segmentSize - 1
|
|
|
|
: null;
|
|
|
|
|
|
|
|
var stream = await _http.GetStreamAsync(_url, from, to, true, cancellationToken);
|
|
|
|
|
|
|
|
return _segmentStream = stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async ValueTask PreloadAsync(CancellationToken cancellationToken = default) =>
|
|
|
|
await ResolveSegmentStreamAsync(cancellationToken);
|
|
|
|
|
|
|
|
public override async Task<int> ReadAsync(byte[] buffer, int offset, int count, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
// Check if consumer changed position between reads
|
|
|
|
if (_actualPosition != Position)
|
|
|
|
ResetSegmentStream();
|
|
|
|
|
|
|
|
// Check if finished reading (exit condition)
|
|
|
|
if (Position >= Length)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
var stream = await ResolveSegmentStreamAsync(cancellationToken);
|
|
|
|
var bytesRead = await stream.ReadAsync(buffer, offset, count, cancellationToken);
|
|
|
|
_actualPosition = Position += bytesRead;
|
|
|
|
|
|
|
|
if (bytesRead != 0)
|
|
|
|
return bytesRead;
|
|
|
|
|
|
|
|
// Reached the end of the segment, try to load the next one
|
|
|
|
ResetSegmentStream();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override int Read(byte[] buffer, int offset, int count) =>
|
|
|
|
ReadAsync(buffer, offset, count).GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override long Seek(long offset, SeekOrigin origin) => Position = origin switch
|
|
|
|
{
|
|
|
|
SeekOrigin.Begin => offset,
|
|
|
|
SeekOrigin.Current => Position + offset,
|
|
|
|
SeekOrigin.End => Length + offset,
|
|
|
|
_ => throw new ArgumentOutOfRangeException(nameof(origin))
|
|
|
|
};
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override void Flush() =>
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override void SetLength(long value) =>
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
|
|
[ExcludeFromCodeCoverage]
|
|
|
|
public override void Write(byte[] buffer, int offset, int count) =>
|
|
|
|
throw new NotSupportedException();
|
|
|
|
|
|
|
|
protected override void Dispose(bool disposing)
|
|
|
|
{
|
|
|
|
base.Dispose(disposing);
|
|
|
|
|
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
ResetSegmentStream();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-10 00:19:07 +00:00
|
|
|
public class TYTDClient : TYTDBase,IDownloader
|
|
|
|
{
|
2023-01-10 17:03:44 +00:00
|
|
|
ExtraData data=null;
|
|
|
|
public override async Task<byte[]> ReadThumbnailAsync(VideoId videoId, string res, CancellationToken token = default)
|
|
|
|
{
|
|
|
|
if(CanReadThumbnailFromUrl())
|
|
|
|
{
|
|
|
|
return await client.GetByteArrayAsync($"{url}api/v2/Thumbnail?v={videoId.Value}&res={res}");
|
|
|
|
}
|
|
|
|
return await base.ReadThumbnailAsync(videoId,res,token);
|
|
|
|
}
|
|
|
|
private bool CanReadThumbnailFromUrl()
|
|
|
|
{
|
|
|
|
var data=GetExtraDataOnce();
|
|
|
|
Version v=new Version(data.TYTDServerVersion);
|
|
|
|
return v.Major >= 2;
|
|
|
|
}
|
2022-06-15 12:59:36 +00:00
|
|
|
string url;
|
2023-02-17 18:34:18 +00:00
|
|
|
public string Url {get{return url;}}
|
2022-04-10 00:19:07 +00:00
|
|
|
public TYTDClient(string url)
|
|
|
|
{
|
|
|
|
client=new HttpClient();
|
2022-06-15 12:59:36 +00:00
|
|
|
this.url = url.TrimEnd('/') + '/';
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public TYTDClient(HttpClient clt,string url)
|
|
|
|
{
|
|
|
|
client = clt;
|
2022-06-15 12:59:36 +00:00
|
|
|
this.url = url.TrimEnd('/') + '/';
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
public TYTDClient(HttpClient clt, Uri uri)
|
|
|
|
{
|
|
|
|
client=clt;
|
2022-06-15 12:59:36 +00:00
|
|
|
this.url = url.ToString().TrimEnd('/') + '/';
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
public TYTDClient(Uri uri)
|
|
|
|
{
|
|
|
|
client=new HttpClient();
|
2022-06-15 12:59:36 +00:00
|
|
|
this.url = url.ToString().TrimEnd('/') + '/';
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
HttpClient client;
|
|
|
|
public async Task AddChannelAsync(ChannelId id, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 13:28:43 +00:00
|
|
|
await client.GetStringAsync($"{url}api/v2/AddChannel?v={id.Value}&res={resolution.ToString()}");
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddPlaylistAsync(PlaylistId id, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 13:28:43 +00:00
|
|
|
await client.GetStringAsync($"{url}api/v2/AddPlaylist?v={id.Value}&res={resolution.ToString()}");
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddUserAsync(UserName userName, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
2023-01-10 17:03:44 +00:00
|
|
|
await client.GetStringAsync($"{url}api/v2/AddUser?id={userName.Value}&res={resolution.ToString()}");
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddVideoAsync(VideoId id, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 13:28:43 +00:00
|
|
|
await client.GetStringAsync($"{url}api/v2/AddVideo?v={id.Value}&res={resolution.ToString()}");
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 22:59:50 +00:00
|
|
|
public async Task AddFileAsync(string url,bool download=true)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
await client.GetStringAsync($"{url}api/v2/AddFile?url={WebUtility.UrlEncode(url)}&download={download}");
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-07-06 22:59:50 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-10 00:19:07 +00:00
|
|
|
public override async Task<bool> DirectoryExistsAsync(string path)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/Storage/DirectoryExists/{path}");
|
2022-04-10 00:19:07 +00:00
|
|
|
return v=="true";
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public async override IAsyncEnumerable<string> EnumerateDirectoriesAsync(string path)
|
|
|
|
{
|
|
|
|
List<string> items=null;
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/Storage/GetDirectory/{path}");
|
2022-04-10 00:19:07 +00:00
|
|
|
items=JsonConvert.DeserializeObject<List<string>>(v);
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(items==null)
|
|
|
|
{
|
|
|
|
yield break;
|
|
|
|
}else{
|
|
|
|
foreach(var item in items)
|
|
|
|
{
|
|
|
|
yield return await Task.FromResult(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-02-16 16:51:46 +00:00
|
|
|
public override async Task<(string Path,bool Delete)> GetRealUrlOrPathAsync(string path)
|
|
|
|
{
|
|
|
|
return await Task.FromResult(($"{url}api/Storage/File/{path.TrimStart('/')}",false));
|
|
|
|
}
|
2022-05-10 12:57:52 +00:00
|
|
|
public async IAsyncEnumerable<Subscription> GetSubscriptionsAsync()
|
|
|
|
{
|
2023-02-16 16:51:46 +00:00
|
|
|
|
2022-05-10 12:57:52 +00:00
|
|
|
string v="[]";
|
|
|
|
try{
|
2023-01-10 17:03:44 +00:00
|
|
|
v=await client.GetStringAsync($"{url}api/v2/subscriptions");
|
2022-05-10 12:57:52 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-05-10 12:57:52 +00:00
|
|
|
}
|
|
|
|
foreach(var item in JsonConvert.DeserializeObject<List<Subscription>>(v))
|
|
|
|
{
|
|
|
|
yield return await Task.FromResult(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public async Task UnsubscribeAsync(ChannelId id)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/unsubscribe?id={id.Value}");
|
2022-05-10 12:57:52 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-05-10 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 07:17:11 +00:00
|
|
|
public async Task SubscribeAsync(ChannelId id,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload)
|
2022-05-10 12:57:52 +00:00
|
|
|
{
|
|
|
|
try{
|
2022-11-01 07:17:11 +00:00
|
|
|
|
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/subscribe?id={id.Value}&conf={bellInfo.ToString()}");
|
2022-05-10 12:57:52 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-05-10 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task SubscribeAsync(UserName name,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/subscribe?id={ WebUtility.UrlEncode(name.Value)}&conf={info.ToString()}");
|
2022-05-10 12:57:52 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-05-10 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task ResubscribeAsync(ChannelId id,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/resubscribe?id={id.Value}&conf={info.ToString()}");
|
2022-05-10 12:57:52 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-05-10 12:57:52 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-10 00:19:07 +00:00
|
|
|
public async override IAsyncEnumerable<string> EnumerateFilesAsync(string path)
|
|
|
|
{
|
|
|
|
List<string> items=null;
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/Storage/GetFiles/{path}");
|
2022-04-10 00:19:07 +00:00
|
|
|
items=JsonConvert.DeserializeObject<List<string>>(v);
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(items==null)
|
|
|
|
{
|
|
|
|
yield break;
|
|
|
|
}else{
|
|
|
|
foreach(var item in items)
|
|
|
|
{
|
|
|
|
yield return await Task.FromResult(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async override Task<bool> FileExistsAsync(string path)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/Storage/FileExists/{path}");
|
2022-04-10 00:19:07 +00:00
|
|
|
return v=="true";
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2023-02-17 18:34:18 +00:00
|
|
|
public async Task<IReadOnlyList<(SavedVideo Video, Resolution Resolution)>> GetQueueListAsync()
|
2022-04-10 00:19:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/QueueList");
|
2022-04-10 00:19:07 +00:00
|
|
|
return JsonConvert.DeserializeObject<List<(SavedVideo Video,Resolution res)>>(v);
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new List<(SavedVideo video,Resolution resolution)>();
|
|
|
|
}
|
2023-02-17 18:34:18 +00:00
|
|
|
public async Task<SavedVideoProgress> GetProgressAsync()
|
2022-04-10 00:19:07 +00:00
|
|
|
{
|
|
|
|
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/Progress");
|
2022-04-10 00:19:07 +00:00
|
|
|
return JsonConvert.DeserializeObject<SavedVideoProgress>(v);
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public SavedVideoProgress GetProgress()
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
if(hadBeenListeningToEvents)
|
|
|
|
{
|
|
|
|
return progress;
|
|
|
|
}else{
|
|
|
|
return Task.Run(GetProgressAsync).GetAwaiter().GetResult();
|
|
|
|
}
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public IReadOnlyList<(SavedVideo Video, Resolution Resolution)> GetQueueList()
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
return Task.Run(GetQueueListAsync).GetAwaiter().GetResult();
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 23:02:51 +00:00
|
|
|
|
|
|
|
|
2022-04-10 00:19:07 +00:00
|
|
|
public async override Task<Stream> OpenReadAsync(string path)
|
|
|
|
{
|
2022-06-24 23:02:51 +00:00
|
|
|
|
2022-04-10 00:19:07 +00:00
|
|
|
try{
|
2022-06-24 23:02:51 +00:00
|
|
|
var strmLen= await client.TryGetContentLengthAsync($"{url}api/Storage/File/{path}",true);
|
|
|
|
SegmentedHttpStream v=new SegmentedHttpStream(client,$"{url}api/Storage/File/{path}",strmLen.GetValueOrDefault(),null);
|
|
|
|
return v;
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 23:02:51 +00:00
|
|
|
return Stream.Null;
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
2022-06-24 23:02:51 +00:00
|
|
|
public async Task AddToPersonalPlaylistAsync(string name, IEnumerable<ListContentItem> items)
|
|
|
|
{
|
2022-11-01 08:55:28 +00:00
|
|
|
foreach(var item in items)
|
|
|
|
{
|
|
|
|
var response = await client.GetStringAsync($"{url}api/v2/AddToList?name={WebUtility.UrlEncode(name)}&v={item.Id}&res={item.Resolution.ToString()}");
|
|
|
|
}
|
2022-06-24 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task ReplacePersonalPlaylistAsync(string name, IEnumerable<ListContentItem> items)
|
|
|
|
{
|
2023-02-24 02:59:09 +00:00
|
|
|
var content = new MultipartFormDataContent();
|
|
|
|
content.Add(new StringContent(name), "name");
|
|
|
|
content.Add(new StringContent(JsonConvert.SerializeObject(items.ToList())), "data");
|
|
|
|
await client.PostAsync($"{url}api/v2/ReplaceList",content);
|
2022-06-24 23:02:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public async Task RemoveItemFromPersonalPlaylistAsync(string name, VideoId id)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
|
|
await client.GetStringAsync($"{url}api/v2/DeleteFromList?name={WebUtility.UrlEncode(name)}&v={id.Value}");
|
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-06-24 23:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task SetResolutionForItemInPersonalPlaylistAsync(string name, VideoId id, Resolution resolution)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
|
|
await client.GetStringAsync($"{url}api/v2/SetResolutionInList?name={WebUtility.UrlEncode(name)}&v={id.Value}&res={resolution.ToString()}");
|
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-06-24 23:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
public void DeletePersonalPlaylist(string name)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
2023-02-24 02:59:09 +00:00
|
|
|
Task.Run(()=>client.GetStringAsync($"{url}api/v2/DeleteList?name={WebUtility.UrlEncode(name)}")).GetAwaiter().GetResult();
|
2022-06-24 23:02:51 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
2022-06-24 23:02:51 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-28 21:40:34 +00:00
|
|
|
|
|
|
|
public void CancelDownload(bool restart = false)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
|
|
|
|
|
|
|
|
client.GetStringAsync($"{url}api/v2/CancelDownload?restart={restart}").GetAwaiter().GetResult();
|
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
2022-11-01 07:17:11 +00:00
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SavedVideoProgress progress=new SavedVideoProgress();
|
|
|
|
CancellationTokenSource src=new CancellationTokenSource();
|
|
|
|
bool hadBeenListeningToEvents=false;
|
|
|
|
private async Task _startEventStreamAsync()
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
src=new CancellationTokenSource();
|
|
|
|
hadBeenListeningToEvents=true;
|
|
|
|
var sse=await client.GetSSEClientAsync($"{url}api/v2/event");
|
|
|
|
sse.Event += sse_Event;
|
|
|
|
await sse.ReadEventsAsync(src.Token);
|
|
|
|
|
|
|
|
}catch(Exception ex){
|
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
|
|
|
}
|
|
|
|
}
|
2023-02-17 18:34:18 +00:00
|
|
|
public void ResetEvents()
|
2022-11-01 07:17:11 +00:00
|
|
|
{
|
|
|
|
if(src != null) {
|
|
|
|
src.Cancel();
|
|
|
|
src.Dispose();
|
2023-02-16 17:43:41 +00:00
|
|
|
|
2022-11-01 07:17:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(hadBeenListeningToEvents)
|
|
|
|
{
|
|
|
|
hadBeenListeningToEvents=false;
|
|
|
|
_startEventStream();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
public void CancelEvents()
|
|
|
|
{
|
|
|
|
if(src!= null){
|
|
|
|
src.Cancel();
|
|
|
|
src.Dispose();
|
|
|
|
}
|
|
|
|
src=null;
|
|
|
|
hadBeenListeningToEvents=false;
|
|
|
|
}
|
|
|
|
private event EventHandler<VideoStartedEventArgs> _started_video;
|
|
|
|
private event EventHandler<VideoFinishedEventArgs> _ended_video;
|
|
|
|
private event EventHandler<VideoProgressEventArgs> _progress_video;
|
|
|
|
private event EventHandler<TYTDErrorEventArgs> _error;
|
|
|
|
private event EventHandler<BellEventArgs> _bell;
|
|
|
|
public event EventHandler<VideoStartedEventArgs> VideoStarted
|
|
|
|
{
|
|
|
|
add{
|
|
|
|
_started_video += value;
|
|
|
|
_startEventStream();
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
remove
|
|
|
|
{
|
|
|
|
_started_video-=value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public event EventHandler<VideoProgressEventArgs> VideoProgress
|
|
|
|
{
|
|
|
|
add{
|
|
|
|
_progress_video += value;
|
|
|
|
_startEventStream();
|
|
|
|
|
|
|
|
}
|
|
|
|
remove
|
|
|
|
{
|
|
|
|
_progress_video -=value;
|
2022-08-28 21:40:34 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-01 07:17:11 +00:00
|
|
|
public event EventHandler<VideoFinishedEventArgs> VideoFinished
|
|
|
|
{
|
|
|
|
add
|
|
|
|
{
|
|
|
|
_ended_video+=value;
|
|
|
|
_startEventStream();
|
|
|
|
|
|
|
|
}
|
|
|
|
remove
|
|
|
|
{
|
|
|
|
_ended_video -= value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public event EventHandler<TYTDErrorEventArgs> Error
|
|
|
|
{
|
|
|
|
add
|
|
|
|
{
|
|
|
|
_error += value;
|
|
|
|
_startEventStream();
|
|
|
|
}
|
|
|
|
remove
|
|
|
|
{
|
|
|
|
_error-=value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public event EventHandler<BellEventArgs> Bell
|
|
|
|
{
|
|
|
|
add
|
|
|
|
{
|
|
|
|
_bell+=value;
|
|
|
|
_startEventStream();
|
|
|
|
}
|
|
|
|
remove
|
|
|
|
{
|
|
|
|
_bell-=value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void sse_Event(object sender,SSEEvent evt)
|
|
|
|
{
|
|
|
|
bool started=false;bool ended=false;bool error=false;bool bell=false;
|
|
|
|
try{
|
|
|
|
ProgressItem item = evt.ParseJson<ProgressItem>();
|
|
|
|
if(item != null)
|
|
|
|
{
|
|
|
|
if(item.Video != null)
|
|
|
|
{
|
|
|
|
progress.Video = item.Video;
|
|
|
|
}
|
|
|
|
if(item.StartEvent)
|
|
|
|
{
|
|
|
|
started = true;
|
|
|
|
}
|
|
|
|
if(item.StopEvent)
|
|
|
|
{
|
|
|
|
ended =true;
|
|
|
|
}
|
|
|
|
if(item.BellEvent)
|
|
|
|
{
|
2023-02-17 18:34:18 +00:00
|
|
|
bell=true;
|
2022-11-01 07:17:11 +00:00
|
|
|
}
|
|
|
|
progress.Length = item.Length;
|
|
|
|
progress.ProgressRaw=item.Percent;
|
|
|
|
progress.Progress = ((int)(item.Percent * 100) % 101);
|
|
|
|
if(started)
|
|
|
|
{
|
|
|
|
VideoStartedEventArgs evt0=new VideoStartedEventArgs();
|
|
|
|
evt0.EstimatedLength=item.Length;
|
|
|
|
evt0.Resolution=Resolution.PreMuxed;
|
|
|
|
evt0.VideoInfo=progress.Video;
|
|
|
|
|
|
|
|
_started_video?.Invoke(this,evt0);
|
|
|
|
|
|
|
|
}else if(ended)
|
|
|
|
{
|
|
|
|
VideoFinishedEventArgs evt0=new VideoFinishedEventArgs();
|
|
|
|
evt0.Resolution=Resolution.PreMuxed;
|
|
|
|
evt0.VideoInfo=progress.Video;
|
|
|
|
_ended_video?.Invoke(this,evt0);
|
|
|
|
}else if(error)
|
|
|
|
{
|
|
|
|
TYTDErrorEventArgs evt0=new TYTDErrorEventArgs(item.Id,new TYTDException(item.Message,item.Error,item.ExceptionName));
|
|
|
|
_error?.Invoke(this,evt0);
|
|
|
|
}else if(bell)
|
|
|
|
{
|
|
|
|
BellEventArgs evt0=new BellEventArgs();
|
|
|
|
evt0.Id = item.Id;
|
|
|
|
_bell?.Invoke(this,evt0);
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
VideoProgressEventArgs evt0=new VideoProgressEventArgs();
|
|
|
|
evt0.Progress = item.Percent;
|
|
|
|
evt0.VideoInfo = progress.Video;
|
|
|
|
_progress_video?.Invoke(this,evt0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch(Exception ex)
|
|
|
|
{
|
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private void _startEventStream()
|
|
|
|
{
|
2023-02-17 18:34:18 +00:00
|
|
|
|
2023-02-16 17:43:41 +00:00
|
|
|
Task.Run(_startEventStreamAsync).Wait(0);
|
2023-02-17 18:34:18 +00:00
|
|
|
|
2022-11-01 07:17:11 +00:00
|
|
|
}
|
2023-01-10 17:03:44 +00:00
|
|
|
public ExtraData GetExtraDataOnce()
|
|
|
|
{
|
|
|
|
if(data ==null){
|
|
|
|
data= Task.Run<ExtraData>(async()=>{
|
|
|
|
string text= await client.GetStringAsync($"{url}api/v2/extra_data.json");
|
|
|
|
return JsonConvert.DeserializeObject<ExtraData>(text);
|
|
|
|
}).GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
2023-02-16 17:43:41 +00:00
|
|
|
public async Task<ExtraData> GetExtraDataOnceAsync()
|
|
|
|
{
|
|
|
|
if(data ==null)
|
|
|
|
{
|
|
|
|
string text= await client.GetStringAsync($"{url}api/v2/extra_data.json");
|
|
|
|
data = JsonConvert.DeserializeObject<ExtraData>(text);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
public async Task<ExtraData> GetExtraDataAsync()
|
|
|
|
{
|
|
|
|
string text= await client.GetStringAsync($"{url}api/v2/extra_data.json");
|
|
|
|
return JsonConvert.DeserializeObject<ExtraData>(text);
|
|
|
|
}
|
2022-11-01 07:17:11 +00:00
|
|
|
public override ExtraData GetExtraData()
|
|
|
|
{
|
2023-01-10 17:03:44 +00:00
|
|
|
return Task.Run<ExtraData>(async()=>{
|
2022-11-01 07:17:11 +00:00
|
|
|
string text= await client.GetStringAsync($"{url}api/v2/extra_data.json");
|
|
|
|
return JsonConvert.DeserializeObject<ExtraData>(text);
|
|
|
|
}).GetAwaiter().GetResult();
|
2023-01-10 17:03:44 +00:00
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddHandleAsync(ChannelHandle handle, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
await client.GetStringAsync($"{url}api/v2/AddHandle?id={handle.Value}&res={resolution.ToString()}");
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task AddSlugAsync(ChannelSlug handle, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
await client.GetStringAsync($"{url}api/v2/AddSlug?id={handle.Value}&res={resolution.ToString()}");
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
|
|
|
|
}
|
2022-11-01 07:17:11 +00:00
|
|
|
}
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|