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-16 11:12:27 +00:00
|
|
|
using Espresso3389.HttpStream;
|
2022-04-10 00:19:07 +00:00
|
|
|
|
|
|
|
namespace Tesses.YouTubeDownloader
|
|
|
|
{
|
|
|
|
public class TYTDClient : TYTDBase,IDownloader
|
|
|
|
{
|
2022-06-15 12:59:36 +00:00
|
|
|
string 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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public async Task AddUserAsync(UserName userName, Resolution resolution = Resolution.PreMuxed)
|
|
|
|
{
|
|
|
|
try{
|
2022-06-15 13:28:43 +00:00
|
|
|
await client.GetStringAsync($"{url}api/v2/AddUser?v={userName.Value}&res={resolution.ToString()}");
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(items==null)
|
|
|
|
{
|
|
|
|
yield break;
|
|
|
|
}else{
|
|
|
|
foreach(var item in items)
|
|
|
|
{
|
|
|
|
yield return await Task.FromResult(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-05-10 12:57:52 +00:00
|
|
|
public async IAsyncEnumerable<Subscription> GetSubscriptionsAsync()
|
|
|
|
{
|
|
|
|
string v="[]";
|
|
|
|
try{
|
2022-06-15 12:59:36 +00:00
|
|
|
v=await client.GetStringAsync("{url}api/v2/subscriptions");
|
2022-05-10 12:57:52 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
public async Task SubscribeAsync(ChannelId id,bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload)
|
|
|
|
{
|
|
|
|
try{
|
|
|
|
string dlcid=downloadChannelInfo ? "true" : "false";
|
2022-06-15 12:59:36 +00:00
|
|
|
string v=await client.GetStringAsync($"{url}api/v2/subscribe?id={id.Value}&conf={bellInfo.ToString()}&getinfo={dlcid}");
|
2022-05-10 12:57:52 +00:00
|
|
|
|
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
}
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
private async Task<IReadOnlyList<(SavedVideo Video, Resolution Resolution)>> GetQueueListAsync()
|
|
|
|
{
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new List<(SavedVideo video,Resolution resolution)>();
|
|
|
|
}
|
|
|
|
private async Task<SavedVideoProgress> GetProgressAsync()
|
|
|
|
{
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
public SavedVideoProgress GetProgress()
|
|
|
|
{
|
|
|
|
return GetProgressAsync().GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
public IReadOnlyList<(SavedVideo Video, Resolution Resolution)> GetQueueList()
|
|
|
|
{
|
|
|
|
return GetQueueListAsync().GetAwaiter().GetResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
public async override Task<Stream> OpenReadAsync(string path)
|
|
|
|
{
|
2022-06-16 11:12:27 +00:00
|
|
|
|
2022-04-10 00:19:07 +00:00
|
|
|
try{
|
2022-06-16 11:12:27 +00:00
|
|
|
|
|
|
|
HttpStream v=new HttpStream(new Uri($"{url}api/Storage/File/{path}"),new MemoryStream(),true,32 * 1024,null,client);
|
|
|
|
|
|
|
|
return await Task.FromResult(v);
|
2022-04-10 00:19:07 +00:00
|
|
|
}catch(Exception ex)
|
|
|
|
{
|
|
|
|
_=ex;
|
|
|
|
}
|
|
|
|
|
2022-06-16 11:12:27 +00:00
|
|
|
return await Task.FromResult(Stream.Null);
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-10 12:57:52 +00:00
|
|
|
|
2022-04-10 00:19:07 +00:00
|
|
|
}
|
|
|
|
}
|