2022-05-03 07:56:28 +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 ;
using System.Text ;
namespace Tesses.YouTubeDownloader
{
2022-05-04 13:23:06 +00:00
internal class LockObj
{
}
2022-05-03 07:56:28 +00:00
public partial class TYTDStorage
{
2022-05-09 22:00:19 +00:00
internal LoggerProperties Properties { get ; set ; }
public LoggerProperties GetProperties ( )
{
CreateDirectoryIfNotExist ( "config" ) ;
CreateDirectoryIfNotExist ( "config/logs" ) ;
if ( FileExists ( "config/tytdprop.json" ) )
{
string data = ReadAllTextAsync ( "config/tytdprop.json" ) . GetAwaiter ( ) . GetResult ( ) ;
return JsonConvert . DeserializeObject < LoggerProperties > ( data ) ;
} else {
LoggerProperties prop = new LoggerProperties ( ) ;
prop . AddDateInLog = true ;
prop . LogVideoIds = true ;
prop . PrintErrors = false ;
prop . PrintVideoIds = true ;
prop . UseLogs = true ;
prop . SubscriptionInterval = TimeSpan . FromHours ( 1 ) ;
prop . AlwaysDownloadChannel = false ;
return prop ;
}
}
public LoggerProperties GetLoggerProperties ( )
{
if ( Properties = = null )
{
Properties = GetProperties ( ) ;
}
return Properties ;
}
2022-05-04 13:23:06 +00:00
internal static LockObj o = new LockObj ( ) ;
2022-05-03 07:56:28 +00:00
private Logger _log = null ;
public Logger GetLogger ( )
{
2022-05-04 13:23:06 +00:00
lock ( o ) {
if ( _log = = null )
{
_log = new Logger ( this ) ;
}
return _log ;
2022-05-03 07:56:28 +00:00
}
}
}
public class LoggerProperties
{
2022-05-09 22:00:19 +00:00
public bool AlwaysDownloadChannel { get ; set ; }
public TimeSpan SubscriptionInterval { get ; set ; }
2022-05-03 07:56:28 +00:00
public bool UseLogs { get ; set ; }
public bool PrintVideoIds { get ; set ; }
public bool PrintErrors { get ; set ; }
public bool LogVideoIds { get ; set ; }
public bool AddDateInLog { get ; set ; }
}
public class Logger
{
2022-05-04 13:23:06 +00:00
2022-05-03 07:56:28 +00:00
private string _filename ;
private TYTDStorage _storage ;
2022-05-09 22:00:19 +00:00
internal Logger ( TYTDStorage storage )
2022-05-03 07:56:28 +00:00
{
2022-05-09 22:00:19 +00:00
2022-05-03 07:56:28 +00:00
_storage = storage ;
string dateTime = DateTime . Now . ToString ( "yyyyMMdd_hhmmss" ) ;
_filename = $"config/logs/{dateTime}.log" ;
}
private void WriteStdErr ( string message )
{
var col = Console . ForegroundColor ;
Console . ForegroundColor = ConsoleColor . Red ;
Console . Error . WriteLine ( message ) ;
Console . ForegroundColor = col ;
}
private void WriteStd ( string message , bool error )
{
if ( error )
WriteStdErr ( message ) ;
else
Console . WriteLine ( message ) ;
}
2022-05-04 13:23:06 +00:00
public async Task WriteAsync ( string message , bool writeToConsole = false , bool isError = false , bool log = true )
{
await Task . Run ( ( ) = > {
Write ( message , writeToConsole , isError , log ) ;
} ) ;
}
public void Write ( string message , bool writeToConsole = false , bool isError = false , bool log = true )
2022-05-03 07:56:28 +00:00
{
if ( writeToConsole ) WriteStd ( message , isError ) ;
2022-05-09 22:00:19 +00:00
if ( ! log | | ! _storage . GetLoggerProperties ( ) . UseLogs ) return ;
2022-05-03 07:56:28 +00:00
// DateTime time = DateTime.Now;
var msg = new StringBuilder ( ) ;
2022-05-09 22:00:19 +00:00
if ( _storage . GetLoggerProperties ( ) . AddDateInLog )
2022-05-03 07:56:28 +00:00
{
var dat = DateTime . Now ;
msg . AppendLine ( $"{dat.ToShortDateString()} at {dat.ToShortTimeString()}:" ) ;
}
msg . AppendLine ( message ) ;
2022-05-04 13:23:06 +00:00
lock ( TYTDStorage . o ) {
try {
using ( var strm = _storage . OpenOrCreateAsync ( _filename ) . GetAwaiter ( ) . GetResult ( ) )
2022-05-03 07:56:28 +00:00
{
if ( ! strm . CanSeek ) return ;
strm . Seek ( 0 , SeekOrigin . End ) ;
using ( var sw = new StreamWriter ( strm ) )
{
2022-05-04 13:23:06 +00:00
sw . WriteLine ( msg . ToString ( ) ) ;
2022-05-03 07:56:28 +00:00
}
2022-05-04 13:23:06 +00:00
} }
catch ( Exception ex )
{
_ = ex ;
}
//mtx.ReleaseMutex();
}
2022-05-03 07:56:28 +00:00
}
public async Task WriteAsync ( Exception ex )
{
2022-05-09 22:00:19 +00:00
await WriteAsync ( $"Exception Catched:\n{ex.ToString()}" , _storage . GetLoggerProperties ( ) . PrintErrors , true ) ;
2022-05-03 07:56:28 +00:00
}
public async Task WriteAsync ( SavedVideo video )
{
2022-05-09 22:00:19 +00:00
await WriteAsync ( $"Download: {video.Title} with Id: {video.Id}" , _storage . GetLoggerProperties ( ) . PrintVideoIds , false , _storage . GetLoggerProperties ( ) . LogVideoIds ) ;
2022-05-03 07:56:28 +00:00
}
}
}