Пример #1
0
        public async Task<Response> Download(ISession session, Episode episode, string template, string downloadpath, Quality quality, Format formats, CancellationToken token, IProgress<DownloadInfo> progress)
        {
            try
            {
                KeyValuePair<string, string> defl = AudioLanguageFromEpisode(episode);
                CrunchySession sess = session as CrunchySession;

                if (sess == null)
                    return new Response { ErrorMessage = "Invalid Session", Status = ResponseStatus.InvalidArgument };
                if (!episode.PluginMetadata.ContainsKey("Url"))
                    return new Response { ErrorMessage = "Invalid Episode", Status = ResponseStatus.InvalidArgument };
                DownloadInfo dp = new DownloadInfo { FileName = TemplateParser.FilenameFromEpisode(episode, quality, template), Format = formats, Percent = 0, Quality = quality };

                token.ThrowIfCancellationRequested();
                dp.Languages = new List<string>();
                dp.Percent = 1;
                dp.Status = "Getting Metadata";
                progress.Report(dp);
                Config c = await GetStandardConfig(sess, episode.Id, quality);
                if (c == null)
                    return new Response { ErrorMessage = "Unable to retrieve metadata", Status = ResponseStatus.WebError };
                if (!string.IsNullOrEmpty(c.Stream_info.Error))
                    return new Response { ErrorMessage = "Login Required", Status = ResponseStatus.LoginRequired };
                List<Task<CrunchySubtitleInfo>> subTasks = new List<Task<CrunchySubtitleInfo>>();
                token.ThrowIfCancellationRequested();
                if ((c.Subtitles != null && c.Subtitles.Subtitle != null && c.Subtitles.Subtitle.Count > 0))
                {
                    foreach (Subtitle s in c.Subtitles.Subtitle)
                    {
                        subTasks.Add(GetSubtitle(sess, int.Parse(s.Id)));
                    }
                    dp.Percent = 2;
                    dp.Status = "Gettings subtitles";
                    progress.Report(dp);
                    await Task.WhenAll(subTasks);
                    foreach (CrunchySubtitleInfo s in subTasks.Select(a => a.Result))
                        dp.Languages.Add(s.Title);
                }
                else
                {
                    dp.Languages.Add("Hardcoded");
                }
                dp.Quality = c.Stream_info.Metadata.Height.ToQuality();
                dp.FileName = TemplateParser.FilenameFromEpisode(episode, dp.Quality, template);
                dp.FullPath = Path.Combine(downloadpath, dp.FileName);
                string intermediatefile = Path.Combine(downloadpath, dp.FileName + ".tm1");
                KeyValuePair<string, string> hh = ParseHost(c.Stream_info.Host);
                string args = string.Format(LibSet[RTMPDumpArgsS], hh.Key, hh.Value, c.Stream_info.File, intermediatefile);
                List<string> todeleteFiles = new List<string>();
                todeleteFiles.Add(intermediatefile);
                dp.Percent = 3;
                dp.Status = "Downloading video";
                progress.Report(dp);
                RTMPDumpParser rtmp = new RTMPDumpParser();
                double dbl = 92;
                double final = 0;
                rtmp.OnProgress += (val) =>
                {
                    final = val;
                    dp.Percent = (val * dbl / 100) + 3;
                    progress.Report(dp);
                };
                await rtmp.Start(LibSet[RTMPDumpEXES], args, token);
                if (final < 100)
                    return new Response { ErrorMessage = "Error downloading video", Status = ResponseStatus.TransferError };
                List<CrunchySubtitleInfo> sis = subTasks.Select(a => a.Result).ToList();
                string inputs = string.Empty;
                string maps = String.Empty;
                int pp = 0;
                foreach (CrunchySubtitleInfo k in sis)
                {
                    string pth = Path.GetTempFileName() + ".ass";
                    todeleteFiles.Add(pth);
                    File.WriteAllText(pth, k.Ass);
                    inputs += "-i \"" + pth + "\" ";
                    maps += GetFFMPEGSubtitleArguments(pp + 1, pp, k.Language, k.Title);
                    pp++;
                }
                dp.Size = await ReMux(intermediatefile, inputs, maps, formats, defl.Key, defl.Value, 96, 4, dp, progress, token);
                dp.Percent = 100;
                dp.Status = "Finished";
                progress.Report(dp);
                foreach (string s in todeleteFiles)
                {
                    try
                    {
                        File.Delete(s);

                    }
                    catch (Exception)
                    {
                        // ignored
                    }
                }
                return new Response { Status = ResponseStatus.Ok, ErrorMessage = "OK" };
            }
            catch (Exception e)
            {
                if (e is OperationCanceledException)
                    return new Response {ErrorMessage = "Canceled", Status = ResponseStatus.Canceled};
                return new Response {ErrorMessage = e.ToString(), Status = ResponseStatus.SystemError};
            }
            
        }