Skip to content

Commit 8dac2e4

Browse files
committed
Add project to github.
1 parent 9cf4ad0 commit 8dac2e4

File tree

10 files changed

+489
-0
lines changed

10 files changed

+489
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SubDBSharp.Test")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SubDBSharp.Test")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("d67e841d-abee-4057-bfe3-bc57a88b8cd0")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

SubDBSharp.Test/SubDBClient.Test.cs

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.IO;
4+
5+
namespace SubDBSharp.Test
6+
{
7+
[TestClass]
8+
public class UnitTest1
9+
{
10+
static readonly Uri _sandBoxEndPoint = new Uri("http://sandbox.thesubdb.com/");
11+
12+
[TestMethod]
13+
public void TestAvailableLanguages()
14+
{
15+
var client = new Client("SubDBSharp", "1.0", "https://github.com/ivandrofly/SubDBSharp");
16+
var sClient = new SubDBClient(client);
17+
string languagues = sClient.LanguagesAvailable();
18+
Assert.IsTrue(languagues.Length > 0);
19+
}
20+
21+
[TestMethod]
22+
public void TestSearchSubtitle()
23+
{
24+
var client = new Client("SubDBSharp", "1.0", "https://github.com/ivandrofly/SubDBSharp");
25+
var sClient = new SubDBClient(client);
26+
string languagues = sClient.SearchSubtitle("ffd8d4aa68033dc03d1c8ef373b9028c");
27+
Assert.IsTrue(languagues.Length > 0);
28+
}
29+
30+
[TestMethod]
31+
public void TestDownloadSubtitle()
32+
{
33+
var client = new Client("SubDBSharp", "1.0", "https://github.com/ivandrofly/SubDBSharp");
34+
var sClient = new SubDBClient(client);
35+
Stream ms = sClient.DownloadSubtitle("ffd8d4aa68033dc03d1c8ef373b9028c", "en");
36+
using (var fs = new FileStream("Subtitle-Downloaded.srt", FileMode.Create, FileAccess.Write))
37+
{
38+
var buffer = new byte[1024];
39+
int bytesRead = 0;
40+
while ((bytesRead = ms.Read(buffer, 0, 1024)) > 0)
41+
{
42+
fs.Write(buffer, 0, bytesRead);
43+
}
44+
}
45+
Assert.IsTrue(File.Exists("Subtitle-Downloaded.srt"));
46+
}
47+
48+
[TestMethod]
49+
public void TestUploadSubtitle()
50+
{
51+
string movieFile = "";
52+
string subfile = "";
53+
string hash = Utils.GetHashString(movieFile);
54+
var client = new Client("SubDBSharp", "1.0", "https://github.com/ivandrofly/SubDBSharp");
55+
var sClient = new SubDBClient(_sandBoxEndPoint, client);
56+
string ouput = sClient.UploadSubtitle(movieFile, subfile);
57+
}
58+
59+
}
60+
}
+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
5+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
6+
<ProjectGuid>{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}</ProjectGuid>
7+
<OutputType>Library</OutputType>
8+
<AppDesignerFolder>Properties</AppDesignerFolder>
9+
<RootNamespace>SubDBSharp.Test</RootNamespace>
10+
<AssemblyName>SubDBSharp.Test</AssemblyName>
11+
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<ProjectTypeGuids>{3AC096D0-A1C2-E12C-1390-A8335801FDAB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
14+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">10.0</VisualStudioVersion>
15+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
16+
<ReferencePath>$(ProgramFiles)\Common Files\microsoft shared\VSTT\$(VisualStudioVersion)\UITestExtensionPackages</ReferencePath>
17+
<IsCodedUITest>False</IsCodedUITest>
18+
<TestProjectType>UnitTest</TestProjectType>
19+
</PropertyGroup>
20+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
21+
<DebugSymbols>true</DebugSymbols>
22+
<DebugType>full</DebugType>
23+
<Optimize>false</Optimize>
24+
<OutputPath>bin\Debug\</OutputPath>
25+
<DefineConstants>DEBUG;TRACE</DefineConstants>
26+
<ErrorReport>prompt</ErrorReport>
27+
<WarningLevel>4</WarningLevel>
28+
</PropertyGroup>
29+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
30+
<DebugType>pdbonly</DebugType>
31+
<Optimize>true</Optimize>
32+
<OutputPath>bin\Release\</OutputPath>
33+
<DefineConstants>TRACE</DefineConstants>
34+
<ErrorReport>prompt</ErrorReport>
35+
<WarningLevel>4</WarningLevel>
36+
</PropertyGroup>
37+
<ItemGroup>
38+
<Reference Include="System" />
39+
</ItemGroup>
40+
<Choose>
41+
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
42+
<ItemGroup>
43+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework, Version=10.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL" />
44+
</ItemGroup>
45+
</When>
46+
<Otherwise>
47+
<ItemGroup>
48+
<Reference Include="Microsoft.VisualStudio.QualityTools.UnitTestFramework">
49+
<Private>False</Private>
50+
</Reference>
51+
</ItemGroup>
52+
</Otherwise>
53+
</Choose>
54+
<ItemGroup>
55+
<Compile Include="SubDBClient.Test.cs" />
56+
<Compile Include="Utils.Test.cs" />
57+
<Compile Include="Properties\AssemblyInfo.cs" />
58+
</ItemGroup>
59+
<ItemGroup>
60+
<ProjectReference Include="..\SubDBSharp\SubDBSharp.csproj">
61+
<Project>{e2da0387-3725-49a2-a0b6-57e9869db524}</Project>
62+
<Name>SubDBSharp</Name>
63+
</ProjectReference>
64+
</ItemGroup>
65+
<Choose>
66+
<When Condition="'$(VisualStudioVersion)' == '10.0' And '$(IsCodedUITest)' == 'True'">
67+
<ItemGroup>
68+
<Reference Include="Microsoft.VisualStudio.QualityTools.CodedUITestFramework, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
69+
<Private>False</Private>
70+
</Reference>
71+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Common, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
72+
<Private>False</Private>
73+
</Reference>
74+
<Reference Include="Microsoft.VisualStudio.TestTools.UITest.Extension, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
75+
<Private>False</Private>
76+
</Reference>
77+
<Reference Include="Microsoft.VisualStudio.TestTools.UITesting, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
78+
<Private>False</Private>
79+
</Reference>
80+
</ItemGroup>
81+
</When>
82+
</Choose>
83+
<Import Project="$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets" Condition="Exists('$(VSToolsPath)\TeamTest\Microsoft.TestTools.targets')" />
84+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
85+
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
86+
Other similar extension points exist, see Microsoft.Common.targets.
87+
<Target Name="BeforeBuild">
88+
</Target>
89+
<Target Name="AfterBuild">
90+
</Target>
91+
-->
92+
</Project>

SubDBSharp.Test/Utils.Test.cs

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
using System;
2+
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
4+
namespace SubDBSharp.Test
5+
{
6+
[TestClass]
7+
public class UtilsTest
8+
{
9+
[TestMethod]
10+
public void TestGetHashString()
11+
{
12+
// dexter
13+
const string expected = "ffd8d4aa68033dc03d1c8ef373b9028c";
14+
// http://thesubdb.com/api/samples/dexter.mp4
15+
string path = @"dexter.mp4";
16+
string hashString = Utils.GetHashString(path);
17+
Assert.AreEqual(expected, hashString);
18+
}
19+
}
20+
}

SubDBSharp.sln

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio 14
4+
VisualStudioVersion = 14.0.25420.1
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubDBSharp", "SubDBSharp\SubDBSharp.csproj", "{E2DA0387-3725-49A2-A0B6-57E9869DB524}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SubDBSharp.Test", "SubDBSharp.Test\SubDBSharp.Test.csproj", "{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{E2DA0387-3725-49A2-A0B6-57E9869DB524}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{E2DA0387-3725-49A2-A0B6-57E9869DB524}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{E2DA0387-3725-49A2-A0B6-57E9869DB524}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{E2DA0387-3725-49A2-A0B6-57E9869DB524}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D67E841D-ABEE-4057-BFE3-BC57A88B8CD0}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
EndGlobal

SubDBSharp/Client.cs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace SubDBSharp
8+
{
9+
public class Client
10+
{
11+
public string Name { get; private set; }
12+
public string Version { get; private set; }
13+
public Uri Url { get; private set; }
14+
15+
public Client(string name, string version, Uri url)
16+
{
17+
Name = name;
18+
Version = version;
19+
Url = url;
20+
}
21+
22+
public Client(string name, string version, string url) :
23+
this(name, version, new Uri(url))
24+
{
25+
}
26+
27+
}
28+
}

SubDBSharp/Properties/AssemblyInfo.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("SubDBSharp")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("SubDBSharp")]
13+
[assembly: AssemblyCopyright("Copyright © 2016")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("e2da0387-3725-49a2-a0b6-57e9869db524")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

SubDBSharp/SubDBClient.cs

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
using System;
2+
using System.IO;
3+
using System.Net;
4+
using System.Text;
5+
6+
namespace SubDBSharp
7+
{
8+
public class SubDBClient
9+
{
10+
private const string ProtocolName = "SubDB";
11+
private const string PrtocolVersion = "1.0";
12+
private readonly Uri _baseAddress;
13+
private readonly string _userAgent;
14+
15+
public SubDBClient(Client client)
16+
: this(new Uri("http://api.thesubdb.com/"), client)
17+
{
18+
}
19+
20+
public SubDBClient(Uri _endPoint, Client client)
21+
{
22+
_baseAddress = _endPoint;
23+
_userAgent = string.Format("{0}/{1} ({2}/{3}; {4})",
24+
ProtocolName, PrtocolVersion, client.Name, client.Version, client.Url);
25+
}
26+
27+
28+
public string LanguagesAvailable()
29+
{
30+
string action = "?action=languages";
31+
return ReadResponseStream(RunRequestGet(action, WebRequestMethods.Http.Get).GetResponse());
32+
}
33+
34+
public string SearchSubtitle(string hash, string version = "")
35+
{
36+
string action = $"?action=search&hash={hash}";
37+
if (version.Length > 0)
38+
action += $"&version={version}";
39+
return ReadResponseStream(RunRequestGet(action, WebRequestMethods.Http.Get).GetResponse());
40+
}
41+
42+
public Stream DownloadSubtitle(string hash, string language)
43+
{
44+
string action = $"?action=download&hash={hash}&language={language}";
45+
var ms = new MemoryStream();
46+
Stream r = RunRequestGet(action, WebRequestMethods.Http.Get).GetResponse().GetResponseStream();
47+
int buffSize = 1024;
48+
byte[] buffer = new byte[buffSize];
49+
int bytesRead = 0;
50+
int totalReadBytes = 0;
51+
while ((bytesRead = r.Read(buffer, 0, buffSize)) > 0)
52+
{
53+
ms.Write(buffer, 0, bytesRead);
54+
totalReadBytes += bytesRead;
55+
}
56+
var buff = ms.ToArray();
57+
Array.Clear(buff, totalReadBytes, (int)ms.Length - totalReadBytes);
58+
return new MemoryStream(buff);
59+
}
60+
61+
public string UploadSubtitle(string movieFile, string subfile)
62+
{
63+
string movieHash = Utils.GetHashString(movieFile);
64+
string subText = File.ReadAllText(subfile, Encoding.UTF8);
65+
byte[] textBytes = Encoding.UTF8.GetBytes(subText);
66+
string action = $"?action=upload&hash={movieHash}";
67+
HttpWebRequest request = RunRequestGet(action, WebRequestMethods.Http.Post);
68+
request.ContentLength = textBytes.Length;
69+
Stream rs = request.GetRequestStream();
70+
rs.Write(textBytes, 0, textBytes.Length);
71+
return ReadResponseStream(request.GetResponse());
72+
}
73+
74+
public HttpWebRequest RunRequestGet(string action, string method)
75+
{
76+
var request = (HttpWebRequest)WebRequest.Create(_baseAddress + action);
77+
request.UserAgent = _userAgent;
78+
request.Method = method;
79+
return request;
80+
}
81+
82+
private static string ReadResponseStream(WebResponse reponseStream)
83+
{
84+
using (var sr = new StreamReader(reponseStream.GetResponseStream()))
85+
{
86+
return sr.ReadToEnd();
87+
}
88+
}
89+
}
90+
}

0 commit comments

Comments
 (0)