示例#1
0
        public static string DownloadSubtitle(string fileName, string[] preferredLanguages, string[] preferredServices, out IMDb imdbMatch, out List <SubtitleMatch> otherChoices, Action <string> messageCallback)
        {
            var fn = Path.GetFileName(fileName);

            otherChoices = new List <SubtitleMatch>();
            List <SubtitleMatch> dummyChoices;

            SubtitleMatch subtitle = null;

            if (ServiceLocator.GetService <ISettings>().PreferenceToHashMatchedSubtitle)
            {
                subtitle = FindSubtitleForFilename(fileName, Path.GetDirectoryName(fileName), preferredLanguages, preferredServices, out imdbMatch, out dummyChoices, messageCallback, false, true, true);

                if (subtitle != null)
                {
                    return(DownloadSubtitle(subtitle, fileName));
                }
            }

            subtitle = FindSubtitleForFilename(fn, Path.GetDirectoryName(fileName), preferredLanguages, preferredServices, out imdbMatch, out dummyChoices, messageCallback, false, true, false);

            if (subtitle != null)
            {
                return(DownloadSubtitle(subtitle, fileName));
            }

            return(null);
        }
示例#2
0
        public static string DownloadSubtitle(SubtitleMatch subtitle, string fileName)
        {
            string resultFile = null;


            if (subtitle != null)
            {
                var dir = Path.GetDirectoryName(fileName);
                if (dir != null && Directory.Exists(dir))
                {
                    var    files  = SubtitleDownloaderFactory.GetSubtitleDownloader(subtitle.Service).SaveSubtitle(subtitle.Subtitle);
                    string tmpDir = files[0].Directory.FullName;

                    if (files.Count == 1)
                    {
                        string subNewName = Path.Combine(dir,
                                                         string.Format("{0}_{1}{2}", Path.GetFileNameWithoutExtension(fileName),
                                                                       subtitle.Subtitle.LanguageCode, Path.GetExtension(files[0].FullName)));
                        lock (_fileLocker)
                        {
                            if (File.Exists(subNewName))
                            {
                                File.Delete(subNewName);
                            }
                            resultFile = subNewName;
                            try
                            {
                                files[0].MoveTo(subNewName);
                            }
                            catch
                            {
                                // read only folder
                                resultFile = files[0].FullName;
                            }
                        }
                    }
                    else
                    {
                        var  cd            = new Regex(@"cd(\d+)", RegexOptions.IgnoreCase);
                        int  cdIndex       = 0;
                        bool isMultiVolume = false;

                        if (cd.IsMatch(Path.GetFileNameWithoutExtension(fileName)))
                        {
                            // multivolume video : find which CD is this
                            cdIndex       = int.Parse(cd.Match(Path.GetFileNameWithoutExtension(fileName)).Groups[0].Value);
                            isMultiVolume = true;
                        }

                        string subNewName = Path.Combine(dir,
                                                         isMultiVolume
                                                             ? string.Format("{0}_CD{1}_{2}{3}",
                                                                             Path.GetFileNameWithoutExtension(fileName),
                                                                             cdIndex, subtitle.Subtitle.LanguageCode,
                                                                             Path.GetExtension(files[cdIndex].FullName))
                                                             : string.Format("{0}_{1}{2}",
                                                                             Path.GetFileNameWithoutExtension(fileName),
                                                                             subtitle.Subtitle.LanguageCode,
                                                                             Path.GetExtension(files[cdIndex].FullName)));

                        if (File.Exists(subNewName))
                        {
                            File.Delete(subNewName);
                        }
                        try
                        {
                            files[cdIndex].MoveTo(subNewName);
                            resultFile = files[cdIndex].FullName;
                        }
                        catch
                        {
                            // read only folder
                            resultFile = files[cdIndex].FullName;
                        }
                    }

                    if (Path.GetTempPath().ToLowerInvariant() != tmpDir.ToLowerInvariant() &&
                        tmpDir.ToLowerInvariant().StartsWith(Path.GetTempPath().ToLowerInvariant()))
                    {
                        if (File.Exists(tmpDir))
                        {
                            File.Delete(tmpDir);
                        }
                        else if (Directory.Exists(tmpDir))
                        {
                            Directory.Delete(tmpDir, true);
                        }
                    }
                }
            }

            return(resultFile);
        }