tytd/Tesses.YouTubeDownloader/TYTDClient.cs

275 lines
8.2 KiB
C#
Raw Normal View History

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-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 12:59:36 +00:00
await client.GetAsync($"{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 12:59:36 +00:00
await client.GetAsync($"{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 12:59:36 +00:00
await client.GetAsync($"{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 12:59:36 +00:00
await client.GetAsync($"{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 override async Task<long> GetLengthAsync(string path)
{
try{
2022-06-15 12:59:36 +00:00
var item=await client.GetAsync($"{url}api/Storage/File/{path}");
2022-04-10 00:19:07 +00:00
return item.Content.Headers.ContentLength.GetValueOrDefault();
}catch(Exception ex)
{
_=ex;
}
return 0;
}
public async override Task<Stream> OpenReadAsync(string path)
{
try{
2022-06-15 12:59:36 +00:00
Stream v=await client.GetStreamAsync($"{url}api/Storage/File/{path}");
2022-04-10 00:19:07 +00:00
return v;
}catch(Exception ex)
{
_=ex;
}
return Stream.Null;
}
2022-05-10 12:57:52 +00:00
2022-04-10 00:19:07 +00:00
}
}