Skip to content

Commit 4603f2c

Browse files
committed
update + cleanup
1 parent 3a04d7e commit 4603f2c

17 files changed

+153
-112
lines changed

UnitTest/SubDBClient.Test.cs

+11-12
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace SubDBSharp.Test
88
{
99
public class UnitTest
1010
{
11-
private static readonly IResponseParser _reponseParser = new CsvResponseParser();
11+
private static readonly IResponseParser ReponseParser = new CsvResponseParser();
1212

1313
[Fact]
1414
public async Task TestAvailableLanguages()
@@ -17,9 +17,9 @@ public async Task TestAvailableLanguages()
1717
var response = await subDbClient.GetAvailableLanguagesAsync();
1818
Assert.NotNull(response.Body);
1919

20-
byte[] buffer = (byte[])response.Body;
21-
string body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
22-
var availableLanguages = _reponseParser.ParseGetAvailablesLanguages(body);
20+
var buffer = (byte[])response.Body;
21+
var body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
22+
var availableLanguages = ReponseParser.ParseGetAvailablesLanguages(body);
2323

2424
Assert.True(availableLanguages.Count > 0);
2525
}
@@ -30,28 +30,27 @@ public async Task TestSearchSubtitle()
3030
var subDbClient = new SubDBClient(GetProductInfo(), ApiUrls.SubDBApiSandBoxUrl);
3131
var response = await subDbClient.SearchSubtitleAsync("ffd8d4aa68033dc03d1c8ef373b9028c", false);
3232
Assert.NotNull(response.Body);
33-
34-
byte[] buffer = (byte[])response.Body;
35-
string body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
36-
37-
var availableLanguages = _reponseParser.ParseGetAvailablesLanguages(body);
33+
var buffer = (byte[])response.Body;
34+
Assert.True(buffer.Length > 0);
35+
var body = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
36+
var availableLanguages = ReponseParser.ParseGetAvailablesLanguages(body);
3837
Assert.True(availableLanguages.Count > 0);
3938
}
4039

4140
[Fact]
4241
public async Task TestDownloadSubtitle()
4342
{
4443
var subDbClient = new SubDBClient(GetProductInfo(), ApiUrls.SubDBApiSandBoxUrl);
45-
string movieHash = Utils.GetMovieHash("./Assets/dexter.mp4");
44+
var movieHash = Utils.GetMovieHash("./Assets/dexter.mp4");
4645
var response = await subDbClient.DownloadSubtitleAsync(movieHash, "en");
4746
Assert.True(response.StatusCode == System.Net.HttpStatusCode.OK);
4847
}
4948

5049
[Fact]
5150
public async Task TestUploadSubtitle()
5251
{
53-
string movie = "./Assets/dexter.mp4";
54-
string subtitle = @"./Assets/Logan.2017.en.srt";
52+
var movie = "./Assets/dexter.mp4";
53+
var subtitle = @"./Assets/Logan.2017.en.srt";
5554
var subDbClient = new SubDBClient(GetProductInfo(), ApiUrls.SubDBApiSandBoxUrl);
5655
var response = await subDbClient.UploadSubtitleAsync(subtitle, movie);
5756
Assert.True(response.StatusCode == System.Net.HttpStatusCode.Created);

UnitTest/UnitTest.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp1.1</TargetFramework>
4+
<TargetFramework>netcoreapp1.1</TargetFramework>
5+
<ApplicationIcon />
6+
<OutputType>Library</OutputType>
7+
<StartupObject />
58
</PropertyGroup>
69

710
<ItemGroup>

UnitTest/Utils.Test.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using SubDBSharp;
1+
using SubDBSharp.Helpers;
22
using System.IO;
33
using Xunit;
44

@@ -12,12 +12,12 @@ public void TestGetHashString()
1212
// dexter
1313
const string expected = "ffd8d4aa68033dc03d1c8ef373b9028c";
1414
// http://thesubdb.com/api/samples/dexter.mp4
15-
string path = "./Assets/dexter.mp4";
16-
if(!File.Exists(path))
15+
var path = "./Assets/dexter.mp4";
16+
if (!File.Exists(path))
1717
{
1818
throw new FileNotFoundException("dexter.mp4");
1919
}
20-
string hashString = Utils.GetMovieHash(path);
20+
var hashString = Utils.GetMovieHash(path);
2121
Assert.Equal(expected, hashString);
2222
}
2323
}

src/Helpers/ApiUrls.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22

3-
namespace SubDBSharp
3+
namespace SubDBSharp.Helpers
44
{
55
public static class ApiUrls
66
{

src/Helpers/CsvResponseParser.cs

+4-20
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ public IReadOnlyList<Language> ParseGetAvailablesLanguages(string response)
99
var readonlyList = new List<Language>();
1010
foreach (var isoTwoLetter in response.Split(','))
1111
{
12-
if (string.IsNullOrWhiteSpace(isoTwoLetter))
13-
{
14-
continue;
15-
}
16-
var lang = new Language(isoTwoLetter);
17-
readonlyList.Add(lang);
12+
if (!string.IsNullOrWhiteSpace(isoTwoLetter)) readonlyList.Add(new Language(isoTwoLetter));
1813
}
1914
return readonlyList;
2015
}
@@ -25,20 +20,9 @@ public IReadOnlyList<Language> ParseSearchSubtitle(string response, bool getVers
2520
foreach (var token in response.Split(','))
2621
{
2722
// Tokens are available only if getVersion == true
28-
string[] tokens = token.Split(':');
29-
Language lang = null;
30-
if (getVersions)
31-
{
32-
lang = new Language(tokens[0], int.Parse(tokens[1]));
33-
}
34-
else
35-
{
36-
lang = new Language(tokens[0]);
37-
}
38-
if (lang != null)
39-
{
40-
listLanguages.Add(lang);
41-
}
23+
var tokens = token.Split(':');
24+
var lang = getVersions ? new Language(tokens[0], int.Parse(tokens[1])) : new Language(tokens[0]);
25+
listLanguages.Add(lang);
4226
}
4327
return listLanguages;
4428
}

src/Helpers/UriExtensions.cs

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4+
using System.Net.Http;
45
using System.Text;
56

6-
namespace SubDBSharp
7+
namespace SubDBSharp.Helpers
78
{
89
public static class UriExtensions
910
{
@@ -60,10 +61,10 @@ public static Uri ApplyParameters(this Uri uri, IDictionary<string, string> para
6061

6162
public static Uri ApplySearchSubtitleParameters(this Uri uri, string hash, bool getVersions)
6263
{
63-
string query = $"action=search&hash={hash}";
64+
var query = $"action=search&hash={hash}";
6465
if (getVersions)
6566
{
66-
query += $"&versions";
67+
query += "&versions";
6768
}
6869
var uriBuilder = new UriBuilder(uri)
6970
{
@@ -75,14 +76,15 @@ public static Uri ApplySearchSubtitleParameters(this Uri uri, string hash, bool
7576
public static Uri ApplyDownloadSubtitleParameters(this Uri uri, string hash, params string[] languages)
7677
{
7778
// en,es,fr,it,nl,pl,pt,ro,sv,tr
78-
7979
var sb = new StringBuilder($"action=download&hash={hash}&language=");
80-
string query = sb.ToString() + string.Join(",", languages);
80+
var query = sb + string.Join(",", languages);
8181
var uriBuilder = new UriBuilder(uri)
8282
{
8383
Query = query
8484
};
8585
return uriBuilder.Uri;
8686
}
87+
88+
public static HttpContent GetFormURlEncodedContent(Dictionary<string, string> values) => new FormUrlEncodedContent(values);
8789
}
8890
}

src/Helpers/Utils.cs

+8-19
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,21 @@
33
using System.Security.Cryptography;
44
using System.Text;
55

6-
namespace SubDBSharp
6+
namespace SubDBSharp.Helpers
77
{
88
public static class Utils
99
{
10-
public static string FormatUserAgent(ProductHeaderValue procInfo)
11-
{
12-
return string.Format("{0} ({1} {2})", "SubDB/1.0", procInfo, "http://github.com/ivandrofly");
13-
}
10+
public static string FormatUserAgent(ProductHeaderValue procInfo) => $"{"SubDB/1.0"} ({procInfo} {"http://github.com/ivandrofly"})";
1411

1512
public static string GetMovieHash(string file)
1613
{
1714
int bytesToRead = 64 * 1024;
1815
using (var fs = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
1916
using (var md5 = MD5.Create())
2017
{
21-
2218
// file too short
23-
if (fs.Length < bytesToRead)
24-
{
25-
return string.Empty;
26-
}
19+
if (fs.Length < bytesToRead) return string.Empty;
2720

28-
int len = (int)fs.Length;
2921
byte[] buffer = new byte[bytesToRead * 2];
3022

3123
// Read first 64kb.
@@ -41,19 +33,16 @@ public static string GetMovieHash(string file)
4133

4234
public static string ToHexadecimal(byte[] bytes)
4335
{
44-
var _hexBuilder = new StringBuilder();
45-
for (int i = 0; i < bytes.Length; i++)
46-
{
47-
_hexBuilder.Append(bytes[i].ToString("x2"));
48-
}
49-
return _hexBuilder.ToString();
36+
var sb = new StringBuilder();
37+
for (int i = 0; i < bytes.Length; i++) sb.Append(bytes[i].ToString("x2"));
38+
39+
return sb.ToString();
5040
}
5141

5242
public static bool IsUtf8(byte[] buffer)
5343
{
5444
// call this method to encoding;
55-
5645
return false;
5746
}
5847
}
59-
}
48+
}

src/Http/IRequest.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Net.Http;
3+
4+
namespace SubDBSharp.Models
5+
{
6+
public interface IRequest
7+
{
8+
HttpContent Body { get; }
9+
Uri EndPoint { get; }
10+
HttpMethod Method { get; }
11+
}
12+
}

src/Http/IResponse.cs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections.Generic;
2+
using System.Net;
3+
4+
namespace SubDBSharp.Http
5+
{
6+
public interface IResponse
7+
{
8+
object Body { get; }
9+
IReadOnlyDictionary<string, string> Headers { get; }
10+
HttpStatusCode StatusCode { get; }
11+
}
12+
}

src/Http/ISubDBApi.cs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using SubDBSharp.Http;
2+
using System.Threading.Tasks;
3+
4+
namespace SubDBSharp
5+
{
6+
public interface ISubDBApi
7+
{
8+
Task<Response> DownloadSubtitle(string hash, params string[] languages);
9+
Task<Response> GetAvailableLanguagesAsync();
10+
Task<Response> SearchSubtitle(string hash, bool getVersions = false);
11+
Task<Response> UploadSubtitle(string subtitle, string movie);
12+
}
13+
}

src/Http/Request.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace SubDBSharp.Models
55
{
6-
public class Request
6+
public class Request : IRequest
77
{
88
public Request(Uri endpoint, HttpMethod method, HttpContent content)
99
{

src/Http/Response.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
namespace SubDBSharp.Http
66
{
7-
public class Response
7+
public class Response : IResponse
88
{
99
public Response(HttpStatusCode statusCode, object body, IDictionary<string, string> headers)
1010
{

0 commit comments

Comments
 (0)