Skip to content

Commit

Permalink
check in open subtitles stub
Browse files Browse the repository at this point in the history
  • Loading branch information
LukePulverenti committed May 5, 2014
1 parent 4b27706 commit a291b20
Show file tree
Hide file tree
Showing 74 changed files with 6,372 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,11 @@ public async Task<HttpResponseInfo> SendAsync(HttpRequestOptions options, string

var httpWebRequest = GetRequest(options, httpMethod, options.EnableHttpCompression);

if (!string.IsNullOrEmpty(options.RequestContent) || string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase))
if (options.RequestContentBytes != null ||
!string.IsNullOrEmpty(options.RequestContent) ||
string.Equals(httpMethod, "post", StringComparison.OrdinalIgnoreCase))
{
var content = options.RequestContent ?? string.Empty;
var bytes = Encoding.UTF8.GetBytes(content);
var bytes = options.RequestContentBytes ?? Encoding.UTF8.GetBytes(options.RequestContent ?? string.Empty);

httpWebRequest.ContentType = options.RequestContentType ?? "application/x-www-form-urlencoded";
httpWebRequest.ContentLength = bytes.Length;
Expand Down
1 change: 1 addition & 0 deletions MediaBrowser.Common/Net/HttpRequestOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public string UserAgent
public string RequestContentType { get; set; }

public string RequestContent { get; set; }
public byte[] RequestContentBytes { get; set; }

public bool BufferContent { get; set; }

Expand Down
1 change: 1 addition & 0 deletions MediaBrowser.Controller/MediaBrowser.Controller.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
<Compile Include="Providers\IMetadataProvider.cs" />
<Compile Include="Providers\IMetadataService.cs" />
<Compile Include="Providers\IRemoteMetadataProvider.cs" />
<Compile Include="Providers\ISubtitleProvider.cs" />
<Compile Include="Providers\ItemLookupInfo.cs" />
<Compile Include="Providers\MetadataRefreshOptions.cs" />
<Compile Include="Providers\NameParser.cs" />
Expand Down
55 changes: 55 additions & 0 deletions MediaBrowser.Controller/Providers/ISubtitleProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Controller.Providers
{
public interface ISubtitleProvider
{
/// <summary>
/// Gets the name.
/// </summary>
/// <value>The name.</value>
string Name { get; }

/// <summary>
/// Gets the supported media types.
/// </summary>
/// <value>The supported media types.</value>
IEnumerable<SubtitleMediaType> SupportedMediaTypes { get; }

/// <summary>
/// Gets the subtitles.
/// </summary>
/// <param name="request">The request.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task{SubtitleResponse}.</returns>
Task<SubtitleResponse> GetSubtitles(SubtitleRequest request, CancellationToken cancellationToken);
}

public enum SubtitleMediaType
{
Episode = 0,
Movie = 1
}

public class SubtitleResponse
{
public string Format { get; set; }
public bool HasContent { get; set; }
public Stream Stream { get; set; }
}

public class SubtitleRequest
{
public string Language { get; set; }

public SubtitleMediaType ContentType { get; set; }

public string MediaPath { get; set; }
public string SeriesName { get; set; }
public int? IndexNumber { get; set; }
public int? ParentIndexNumber { get; set; }
}
}
23 changes: 14 additions & 9 deletions MediaBrowser.Model/MediaInfo/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@ namespace MediaBrowser.Model.MediaInfo
{
public class Container
{
public string MP4 = "MP4";
public const string MP4 = "MP4";
}

public class AudioCodec
{
public string AAC = "AAC";
public string MP3 = "MP3";
public const string AAC = "AAC";
public const string MP3 = "MP3";
}

public class VideoCodec
{
public string H263 = "H263";
public string H264 = "H264";
public string H265 = "H265";
public string MPEG4 = "MPEG4";
public string MSMPEG4 = "MSMPEG4";
public string VC1 = "VC1";
public const string H263 = "H263";
public const string H264 = "H264";
public const string H265 = "H265";
public const string MPEG4 = "MPEG4";
public const string MSMPEG4 = "MSMPEG4";
public const string VC1 = "VC1";
}

public class SubtitleFormat
{
public const string SRT = "SRT";
}
}
5 changes: 5 additions & 0 deletions MediaBrowser.Providers/MediaBrowser.Providers.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@
<Compile Include="Savers\XmlSaverHelpers.cs" />
<Compile Include="Studios\StudiosImageProvider.cs" />
<Compile Include="Studios\StudioMetadataService.cs" />
<Compile Include="Subtitles\OpenSubtitleDownloader.cs" />
<Compile Include="TV\EpisodeLocalImageProvider.cs" />
<Compile Include="TV\EpisodeMetadataService.cs" />
<Compile Include="TV\EpisodeXmlProvider.cs" />
Expand Down Expand Up @@ -229,6 +230,10 @@
<Project>{7EEEB4BB-F3E8-48FC-B4C5-70F0FFF8329B}</Project>
<Name>MediaBrowser.Model</Name>
</ProjectReference>
<ProjectReference Include="..\OpenSubtitlesHandler\OpenSubtitlesHandler.csproj">
<Project>{4a4402d4-e910-443b-b8fc-2c18286a2ca0}</Project>
<Name>OpenSubtitlesHandler</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
Expand Down
132 changes: 132 additions & 0 deletions MediaBrowser.Providers/Subtitles/OpenSubtitleDownloader.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
using MediaBrowser.Common.Net;
using MediaBrowser.Controller.Providers;
using MediaBrowser.Model.Logging;
using MediaBrowser.Model.MediaInfo;
using OpenSubtitlesHandler;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MediaBrowser.Providers.Subtitles
{
public class OpenSubtitleDownloader : ISubtitleProvider
{
private readonly ILogger _logger;
private readonly IHttpClient _httpClient;
private readonly CultureInfo _usCulture = new CultureInfo("en-US");

public OpenSubtitleDownloader(ILogger logger, IHttpClient httpClient)
{
_logger = logger;
_httpClient = httpClient;
}

public string Name
{
get { return "Open Subtitles"; }
}

public IEnumerable<SubtitleMediaType> SupportedMediaTypes
{
get { return new[] { SubtitleMediaType.Episode, SubtitleMediaType.Movie }; }
}

public Task<SubtitleResponse> GetSubtitles(SubtitleRequest request, CancellationToken cancellationToken)
{
return request.ContentType == SubtitleMediaType.Episode
? GetEpisodeSubtitles(request, cancellationToken)
: GetMovieSubtitles(request, cancellationToken);
}

public async Task<SubtitleResponse> GetMovieSubtitles(SubtitleRequest request, CancellationToken cancellationToken)
{
throw new NotImplementedException();
}

public async Task<SubtitleResponse> GetEpisodeSubtitles(SubtitleRequest request, CancellationToken cancellationToken)
{
var response = new SubtitleResponse();

if (!request.IndexNumber.HasValue || !request.ParentIndexNumber.HasValue)
{
_logger.Debug("Information Missing");
return response;
}
if (string.IsNullOrEmpty(request.MediaPath))
{
_logger.Debug("Path Missing");
return response;
}

Utilities.HttpClient = _httpClient;
OpenSubtitles.SetUserAgent("OS Test User Agent");
var loginResponse = OpenSubtitles.LogIn("", "", "en");
if (!(loginResponse is MethodResponseLogIn))
{
_logger.Debug("Login error");
return response;
}

var subLanguageId = request.Language;
var hash = Utilities.ComputeHash(request.MediaPath);
var fileInfo = new FileInfo(request.MediaPath);
var movieByteSize = fileInfo.Length;

var parms = new List<SubtitleSearchParameters> {
new SubtitleSearchParameters(subLanguageId, hash, movieByteSize),
new SubtitleSearchParameters(subLanguageId, request.SeriesName, request.ParentIndexNumber.Value.ToString(_usCulture), request.IndexNumber.Value.ToString(_usCulture)),

};

var result = OpenSubtitles.SearchSubtitles(parms.ToArray());
if (!(result is MethodResponseSubtitleSearch))
{
_logger.Debug("invalid response type");
return null;
}

var results = ((MethodResponseSubtitleSearch)result).Results;
var bestResult = results.Where(x => x.SubBad == "0" && int.Parse(x.SeriesSeason) == request.ParentIndexNumber && int.Parse(x.SeriesEpisode) == request.IndexNumber)
.OrderBy(x => x.MovieHash == hash)
.ThenBy(x => Math.Abs(long.Parse(x.MovieByteSize) - movieByteSize))
.ThenByDescending(x => int.Parse(x.SubDownloadsCnt))
.ThenByDescending(x => double.Parse(x.SubRating))
.ToList();

if (!bestResult.Any())
{
_logger.Debug("No Subtitles");
return response;
}

_logger.Debug("Found " + bestResult.Count + " subtitles.");

var subtitle = bestResult.First();
var downloadsList = new[] { int.Parse(subtitle.IDSubtitleFile) };

var resultDownLoad = OpenSubtitles.DownloadSubtitles(downloadsList);
if (!(resultDownLoad is MethodResponseSubtitleDownload))
{
_logger.Debug("invalid response type");
return response;
}
if (!((MethodResponseSubtitleDownload)resultDownLoad).Results.Any())
{
_logger.Debug("No Subtitle Downloads");
return response;
}

var res = ((MethodResponseSubtitleDownload)resultDownLoad).Results.First();
var data = Convert.FromBase64String(res.Data);

response.HasContent = true;
response.Format = SubtitleFormat.SRT;
response.Stream = new MemoryStream(Utilities.Decompress(new MemoryStream(data)));
return response;
}
}
}
6 changes: 1 addition & 5 deletions MediaBrowser.ServerApplication/ApplicationHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,16 +1026,12 @@ public bool HasUpdateAvailable
/// <returns>Task{CheckForUpdateResult}.</returns>
public override async Task<CheckForUpdateResult> CheckForApplicationUpdate(CancellationToken cancellationToken, IProgress<double> progress)
{
#if DEBUG
return new CheckForUpdateResult { AvailableVersion = ApplicationVersion, IsUpdateAvailable = false };
#endif

var availablePackages = await InstallationManager.GetAvailablePackagesWithoutRegistrationInfo(cancellationToken).ConfigureAwait(false);

var version = InstallationManager.GetLatestCompatibleVersion(availablePackages, Constants.MbServerPkgName, null, ApplicationVersion,
ConfigurationManager.CommonConfiguration.SystemUpdateLevel);

HasUpdateAvailable = version != null;
HasUpdateAvailable = version != null && version.version >= ApplicationVersion;

return version != null ? new CheckForUpdateResult { AvailableVersion = version.version, IsUpdateAvailable = version.version > ApplicationVersion, Package = version } :
new CheckForUpdateResult { AvailableVersion = ApplicationVersion, IsUpdateAvailable = false };
Expand Down
1 change: 1 addition & 0 deletions MediaBrowser.WebDashboard/dashboard-ui/css/librarymenu.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
width: 200px;
min-height: 100px;
border-bottom-right-radius: 5px;
padding-bottom: 10px;
}

.itemDetailPage .desktopLibraryMenu {
Expand Down
40 changes: 24 additions & 16 deletions MediaBrowser.WebDashboard/dashboard-ui/metadataadvanced.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,31 @@
</div>
</li>
</ul>
<h2>${HeaderAutomaticUpdates}</h2>

<div data-role="collapsible">
<h2>${HeaderAutomaticUpdates}</h2>
<div>
<ul data-role="listview" class="ulForm">
<li>
<input type="checkbox" id="chkEnableFanartUpdates" name="chkEnableFanartUpdates" data-mini="true" />
<label for="chkEnableFanartUpdates">${LabelAutomaticUpdatesFanart}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesFanartHelp}</div>
</li>
<li>
<input type="checkbox" id="chkEnableTmdbPersonUpdates" name="chkEnableTmdbPersonUpdates" data-mini="true" />
<label for="chkEnableTmdbPersonUpdates">${LabelAutomaticUpdatesTmdb}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesTmdbHelp}</div>
</li>
<li>
<input type="checkbox" id="chkEnableTvdbUpdates" name="chkEnableTvdbUpdates" data-mini="true" />
<label for="chkEnableTvdbUpdates">${LabelAutomaticUpdatesTvdb}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesTvdbHelp}</div>
</li>
</ul>
</div>
</div>
<br />
<ul data-role="listview" class="ulForm">
<li>
<input type="checkbox" id="chkEnableFanartUpdates" name="chkEnableFanartUpdates" data-mini="true" />
<label for="chkEnableFanartUpdates">${LabelAutomaticUpdatesFanart}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesFanartHelp}</div>
</li>
<li>
<input type="checkbox" id="chkEnableTmdbPersonUpdates" name="chkEnableTmdbPersonUpdates" data-mini="true" />
<label for="chkEnableTmdbPersonUpdates">${LabelAutomaticUpdatesTmdb}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesTmdbHelp}</div>
</li>
<li>
<input type="checkbox" id="chkEnableTvdbUpdates" name="chkEnableTvdbUpdates" data-mini="true" />
<label for="chkEnableTvdbUpdates">${LabelAutomaticUpdatesTvdb}</label>
<div class="fieldDescription">${LabelAutomaticUpdatesTvdbHelp}</div>
</li>
<li>
<button type="submit" data-theme="b" data-icon="check" data-mini="true">
${ButtonSave}
Expand Down
16 changes: 16 additions & 0 deletions MediaBrowser.sln
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.Dlna", "MediaB
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MediaBrowser.MediaEncoding", "MediaBrowser.MediaEncoding\MediaBrowser.MediaEncoding.csproj", "{0BD82FA6-EB8A-4452-8AF5-74F9C3849451}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenSubtitlesHandler", "OpenSubtitlesHandler\OpenSubtitlesHandler.csproj", "{4A4402D4-E910-443B-B8FC-2C18286A2CA0}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -263,6 +265,20 @@ Global
{0BD82FA6-EB8A-4452-8AF5-74F9C3849451}.Release|Win32.ActiveCfg = Release|Any CPU
{0BD82FA6-EB8A-4452-8AF5-74F9C3849451}.Release|x64.ActiveCfg = Release|Any CPU
{0BD82FA6-EB8A-4452-8AF5-74F9C3849451}.Release|x86.ActiveCfg = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|Win32.ActiveCfg = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|x64.ActiveCfg = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Debug|x86.ActiveCfg = Debug|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|Any CPU.Build.0 = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|Win32.ActiveCfg = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|x64.ActiveCfg = Release|Any CPU
{4A4402D4-E910-443B-B8FC-2C18286A2CA0}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
Loading

0 comments on commit a291b20

Please sign in to comment.