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; using System.Net; namespace Tesses.YouTubeDownloader { public class TYTDClient : TYTDBase,IDownloader { string url; public TYTDClient(string url) { client=new HttpClient(); this.url = url.TrimEnd('/') + '/'; } public TYTDClient(HttpClient clt,string url) { client = clt; this.url = url.TrimEnd('/') + '/'; } public TYTDClient(HttpClient clt, Uri uri) { client=clt; this.url = url.ToString().TrimEnd('/') + '/'; } public TYTDClient(Uri uri) { client=new HttpClient(); this.url = url.ToString().TrimEnd('/') + '/'; } HttpClient client; public async Task AddChannelAsync(ChannelId id, Resolution resolution = Resolution.PreMuxed) { try{ await client.GetAsync($"{url}api/v2/AddChannel?v={id.Value}&res={resolution.ToString()}"); }catch(Exception ex) { _=ex; } } public async Task AddPlaylistAsync(PlaylistId id, Resolution resolution = Resolution.PreMuxed) { try{ await client.GetAsync($"{url}api/v2/AddPlaylist?v={id.Value}&res={resolution.ToString()}"); }catch(Exception ex) { _=ex; } } public async Task AddUserAsync(UserName userName, Resolution resolution = Resolution.PreMuxed) { try{ await client.GetAsync($"{url}api/v2/AddUser?v={userName.Value}&res={resolution.ToString()}"); }catch(Exception ex) { _=ex; } } public async Task AddVideoAsync(VideoId id, Resolution resolution = Resolution.PreMuxed) { try{ await client.GetAsync($"{url}api/v2/AddVideo?v={id.Value}&res={resolution.ToString()}"); }catch(Exception ex) { _=ex; } } public override async Task DirectoryExistsAsync(string path) { try{ string v=await client.GetStringAsync($"{url}api/Storage/DirectoryExists/{path}"); return v=="true"; }catch(Exception ex) { _=ex; } return false; } public async override IAsyncEnumerable EnumerateDirectoriesAsync(string path) { List items=null; try{ string v=await client.GetStringAsync($"{url}api/Storage/GetDirectory/{path}"); items=JsonConvert.DeserializeObject>(v); }catch(Exception ex) { _=ex; } if(items==null) { yield break; }else{ foreach(var item in items) { yield return await Task.FromResult(item); } } } public async IAsyncEnumerable GetSubscriptionsAsync() { string v="[]"; try{ v=await client.GetStringAsync("{url}api/v2/subscriptions"); }catch(Exception ex) { _=ex; } foreach(var item in JsonConvert.DeserializeObject>(v)) { yield return await Task.FromResult(item); } } public async Task UnsubscribeAsync(ChannelId id) { try{ string v=await client.GetStringAsync($"{url}api/v2/unsubscribe?id={id.Value}"); }catch(Exception ex) { _=ex; } } public async Task SubscribeAsync(ChannelId id,bool downloadChannelInfo=false,ChannelBellInfo bellInfo = ChannelBellInfo.NotifyAndDownload) { try{ string dlcid=downloadChannelInfo ? "true" : "false"; string v=await client.GetStringAsync($"{url}api/v2/subscribe?id={id.Value}&conf={bellInfo.ToString()}&getinfo={dlcid}"); }catch(Exception ex) { _=ex; } } public async Task SubscribeAsync(UserName name,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload) { try{ string v=await client.GetStringAsync($"{url}api/v2/subscribe?id={ WebUtility.UrlEncode(name.Value)}&conf={info.ToString()}"); }catch(Exception ex) { _=ex; } } public async Task ResubscribeAsync(ChannelId id,ChannelBellInfo info=ChannelBellInfo.NotifyAndDownload) { try{ string v=await client.GetStringAsync($"{url}api/v2/resubscribe?id={id.Value}&conf={info.ToString()}"); }catch(Exception ex) { _=ex; } } public async override IAsyncEnumerable EnumerateFilesAsync(string path) { List items=null; try{ string v=await client.GetStringAsync($"{url}api/Storage/GetFiles/{path}"); items=JsonConvert.DeserializeObject>(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 FileExistsAsync(string path) { try{ string v=await client.GetStringAsync($"{url}api/Storage/FileExists/{path}"); return v=="true"; }catch(Exception ex) { _=ex; } return false; } private async Task> GetQueueListAsync() { try{ string v=await client.GetStringAsync($"{url}api/v2/QueueList"); return JsonConvert.DeserializeObject>(v); }catch(Exception ex) { _=ex; } return new List<(SavedVideo video,Resolution resolution)>(); } private async Task GetProgressAsync() { try{ string v=await client.GetStringAsync($"{url}api/v2/Progress"); return JsonConvert.DeserializeObject(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 override async Task GetLengthAsync(string path) { try{ var item=await client.GetAsync($"{url}api/Storage/File/{path}"); return item.Content.Headers.ContentLength.GetValueOrDefault(); }catch(Exception ex) { _=ex; } return 0; } public async override Task OpenReadAsync(string path) { try{ Stream v=await client.GetStreamAsync($"{url}api/Storage/File/{path}"); return v; }catch(Exception ex) { _=ex; } return Stream.Null; } } }