Fix TYTDClient for blazor

This commit is contained in:
Mike Nolan 2023-02-16 11:43:41 -06:00
parent 26c9960003
commit e1046c33b5
4 changed files with 35 additions and 10 deletions

View File

@ -64,7 +64,7 @@ namespace Tesses.YouTubeDownloader
//Console.WriteLine("IN FUNC"); //Console.WriteLine("IN FUNC");
//Console.WriteLine("DIR"); //Console.WriteLine("DIR");
if(storage.BestStreamInfoExists(id)) if(await storage.BestStreamInfoExistsAsync(id))
{ {
//Console.WriteLine("STREAMS"); //Console.WriteLine("STREAMS");
BestStreamsSerialized serialization=await storage.GetBestStreamInfoAsync(id); BestStreamsSerialized serialization=await storage.GetBestStreamInfoAsync(id);
@ -83,7 +83,7 @@ namespace Tesses.YouTubeDownloader
{ {
if(storage.BestStreamInfoExists(id)) if(await storage.BestStreamInfoExistsAsync(id))
{ {
BestStreamsSerialized serialization=await storage.GetBestStreamInfoAsync(id); BestStreamsSerialized serialization=await storage.GetBestStreamInfoAsync(id);
if(DateTime.Now < serialization.Expires || !expire_check) if(DateTime.Now < serialization.Expires || !expire_check)
@ -99,7 +99,7 @@ namespace Tesses.YouTubeDownloader
DateTime expires=DateTime.Now.AddHours(6); DateTime expires=DateTime.Now.AddHours(6);
try{ try{
if(storage.VideoInfoExists(id)) if(await storage.VideoInfoExistsAsync(id))
{ {
var video = await storage.GetVideoInfoAsync(id); var video = await storage.GetVideoInfoAsync(id);
if(video.VideoFrozen) if(video.VideoFrozen)

View File

@ -128,7 +128,7 @@ namespace Tesses.YouTubeDownloader
{ {
string file = info.DownloadFrom.StartsWith("NormalDownload,Length=") ? $"DownloadsInfo/{HashDownloadUrl(info.Id)}.json" : $"Info/{info.Id}.json"; string file = info.DownloadFrom.StartsWith("NormalDownload,Length=") ? $"DownloadsInfo/{HashDownloadUrl(info.Id)}.json" : $"Info/{info.Id}.json";
if(!FileExists(file)) if(!await FileExistsAsync(file))
{ {
await WriteAllTextAsync(file,JsonConvert.SerializeObject(info)); await WriteAllTextAsync(file,JsonConvert.SerializeObject(info));
} }
@ -144,7 +144,7 @@ namespace Tesses.YouTubeDownloader
public virtual async Task WriteChannelInfoAsync(SavedChannel info) public virtual async Task WriteChannelInfoAsync(SavedChannel info)
{ {
string file = $"Channel/{info.Id}.json"; string file = $"Channel/{info.Id}.json";
if(!FileExists(file)) if(!await FileExistsAsync(file))
{ {
await WriteAllTextAsync(file,JsonConvert.SerializeObject(info)); await WriteAllTextAsync(file,JsonConvert.SerializeObject(info));
} }

View File

@ -552,11 +552,13 @@ internal class SegmentedHttpStream : Stream
_error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex)); _error.Invoke(this,new TYTDErrorEventArgs("jNQXAC9IVRw",ex));
} }
} }
public IBlazorContext BlazorContext {get;set;} = null;
public void ResetEvents() public void ResetEvents()
{ {
if(src != null) { if(src != null) {
src.Cancel(); src.Cancel();
src.Dispose(); src.Dispose();
} }
if(hadBeenListeningToEvents) if(hadBeenListeningToEvents)
@ -711,8 +713,17 @@ internal class SegmentedHttpStream : Stream
} }
private void _startEventStream() private void _startEventStream()
{ {
if(BlazorContext != null)
Task.Run(_startEventStreamAsync).Wait(0); {
BlazorContext.StartEvents((evt)=>{
sse_Event(this,new SSEEvent(evt));
});
src=new CancellationTokenSource();
hadBeenListeningToEvents=true;
src.Token.Register(BlazorContext.StopEvents);
}else{
Task.Run(_startEventStreamAsync).Wait(0);
}
} }
public ExtraData GetExtraDataOnce() public ExtraData GetExtraDataOnce()
{ {
@ -724,6 +735,20 @@ internal class SegmentedHttpStream : Stream
} }
return data; return data;
} }
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);
}
public override ExtraData GetExtraData() public override ExtraData GetExtraData()
{ {
return Task.Run<ExtraData>(async()=>{ return Task.Run<ExtraData>(async()=>{

View File

@ -7,9 +7,9 @@
<PackageId>Tesses.YouTubeDownloader</PackageId> <PackageId>Tesses.YouTubeDownloader</PackageId>
<Author>Mike Nolan</Author> <Author>Mike Nolan</Author>
<Company>Tesses</Company> <Company>Tesses</Company>
<Version>2.0.2.1</Version> <Version>2.0.2.2</Version>
<AssemblyVersion>2.0.2.1</AssemblyVersion> <AssemblyVersion>2.0.2.2</AssemblyVersion>
<FileVersion>2.0.2.1</FileVersion> <FileVersion>2.0.2.2</FileVersion>
<Description>A YouTube Downloader</Description> <Description>A YouTube Downloader</Description>
<PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression> <PackageLicenseExpression>GPL-3.0-only</PackageLicenseExpression>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance> <PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>