Skip to content

Commit

Permalink
Add *.ass to the default subtitle extensions
Browse files Browse the repository at this point in the history
Add a wip synchronization result. It only displays different counts such as skipped, failed and succeeded. I will need to rewrite this later to add support for listing the actual files that failed.
  • Loading branch information
Zerratar committed Apr 8, 2018
1 parent 4f109b6 commit 7551a99
Show file tree
Hide file tree
Showing 10 changed files with 121 additions and 45 deletions.
2 changes: 1 addition & 1 deletion src/SubSync/Core/IFileSystemWatcher.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;

namespace SubSync.Processors
namespace SubSync
{
internal interface IFileSystemWatcher : IDisposable
{
Expand Down
4 changes: 1 addition & 3 deletions src/SubSync/Core/IWorkerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using SubSync.Processors;

namespace SubSync
namespace SubSync
{
internal interface IWorkerProvider
{
Expand Down
3 changes: 2 additions & 1 deletion src/SubSync/Core/IWorkerQueue.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
using System;

namespace SubSync.Processors
namespace SubSync
{
internal interface IWorkerQueue : IDisposable
{
event EventHandler<QueueCompletedEventArgs> QueueCompleted;
void Enqueue(string fullFilePath);
void Enqueue(IWorker worker);
void Start();
Expand Down
18 changes: 18 additions & 0 deletions src/SubSync/Core/QueueCompletedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;

namespace SubSync
{
internal class QueueCompletedEventArgs : EventArgs
{
public QueueCompletedEventArgs(int total, int succeeded, int failed)
{
Total = total;
Succeeded = succeeded;
Failed = failed;
}

public int Total { get; }
public int Succeeded { get; }
public int Failed { get; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using SubSync.Extensions;

namespace SubSync.Processors
namespace SubSync
{
internal class FileSystemWatcher : IFileSystemWatcher
internal class SubtitleSynchronizer : IFileSystemWatcher
{
private readonly ILogger logger;
private readonly IWorkerQueue workerQueue;
Expand All @@ -15,8 +16,9 @@ internal class FileSystemWatcher : IFileSystemWatcher
private readonly HashSet<string> subtitleExtensions;
private System.IO.FileSystemWatcher fsWatcher;
private bool disposed;
private int skipped = 0;

public FileSystemWatcher(
public SubtitleSynchronizer(
ILogger logger,
IWorkerQueue workerQueue,
string input,
Expand Down Expand Up @@ -50,20 +52,52 @@ public void Start()
NotifyFilters.Size | NotifyFilters.Security;

this.fsWatcher.Created += FileCreated;
this.fsWatcher.Renamed += FileCreated;
this.workerQueue.QueueCompleted += ResultReport;
this.workerQueue.Start();
this.SyncAll();
}

public void Stop()
{
if (this.fsWatcher == null) return;
this.workerQueue.QueueCompleted -= ResultReport;
this.workerQueue.Stop();
this.fsWatcher.Error -= FsWatcherOnError;
this.fsWatcher.Created -= FileCreated;
this.fsWatcher.Renamed -= FileCreated;
this.fsWatcher.Dispose();
this.fsWatcher = null;
}

private void ResultReport(object sender, QueueCompletedEventArgs e)
{
var skipcount = Interlocked.Exchange(ref skipped, 0);
var total = e.Total;
var failed = e.Failed;
var success = e.Succeeded;

if (total == 0 && skipcount == 0)
{
return;
}

this.logger.WriteLine($"");
this.logger.WriteLine($" ═════════════════════════════════════════════════════");
this.logger.WriteLine($"");
this.logger.WriteLine($" @whi@Synchronization completed with a total of @yel@{total} @whi@video(s) processed.");

if (success > 0)
{
this.logger.WriteLine($" @green@{success} @whi@video(s) was successefully was synchronized.");
}
if (failed > 0)
{
this.logger.WriteLine($" @red@{success} @whi@video(s) failed to synchronize.");
}
this.logger.WriteLine($" {skipcount} video(s) was skipped.");
}

public void SyncAll()
{
var directoryInfo = new DirectoryInfo(this.input);
Expand All @@ -77,6 +111,7 @@ private void Sync(string fullFilePath)
{
if (IsSynchronized(fullFilePath))
{
Interlocked.Increment(ref skipped);
return;
}

Expand Down
61 changes: 36 additions & 25 deletions src/SubSync/Core/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Threading;
using System.Threading.Tasks;
using SharpCompress.Archives;
using SubSync.Processors;

namespace SubSync
{
Expand All @@ -22,7 +21,7 @@ internal class Worker : IWorker
".zip", ".rar", ".gzip", ".gz", ".7z", ".tar", ".tar.gz"
};

private readonly TaskCompletionSource<object> taskCompletionSource = new TaskCompletionSource<object>();
private TaskCompletionSource<object> taskCompletionSource;

private int syncCount;

Expand All @@ -42,42 +41,54 @@ public Worker(

public Task SyncAsync()
{
if (taskCompletionSource == null
|| taskCompletionSource.Task.Status == TaskStatus.RanToCompletion
|| taskCompletionSource.Task.Status == TaskStatus.Canceled
|| taskCompletionSource.Task.Status == TaskStatus.Faulted)
{
taskCompletionSource = new TaskCompletionSource<object>();
}

try
{
return taskCompletionSource.Task;
}
finally
{
Task.Factory.StartNew(async () =>
if (Volatile.Read(ref this.syncCount) == 0)
{
var counter = Interlocked.Increment(ref this.syncCount);
var file = new FileInfo(filePath);
this.logger.WriteLine($"Synchronizing {file.Name}");
try
Task.Factory.StartNew(async () =>
{
var directory = file.Directory?.FullName ?? "./";
var outputName = await subtitleProvider.GetAsync(file.Name, directory);
var extension = Path.GetExtension(outputName);
if (IsCompressed(extension))
var counter = Interlocked.Increment(ref this.syncCount);
var file = new FileInfo(filePath);
this.logger.WriteLine($"Synchronizing {file.Name}");
try
{
outputName = await DecompressAsync(outputName);
}

var finalName = Rename(outputName, Path.GetFileNameWithoutExtension(file.Name));
this.logger.WriteLine($"@gray@Subtitle @white@{Path.GetFileName(finalName)} @green@downloaded!");
}
catch (Exception exc)
{
this.logger.Error($"Synchronization of {file.Name} failed with: ${exc.Message}");
var directory = file.Directory?.FullName ?? "./";
var outputName = await subtitleProvider.GetAsync(file.Name, directory);
var extension = Path.GetExtension(outputName);
if (IsCompressed(extension))
{
outputName = await DecompressAsync(outputName);
}

if (counter <= RetryLimit)
var finalName = Rename(outputName, Path.GetFileNameWithoutExtension(file.Name));
this.logger.WriteLine($"@gray@Subtitle @white@{Path.GetFileName(finalName)} @green@downloaded!");
}
catch (Exception exc)
{
this.workerQueue.Enqueue(this);
this.logger.Error($"Synchronization of {file.Name} failed with: ${exc.Message}");

if (counter <= RetryLimit)
{
this.workerQueue.Enqueue(this);
}
}
}

this.taskCompletionSource.SetResult(true);
}, TaskCreationOptions.LongRunning);
Interlocked.Decrement(ref this.syncCount);
this.taskCompletionSource.SetResult(true);
}, TaskCreationOptions.LongRunning);
}
}
}

Expand Down
1 change: 0 additions & 1 deletion src/SubSync/Core/WorkerProvider.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Collections.Generic;
using SubSync.Processors;

namespace SubSync
{
Expand Down
24 changes: 19 additions & 5 deletions src/SubSync/Core/WorkerQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;

namespace SubSync.Processors
namespace SubSync
{
internal class WorkerQueue : IWorkerQueue
{
public event EventHandler<QueueCompletedEventArgs> QueueCompleted;
private const int ConcurrentWorkers = 7;
private readonly IWorkerProvider workerProvider;
private readonly ConcurrentQueue<IWorker> queue = new ConcurrentQueue<IWorker>();
private readonly Thread workerThread;
private bool enabled;
private bool disposed;

private int currentJobCount = 0;
private int queueSize = 0;

public WorkerQueue(IWorkerProvider workerProvider)
{
Expand All @@ -39,6 +38,7 @@ public void Enqueue(string fullFilePath)

public void Enqueue(IWorker worker)
{
Interlocked.Increment(ref queueSize);
queue.Enqueue(worker);
}

Expand All @@ -59,6 +59,9 @@ public void Stop()
private async void ProcessQueue()
{
var activeJobs = new List<Task>();
var resultReported = false;
var failed = 0;
var succeeded = 0;
do
{
while (activeJobs.Count < ConcurrentWorkers && this.queue.TryDequeue(out var worker))
Expand All @@ -68,12 +71,23 @@ private async void ProcessQueue()

if (activeJobs.Count > 0)
{
resultReported = false;
await Task.WhenAny(activeJobs);

failed += activeJobs.Count(x => x.IsFaulted && x.IsCompleted);
succeeded += activeJobs.Count(x => !x.IsFaulted && x.IsCompleted);
activeJobs = activeJobs.Where(x => !x.IsCompleted).ToList();
}
else
{
if (!resultReported && QueueCompleted != null)
{
QueueCompleted?.Invoke(this, new QueueCompletedEventArgs(Volatile.Read(ref queueSize), succeeded, failed));
resultReported = true;
}

succeeded = 0;
failed = 0;
Volatile.Write(ref queueSize, 0);
System.Threading.Thread.Sleep(100);
}

Expand Down
9 changes: 4 additions & 5 deletions src/SubSync/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using SubSync.Processors;

namespace SubSync
{
Expand All @@ -13,7 +12,7 @@ static void Main(string[] args)
{
var input = "./";
var videoExtensions = ParseList("*.avi;*.mp4;*.mkv;*.mpeg;*.flv;*.webm");
var subtitleExtensions = ParseList("*.srt;*.txt;*.sub;*.idx;*.ssa");
var subtitleExtensions = ParseList("*.srt;*.txt;*.sub;*.idx;*.ssa;*.ass");
var languages = ParseList("english");

if (args.Length > 0 && !string.IsNullOrEmpty(args[0]))
Expand All @@ -35,14 +34,14 @@ static void Main(string[] args)
var logger = new ConsoleLogger();

var fallbackSubtitleProvider = new FallbackSubtitleProvider(
//new OpenSubtitles(languages),
//new OpenSubtitles(languages), // uncomment as soon as its been implemented
new Subscene(languages)
);

var subSyncWorkerProvider = new WorkerProvider(logger, subtitleExtensions, fallbackSubtitleProvider);
var subSyncWorkerQueue = new WorkerQueue(subSyncWorkerProvider);

using (var mediaWatcher = new FileSystemWatcher(logger, subSyncWorkerQueue, input, videoExtensions, subtitleExtensions))
using (var mediaWatcher = new SubtitleSynchronizer(logger, subSyncWorkerQueue, input, videoExtensions, subtitleExtensions))
{

logger.WriteLine("╔═════════════════════════════════════════════════════╗");
Expand All @@ -58,7 +57,7 @@ static void Main(string[] args)
logger.WriteLine("");
logger.WriteLine(" You may press @green@'q' @gray@at any time to quit.");
logger.WriteLine("");
logger.WriteLine(" ----------------------------------------------------------- ");
logger.WriteLine(" ───────────────────────────────────────────────────── ");
logger.WriteLine("");

mediaWatcher.Start();
Expand Down
3 changes: 2 additions & 1 deletion src/SubSync/SubSync.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp2.0</TargetFramework>
<Version>0.1.2</Version>
<Version>0.1.3</Version>
<AssemblyVersion>0.1.3.0</AssemblyVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down

0 comments on commit 7551a99

Please sign in to comment.