示例#1
0
        private string addSeasonsDirIfDesired(string path)
        {
            if (useSeasonSubDirs())
            {
                string[] dirs = path.Split(new char[] { Path.DirectorySeparatorChar });
                //figure out if we are in a season dir
                string[] seasondirs = Helper.ReadProperties(Config.Extract);
                string   aSeasondir = "";

                if (Directory.Exists(path))
                {
                    string[] Directories = Directory.GetDirectories(path);
                    for (int i = 0; i < seasondirs.Length; i++)
                    {
                        foreach (string dir in Directories)
                        {
                            aSeasondir = RegexConverter.replaceSeriesnameAndSeason(seasondirs[i], nameOfSeries, season);
                            if (dirs.Length > 1)
                            {
                                Match m = Regex.Match(dir, aSeasondir);

                                if (m.Success)
                                {
                                    return(path + System.IO.Path.DirectorySeparatorChar + Path.GetFileName(dir));
                                }
                            }
                        }
                    }
                }
            }
            return(path + ((useSeasonSubDirs())?seasonsSubDir():""));
        }
        /// <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);
        }
示例#3
0
        private string seasonsSubDir()
        {
            string seasondir = RegexConverter.replaceSeriesnameAndSeason(Helper.ReadProperties(Config.Extract)[0], nameOfSeries, season);

            return(System.IO.Path.DirectorySeparatorChar + seasondir);
        }
示例#4
0
        // TODO: function is still tooooooo large
        private void SetSeriesPath()
        {
            if (Season == -1 || Showname == "")
            {
                Destination = "";
                return;
            }
            //string basepath = Helper.ReadProperty(Config.LastDirectory);
            string DestinationPath = Helper.ReadProperty(Config.DestinationDirectory);

            if (!Directory.Exists(DestinationPath))
            {
                DestinationPath = FilePath.Path;
            }
            bool DifferentDestinationPath = FilePath.Path != DestinationPath;
            //for placing files in directory structure, figure out if selected directory is show name, otherwise create one
            bool isNetwork = FilePath.Path.StartsWith("" + Path.DirectorySeparatorChar + Path.DirectorySeparatorChar);

            string[] dirs        = this.source.Folders;
            bool     InSeriesDir = false;
            bool     InSeasonDir = false;
            //any (other) season dir
            bool InASeasonDir     = false;
            bool UseSeasonSubDirs = Helper.ReadBool(Config.UseSeasonSubDir);

            //figure out if we are in a season dir
            string[] seasondirs   = Helper.ReadProperties(Config.Extract);
            string   aSeasondir   = "";
            int      showdirlevel = 0;

            //figure out if we are in an extraction dir, if we are, we need to go upwards one level
            if (dirs.Length > 0 && Filepath.IsExtractionDirectory(dirs[dirs.Length - 1]))
            {
                DestinationPath = Filepath.goUpwards(DestinationPath, 1);
                List <string> blah = new List <string>(dirs);
                blah.RemoveAt(dirs.Length - 1);
                dirs = blah.ToArray();
            }

            //check if we are in a season and/or series directory
            //loop backwards so first season entry is used if nothing is recognized and folder has to be created
            for (int i = seasondirs.Length - 1; i >= 0; i--)
            {
                aSeasondir = RegexConverter.replaceSeriesname(seasondirs[i], nameOfSeries);
                bool InSomething = false;
                if (dirs.Length > 1)
                {
                    Match m = Regex.Match(dirs[dirs.Length - 1], aSeasondir);
                    int   parsedSeason;
                    Int32.TryParse(m.Groups["Season"].Value, out parsedSeason);
                    if (m.Success)
                    {
                        if (parsedSeason == season)
                        {
                            InSeasonDir = true;
                        }
                        InASeasonDir = true;
                        InSomething  = true;
                    }
                }

                //remove dots to avoid problems with series like "N.C.I.S." or "Dr. House"

                /*if (dirs.Length > 0 && dirs[dirs.Length - 1].Replace(".","").StartsWith(nameOfSeries.Replace(".",""))){
                 *  InSeriesDir=true;
                 * }*/
                if (dirs.Length > 0 && Helper.InitialsMatch(dirs[dirs.Length - 1], nameOfSeries))
                {
                    InSeriesDir = true;
                }
                /*else if (dirs.Length > 1 && dirs[dirs.Length - 2].Replace(".", "").StartsWith(nameOfSeries.Replace(".", "")))*/
                else if (dirs.Length > 1 && Helper.InitialsMatch(dirs[dirs.Length - 2], nameOfSeries))
                {
                    InSeriesDir  = true;
                    showdirlevel = 1;
                }
                if (InSomething)
                {
                    break;
                }
            }

            getCreateDirectory();
            if (createDirectoryStructure != DirectoryStructure.CreateDirectoryStructure || !isSeasonValid())
            {
                Destination = "";
                return;
            }
            //if files aren't meant to be moved somewhere else
            if (!DifferentDestinationPath)
            {
                //somewhere else, create new series dir
                if (!InSeriesDir && !InSeasonDir && !InASeasonDir)
                {
                    DestinationPath = addSeriesDir(DestinationPath);
                    DestinationPath = addSeasonsDirIfDesired(DestinationPath);
                }
                //in series dir, create seasons dir
                else if (InSeriesDir && !InASeasonDir)
                {
                    DestinationPath = addSeasonsDirIfDesired(DestinationPath);
                }
                //wrong season dir, add real seasons dir
                else if (InSeriesDir && InASeasonDir && !InSeasonDir)
                {
                    DestinationPath = Filepath.goUpwards(DestinationPath, 1);
                    if (showdirlevel == 0)
                    {
                        DestinationPath = addSeriesDir(DestinationPath);
                    }
                    DestinationPath = addSeasonsDirIfDesired(DestinationPath);
                }
                //wrong show dir, go back two levels and add proper dir structure
                else if (!InSeriesDir && InASeasonDir)
                {
                    DestinationPath = addSeriesDir(Filepath.goUpwards(DestinationPath, 2));
                    DestinationPath = addSeasonsDirIfDesired(DestinationPath);
                }
            }
            //if they should be moved
            else
            {
                DestinationPath = addSeriesDir(DestinationPath);
                DestinationPath = addSeasonsDirIfDesired(DestinationPath);
            }
            if (DestinationPath != FilePath.Path)
            {
                Destination = DestinationPath;
            }
            else
            {
                Destination = "";
            }
        }
示例#5
0
        private string seasonsSubDir()
        {
            string seasondir = RegexConverter.replaceSeriesnameAndSeason(Helper.ReadProperties(ConfigKeyConstants.SEASON_NR_EXTRACTION_PATTERNS_KEY)[0], showname, seasonNr);

            return(System.IO.Path.DirectorySeparatorChar + seasondir);
        }
示例#6
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);
            }
        }