Example #1
0
        public async Task <SubtitleResponse> GetSubtitles(string id, CancellationToken cancellationToken, int depth = 0)
        {
            var idParts  = LegendasTVIdParts.parse(id);
            var savePath = $"{_appPaths.TempDirectory}{_fileSystem.DirectorySeparatorChar}{Name}_{idParts.downloadId}";

            var requestOptions = new HttpRequestOptions()
            {
                Url = string.Format(URL_BASE + "/downloadarquivo/" + idParts.downloadId),
                CancellationToken = cancellationToken,
            };

            try
            {
                using (var stream = await _httpClient.Get(requestOptions))
                {
                    _logger.Info("Extracting file to " + savePath);
                    _zipClient.ExtractAll(stream, savePath, true);
                }
            }
            catch (System.Exception)
            {
                if (depth < 1 && await Login())
                {
                    return(await GetSubtitles(id, cancellationToken, depth + 1));
                }
                else
                {
                    throw;
                }
            }

            var media         = _libraryManager.GetItemById(idParts.idMedia);
            var subtitleFiles = _fileSystem.GetFiles(savePath, new string[] { ".srt", ".ass" }, false, true);
            var bestCandidate = FindBestCandidate(media.FileNameWithoutExtension, subtitleFiles);

            _logger.Info("Best subtitle found: " + bestCandidate);

            var ms = new MemoryStream();
            await _fileSystem.GetFileStream(bestCandidate, FileOpenMode.Open, FileAccessMode.Read, FileShareMode.Read).CopyToAsync(ms);

            ms.Position = 0;

            return(new SubtitleResponse()
            {
                Format = "srt",
                IsForced = false,
                Stream = ms,
                Language = idParts.language
            });
        }
Example #2
0
        public async Task <IEnumerable <RemoteSubtitleInfo> > Search(
            Guid idMedia,
            CancellationToken cancellationToken,
            CultureDto lang = null,
            string query    = "-",
            string page     = "-",
            string itemId   = "-"
            )
        {
            if (lang == null)
            {
                _logger.Error("No language defined.");
                return(Array.Empty <RemoteSubtitleInfo>());
            }

            var requestOptions = new HttpRequestOptions()
            {
                Url = string.Format(URL_BASE + "/legenda/busca/{0}/{1}/-/{2}/{3}", HttpUtility.HtmlEncode(query), GetLanguageId(lang), page, itemId),
                CancellationToken = cancellationToken,
                Referer           = URL_BASE + "/busca/" + query
            };

            requestOptions.RequestHeaders.Add("X-Requested-With", "XMLHttpRequest");

            using (var stream = await _httpClient.Get(requestOptions))
            {
                using (var reader = new StreamReader(stream))
                {
                    var response = reader.ReadToEnd();

                    return(ParseHtml(idMedia, response, lang)
                           .OrderBy(sub => LegendasTVIdParts.parse(sub.Id).sortingOverride)
                           .ThenByDescending(sub => sub.CommunityRating)
                           .ThenByDescending(sub => sub.DownloadCount));
                }
            }
        }