/// <summary>
        /// Extracts season from directory name
        /// </summary>
        /// <param name="path">path from which to extract the data (NO FILEPATH, JUST FOLDER)</param>
        /// <returns>recognized season, -1 if not recognized</returns>
        public int ExtractSeasonFromDirectory(string path)
        {
            string[] patterns = Helper.ReadProperties(Config.Extract);
            string[] folders  = path.Split(new char[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
            for (int i = patterns.Length - 1; i >= 0; i--)
            {
                string pattern = RegexConverter.toRegex(patterns[i]);
                Match  m       = Regex.Match(folders[folders.Length - 1], pattern, RegexOptions.IgnoreCase);

                if (m.Success)
                {
                    try {
                        return(Int32.Parse(m.Groups["Season"].Value));
                    }
                    catch (Exception) {
                        return(-1);
                    }
                }
            }
            return(-1);
        }
Beispiel #2
0
        /// <summary>
        /// Extracts all downloaded archives and moves subtitles to the movie files with proper naming
        /// </summary>
        /// <param name="i">Index of the temporary directory in which subtitles are stored. Temp Directory Name is "TEMP"+i</param>
        public void ProcessSubtitles(int i)
        {
            string        folder     = Helper.ReadProperty(Config.LastDirectory) + "TEMP" + i.ToString();
            List <string> extensions = new List <string>(Helper.ReadProperties(Config.SubtitleExtensions, true));

            if (extensions == null)
            {
                Logger.Instance.LogMessage("No Subtitle Extensions found!", LogLevel.WARNING);
                return;
            }

            if (Directory.Exists(folder))
            {
                extractArchives(folder, extensions);
                //now that everything is extracted, try to assign subtitles to episodes
                //first, figure out episode and season numbers from filenames

                //scan for subtitle files in temp folder
                List <FileSystemInfo> Files = new List <FileSystemInfo>();
                int count = 0;
                foreach (string ex in extensions)
                {
                    List <FileSystemInfo> fsi = Helper.GetAllFilesRecursively(folder, "*." + ex, ref count, null);
                    Files.AddRange(fsi);
                }
                string[] patterns = Helper.ReadProperties(Config.EpIdentifier);
                foreach (FileSystemInfo file in Files)
                {
                    int Season  = -1;
                    int Episode = -1;
                    foreach (string str in patterns)
                    {
                        //replace %S and %E by proper regexps
                        string pattern = RegexConverter.toRegex(str);
                        Match  m       = Regex.Match(file.Name, pattern, RegexOptions.IgnoreCase | RegexOptions.RightToLeft);
                        if (m.Success)
                        {
                            try {
                                Season = Int32.Parse(m.Groups["Season"].Value);
                            }
                            catch (FormatException) {
                                Logger.Instance.LogMessage("Cannot parse " + m.Groups["Season"].Value + " to an integer", LogLevel.WARNING);
                            }
                            try {
                                Episode = Int32.Parse(m.Groups["Episode"].Value);
                            }
                            catch (FormatException) {
                                Logger.Instance.LogMessage("Cannot parse " + m.Groups["Episode"].Value + " to an integer", LogLevel.WARNING);
                            }
                            break;
                        }
                    }

                    //now that season and episode are known, assign the filename to a SubtitleFile object
                    bool contains = false;
                    foreach (SubtitleFile s in this.subtitles)
                    {
                        if (Season != -1 && Episode != -1 && s.Episode == Episode && s.Season == Season)
                        {
                            s.Filenames.Add(file.Name);
                            contains = true;
                        }
                    }
                    if (!contains)
                    {
                        SubtitleFile sf = new SubtitleFile();
                        sf.Episode = Episode;
                        sf.Season  = Season;
                        sf.Filenames.Add(file.Name);
                        this.subtitles.Add(sf);
                    }
                }
                int MatchedSubtitles = 0;
                //Move subtitle files to their video files
                foreach (InfoEntry ie in InfoEntryManager.Instance)
                {
                    List <string> ext = new List <string>(Helper.ReadProperties(Config.Extensions));
                    for (int b = 0; b < ext.Count; b++)
                    {
                        ext[b] = ext[b].ToLower();
                    }
                    if (ext.Contains(Path.GetExtension(ie.Filename).Substring(1).ToLower()) && ie.ProcessingRequested && ie.Episode != -1 && ie.Season != -1)
                    {
                        foreach (SubtitleFile sf in this.subtitles)
                        {
                            if (sf.Season == ie.Season && sf.Episode == ie.Episode)
                            {
                                bool   move   = false;
                                string source = "";
                                string target = ie.FilePath.Path + Path.DirectorySeparatorChar + Path.GetFileNameWithoutExtension(ie.Filename) + Path.GetExtension(sf.Filenames[0]);
                                if (sf.Filenames.Count == 1)
                                {
                                    move   = true;
                                    source = folder + Path.DirectorySeparatorChar + sf.Filenames[0];
                                }
                                else
                                {
                                    FileSelector fs = new FileSelector(sf.Filenames);
                                    if (fs.ShowDialog() == DialogResult.OK)
                                    {
                                        move   = true;
                                        source = folder + Path.DirectorySeparatorChar + sf.Filenames[fs.selection];
                                    }
                                }

                                if (File.Exists(target))
                                {
                                    if (MessageBox.Show(target + " already exists. Overwrite?", "Overwrite?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                                    {
                                        File.Delete(target);
                                    }
                                    else
                                    {
                                        move = false;
                                    }
                                }
                                if (move)
                                {
                                    try {
                                        File.Copy(source, target);
                                        MatchedSubtitles++;
                                    }
                                    catch (Exception ex) {
                                        Logger.Instance.LogMessage(source + " --> " + target + ": " + ex.Message, LogLevel.ERROR);
                                    }
                                }
                            }
                        }
                    }
                }
                Logger.Instance.LogMessage("Downloaded " + Files.Count + " subtitles and matched " + MatchedSubtitles + " of them.", LogLevel.INFO);
                //cleanup
                this.subtitles.Clear();
                Directory.Delete(folder, true);
                //UpdateList(true);
            }
        }