-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from zerratar/feature/add-opensubtitles-provider
Feature/add opensubtitles provider
- Loading branch information
Showing
49 changed files
with
2,261 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ x64/ | |
x86/ | ||
bld/ | ||
[Bb]in/ | ||
[Bb]uild/ | ||
[Oo]bj/ | ||
[Ll]og/ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
rem rmdir .\build\self-contained-win10-x86 /S /Q | ||
rem rmdir .\build\self-contained-win10-x64 /S /Q | ||
rmdir .\build\win10-x86 /S /Q | ||
rmdir .\build\win10-x64 /S /Q | ||
rem dotnet publish -o ..\..\build\self-contained-win10-x86 -r win10-x86 --self-contained | ||
rem dotnet publish -o ..\..\build\self-contained-win10-x64 -r win10-x64 --self-contained | ||
dotnet publish -o ..\..\build\win10-x86 -r win10-x86 --self-contained false | ||
dotnet publish -o ..\..\build\win10-x64 -r win10-x64 --self-contained false | ||
pause |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
namespace SubSync | ||
{ | ||
internal struct AuthCredentials | ||
{ | ||
public readonly string Username; | ||
public readonly string Password; | ||
|
||
public AuthCredentials(string username, string password) | ||
{ | ||
Username = username; | ||
Password = password; | ||
} | ||
|
||
public bool IsEmpty => string.IsNullOrEmpty(Username) && string.IsNullOrEmpty(Password); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace SubSync | ||
{ | ||
internal class DownloadQuotaReachedException : Exception | ||
{ | ||
} | ||
} |
12 changes: 12 additions & 0 deletions
12
src/SubSync/Core/Exceptions/NestedArchiveNotSupportedException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
using System; | ||
|
||
namespace SubSync | ||
{ | ||
internal class NestedArchiveNotSupportedException : Exception | ||
{ | ||
public NestedArchiveNotSupportedException(string filename) | ||
: base($"Downloaded archive, '{filename}' @red@contain another archive within it and cannot properly be extracted. Archive kept for manual labor.") | ||
{ | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace SubSync | ||
{ | ||
internal class RequestQuotaReachedException : Exception | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
using System; | ||
|
||
namespace SubSync | ||
{ | ||
internal class SubtitleNotFoundException : Exception | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using System; | ||
|
||
namespace SubSync | ||
{ | ||
internal class FileBasedCredentialsProvider : IAuthCredentialProvider | ||
{ | ||
private readonly string filename; | ||
private readonly ILogger logger; | ||
private bool synchronizedWithFile; | ||
private string username; | ||
private string password; | ||
|
||
public FileBasedCredentialsProvider(string filename, ILogger logger) | ||
{ | ||
this.filename = filename; | ||
this.logger = logger; | ||
} | ||
|
||
private void ReadFile() | ||
{ | ||
if (!System.IO.File.Exists(filename)) | ||
{ | ||
return; | ||
} | ||
// storing the user/pass is generally a very bad idea as you could find the values by looking in the application memory. | ||
// But since I highly doubt anyone is going to go that far to write a virus or app to read the memory of SubSync just to steal someones subtitle provider passwords. lol | ||
try | ||
{ | ||
var lines = System.IO.File.ReadAllLines(filename); | ||
foreach (var line in lines) | ||
{ | ||
var data = line.Split('='); | ||
if (data[0].Equals("username", StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
this.username = data[1]; | ||
} | ||
else if (data[0].Equals("password", StringComparison.CurrentCultureIgnoreCase)) | ||
{ | ||
this.password = data[1]; | ||
} | ||
} | ||
synchronizedWithFile = true; | ||
} | ||
catch (Exception exc) | ||
{ | ||
this.logger.WriteLine("@yel@Unable to read opensubtitles.auth! username and password will be left blank!"); | ||
} | ||
|
||
} | ||
|
||
public AuthCredentials Get() | ||
{ | ||
if (!this.synchronizedWithFile) | ||
{ | ||
// in case it previously failed or file was added after the application started. | ||
this.ReadFile(); | ||
} | ||
|
||
return new AuthCredentials(this.username, this.password); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Net.Sockets; | ||
using System.Text; | ||
using SubSync.Extensions; | ||
|
||
namespace SubSync | ||
{ | ||
public static class FilenameDiff | ||
{ | ||
public static int IndexOfBestMatch(string needle, string[] haystack) | ||
{ | ||
var index = 0; | ||
var scored = new Dictionary<int, double>(); | ||
var input = Path.GetFileNameWithoutExtension(needle); | ||
|
||
foreach (var item in haystack) | ||
{ | ||
if (item.Equals(input, StringComparison.OrdinalIgnoreCase)) | ||
{ | ||
return index; // direct match | ||
} | ||
scored[index++] = FilenameDiff.GetDiffScore(input, item); | ||
} | ||
|
||
// score: the lower, the better. | ||
if (scored.Count > 0) | ||
{ | ||
return scored.OrderBy(x => x.Value).First().Key; | ||
} | ||
|
||
return -1; | ||
} | ||
|
||
public static T FindBestMatch<T>(string needle, T[] haystack, Func<T, string> stringComparison) | ||
{ | ||
var index = IndexOfBestMatch(needle, haystack.Select(stringComparison).ToArray()); | ||
return index != -1 ? haystack[index] : default(T); | ||
} | ||
|
||
public static double GetDiffScore(string a, string b) | ||
{ | ||
// first iteration is to take all distinct values from a and b | ||
// then compare the actual content and score it. | ||
// second iteration takes words into consideration | ||
var c1 = a.ToLower().ToCharArray(); | ||
var c2 = b.ToLower().ToCharArray(); | ||
|
||
var diff = new HashSet<char>(c1); | ||
diff.SymmetricExceptWith(c2); | ||
|
||
var changes = diff.ToArray(); // c1.Intersect(c2).ToArray(); | ||
var score = 0.0; | ||
// different chars have different scoring | ||
// letters are 1.0 | ||
// numbers are 0.75 | ||
// brackets and paranthesis are 0.5 | ||
// spaces are 0.1 | ||
foreach (var change in changes) | ||
{ | ||
if (change == '[' || change == ']' || change == '(' || change == ')') score += 0.5; | ||
else if (char.IsDigit(change)) score += 0.75; | ||
else if (change == ' ') score += 0.1; | ||
else score += 1.0; | ||
} | ||
|
||
var l0 = new HashSet<string>(); | ||
a.ToLower().Split(new[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ForEach(x => l0.Add(x)); | ||
if (l0.Count > 1) l0.Remove(a.ToLower()); | ||
|
||
var l1 = new HashSet<string>(); | ||
b.ToLower().Split(new[] { '.', ' ' }, StringSplitOptions.RemoveEmptyEntries).ForEach(x => l1.Add(x)); | ||
if (l1.Count > 1) l1.Remove(b.ToLower()); | ||
|
||
var l2 = new HashSet<string>(); | ||
l2.UnionWith(l0); | ||
l2.UnionWith(l1); | ||
|
||
for (var i = 0; i < l2.Count; i++) | ||
{ | ||
if (!l0.Contains(l2.ElementAt(i))) score++; | ||
if (!l1.Contains(l2.ElementAt(i))) score++; | ||
} | ||
|
||
return score; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
namespace SubSync | ||
{ | ||
internal interface IAuthCredentialProvider | ||
{ | ||
AuthCredentials Get(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.