tytd/Tesses.YouTubeDownloader/VideoQueue.cs

93 lines
2.5 KiB
C#
Raw Permalink Normal View History

2022-04-06 16:41:29 +00:00
using System;
using YoutubeExplode;
using YoutubeExplode.Videos;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Net.Http;
using Newtonsoft.Json;
using System.IO;
using YoutubeExplode.Playlists;
using YoutubeExplode.Channels;
namespace Tesses.YouTubeDownloader
{
public partial class TYTDStorage
{
/// <summary>
///Get video queue (Used by server)
/// </summary>
public IReadOnlyList<(SavedVideo Video,Resolution Resolution)> GetQueueList()
{
return QueueList;
}
/// <summary>
/// Get progress (Used by server)
/// </summary>
public SavedVideoProgress GetProgress()
{
return Progress;
}
2022-05-09 22:00:19 +00:00
List<Subscription> Subscriptions {get;set;}
2022-04-06 16:41:29 +00:00
List<(SavedVideo Video, Resolution Resolution)> QueueList = new List<(SavedVideo Video, Resolution Resolution)>();
List<IMediaContext> Temporary =new List<IMediaContext>();
2022-07-13 13:59:23 +00:00
public void WaitTillMediaContentQueueEmpty()
{
while(true)
{
lock(Temporary)
{
if(Temporary.Count <= 0) return;
}
Thread.Sleep(100);
}
}
2022-04-06 16:41:29 +00:00
private async Task QueueLoop(CancellationToken token)
{
2022-05-09 22:00:19 +00:00
try{
Subscriptions=new List<Subscription>();
await foreach(var sub in GetSubscriptionsAsync())
{
Subscriptions.Add(sub);
}
}catch(Exception ex)
{
await GetLogger().WriteAsync(ex);
}
2022-04-06 16:41:29 +00:00
while(!token.IsCancellationRequested)
2022-05-03 07:56:28 +00:00
{ try{
2022-05-09 22:00:19 +00:00
2022-04-06 16:41:29 +00:00
IMediaContext context;
lock(Temporary)
{
if(Temporary.Count > 0)
{
context=Temporary[0];
Temporary.RemoveAt(0);
}else{
context=null;
}
}
if(context != null)
{
await context.FillQueue(this,QueueList);
2022-05-03 07:56:28 +00:00
}
} catch(Exception ex)
{
//did this so app can keep running
await GetLogger().WriteAsync(ex);
2022-04-06 16:41:29 +00:00
}
}
2022-05-03 07:56:28 +00:00
2022-04-06 16:41:29 +00:00
}
}
}