Exemplo n.º 1
0
        /// <summary>
        /// Combines string value for strings for a file word type from simplify results
        /// </summary>
        /// <param name="simplifyResult">File name simplifying results</param>
        /// <param name="type">Type of file word we want string of</param>
        /// <param name="combinedWord">Resulting combination of all strings of specified type</param>
        /// <returns>whether any words of the specified type were found in results</returns>
        private static bool GetFileWord(FileHelper.SimplifyStringResults simplifyResult, FileWordType type, out string combinedWord)
        {
            // Initialize combined string
            combinedWord = string.Empty;

            // Check if any word of the desired type were remove during simplify
            if (simplifyResult.RemovedWords.ContainsKey(type))
            {
                // Get words of desired type
                List <string> words = simplifyResult.RemovedWords[type];

                // Go through each removed word and build single string with all of them together
                for (int i = 0; i < words.Count; i++)
                {
                    if (i != 0)
                    {
                        combinedWord += "_";
                    }
                    combinedWord += words[i];
                }

                // Words were found
                return(true);
            }

            // No words of type found
            return(false);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Builds formatted file name for TV episode file from an existing file path.
        /// </summary>
        /// <param name="episode1">First episode in file</param>
        /// <param name="episode2">Second episode in file</param>
        /// <param name="fullPath">Path of file to be formatted</param>
        /// <returns>Path with file name formatted</returns>
        public string BuildTvFileName(TvEpisode episode1, TvEpisode episode2, string fullPath)
        {
            // Get file name
            string fileName = Path.GetFileNameWithoutExtension(fullPath);

            // Get info from file using simplify function (pulls out file words and categorizes them)
            FileHelper.OptionalSimplifyRemoves simplifyOptions = FileHelper.OptionalSimplifyRemoves.Year;
            FileHelper.SimplifyStringResults   simplifyResults = FileHelper.BuildSimplifyResults(fileName, false, false, simplifyOptions, true, false, false, false);

            // Build file name
            string buildName = BuildFileName(episode1, episode2, simplifyResults);

            // Remove unsafe file characters and add extension
            return(FileHelper.GetSafeFileName(buildName));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Build file name for TV or movie file.
        /// </summary>
        /// <param name="movieName">Name of movie</param>
        /// <param name="episode1">First episode in file</param>
        /// <param name="episode2">Second episode in file</param>
        /// <param name="date">Date item was released</param>
        /// <param name="simplifyResults">File name simplifying results</param>
        /// <param name="differentiator"></param>
        /// <returns></returns>
        private string BuildFileName(string movieName, TvEpisode episode1, TvEpisode episode2, int year, FileHelper.SimplifyStringResults simplifyResults, string differentiator)
        {
            // Init file name
            string fileName = string.Empty;

            // Loop through each portion of format and add it to the file names
            foreach (FileNamePortion portion in this.Format)
            {
                // Get string value for current portion
                string value = portion.Header;
                switch (portion.Type)
                {
                case FileWordType.MovieName:
                    value = movieName;
                    break;

                case FileWordType.ShowName:
                    value = episode1.Show.DatabaseName;
                    break;

                case FileWordType.EpisodeName:
                    value = BuildEpisodeName(episode1, episode2);
                    break;

                case FileWordType.EpisodeNumber:
                    value = EpisodeFormat.BuildEpisodeString(episode1, episode2);
                    break;

                case FileWordType.Year:
                    value = year.ToString();
                    break;

                case FileWordType.FilePart:
                    if (!GetFileWord(simplifyResults, portion.Type, out value))
                    {
                        value = differentiator;
                    }
                    break;

                default:
                    GetFileWord(simplifyResults, portion.Type, out value);
                    break;
                }

                // Add portion with header/footer and apply option
                if (!string.IsNullOrWhiteSpace(value))
                {
                    // Add header
                    fileName += portion.Header;

                    // Apply case options
                    switch (portion.CaseOption)
                    {
                    case CaseOptionType.None:
                        break;

                    case CaseOptionType.LowerCase:
                        value = value.ToLower();
                        break;

                    case CaseOptionType.UpperCase:
                        value = value.ToUpper();
                        break;
                    }

                    // Apply whitespace options
                    fileName += Regex.Replace(value, @"\s+", portion.Whitespace);

                    // Add footer
                    fileName += portion.Footer;
                }
            }

            // Return file name that was built
            return(fileName);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Build file name for TV epiosode file.
 /// </summary>
 /// <param name="episode1">First episode in file</param>
 /// <param name="episode2">Second episode in file (if any)</param>
 /// <param name="simplifyResults">File name simplifying results</param>
 /// <returns>Resulting formatted file name string</returns>
 private string BuildFileName(TvEpisode episode1, TvEpisode episode2, FileHelper.SimplifyStringResults simplifyResults)
 {
     return(BuildFileName(string.Empty, episode1, episode2, 1, simplifyResults, string.Empty));
 }
Exemplo n.º 5
0
 /// <summary>
 /// Build file name for movie.
 /// </summary>
 /// <param name="movieName">Name of movie</param>
 /// <param name="date">Date movie was released</param>
 /// <param name="simplifyResults">File name simplifying results</param>
 /// <param name="differentiator">String that differentiates file from other similar files in same directory</param>
 /// <returns>Resulting formatted file name string</returns>
 private string BuildFileName(string movieName, int year, FileHelper.SimplifyStringResults simplifyResults, string differentiator)
 {
     return(BuildFileName(movieName, null, null, year, simplifyResults, differentiator));
 }
Exemplo n.º 6
0
        /// <summary>
        /// Builds formatted file name for movie from an existing file path.
        /// </summary>
        /// <param name="movie">The movie associated with file</param>
        /// <param name="fullPath">The path of current file to be renamed</param>
        /// <returns>Resulting formatted file name string</returns>
        public string BuildMovieFileName(Movie movie, string fullPath)
        {
            string fileName   = Path.GetFileNameWithoutExtension(fullPath);
            string folderPath = Path.GetDirectoryName(fullPath);
            string fileExt    = Path.GetExtension(fullPath).ToLower();

            // Get info from file using simplify function (pulls out file words and categorizes them)
            FileHelper.OptionalSimplifyRemoves simplifyOptions = FileHelper.OptionalSimplifyRemoves.Year;
            FileHelper.SimplifyStringResults   simpleResult    = FileHelper.BuildSimplifyResults(fileName, false, false, simplifyOptions, true, false, true, false);

            // Get all video files from folder that don't match current path
            string differentiator = string.Empty;

            if (Directory.Exists(folderPath)) // Check in case path was empty
            {
                string[]      dirFiles  = Directory.GetFiles(folderPath);
                List <string> diffFiles = dirFiles.ToList().FindAll(f => Path.GetFileName(f) != Path.GetFileName(fileName) && Settings.VideoFileTypes.Contains(Path.GetExtension(f)));

                // Get differentiating part of string for other files with similar name
                foreach (string diffFile in diffFiles)
                {
                    // Get file name
                    string diffFileName = Path.GetFileNameWithoutExtension(diffFile);

                    // Init differentiating string
                    string diff = string.Empty;

                    // Count number of characters in that match 2 file names and build string of different characters
                    int matchCnt = 0;
                    for (int i = 0; i < fileName.Length; i++)
                    {
                        if (i < diffFileName.Length && diffFileName[i].Equals(fileName[i]))
                        {
                            matchCnt++;
                        }
                        else
                        {
                            diff += fileName[i];
                        }
                    }

                    // If enough characters are common between the two strings it likely means there the same movie
                    if (matchCnt > 10 || matchCnt > (fileName.Length * 3) / 4)
                    {
                        // Save longest file name difference
                        if (diff.Length > differentiator.Length)
                        {
                            differentiator = diff;
                        }
                    }
                }

                // Simplify differentiator
                differentiator = Regex.Replace(differentiator, @"']+", " ");
                differentiator = Regex.Replace(differentiator, @"[!\?\u0028\u0029\:\]\[]+", " ");
                differentiator = Regex.Replace(differentiator, @"\W+|_", " ");
                differentiator = differentiator.Trim();
            }

            // Build file name
            string buildName = BuildFileName(movie.DisplayName, movie.DisplayYear, simpleResult, differentiator);

            // Remove unsafe file characters and add extension
            return(FileHelper.GetSafeFileName(buildName) + fileExt);
        }
Exemplo n.º 7
0
        /// <summary>
        /// Attempts to match string to content from the online database.
        /// </summary>
        /// <param name="search">Search string to match against</param>
        /// <param name="rootFolder">The root folder the content will belong to</param>
        /// <param name="folderPath">Folder path where the content should be moved to</param>
        /// <returns>Match content item, null if no match</returns>
        protected bool ContentMatch(string search, string rootFolder, string folderPath, bool fast, bool threaded, out Content match, Content knownContent)
        {
            // Create empty content
            Content emptyContent;

            switch (this.ContentType)
            {
            case ContentType.Movie:
                emptyContent = new Movie();
                break;

            case ContentType.TvShow:
                emptyContent = new TvShow();
                break;

            default:
                throw new Exception("Unknown content type");
            }
            emptyContent.Path       = folderPath;
            emptyContent.RootFolder = rootFolder;
            emptyContent.Found      = true;

            // Check for empty search condition
            if (string.IsNullOrEmpty(search))
            {
                match = emptyContent;
                return(false);
            }

            // Get year from search string
            int dirYear = FileHelper.GetYear(search);

            // Get list of simplified strings
            List <FileHelper.SimplifyStringResults> searches = new List <FileHelper.SimplifyStringResults>();

            // Get list of search bases
            List <string> searchBases = GetModifiedSearches(search);

            // Fast search: use first search base only
            if (fast)
            {
                FileHelper.SimplifyStringResults result = FileHelper.BuildSimplifyResults(searchBases[0], false, false, FileHelper.OptionalSimplifyRemoves.YearAndFollowing, true, false, true, false);
                searches.Add(result);
            }
            // Full search: Go through each search base and get simplified search options
            else
            {
                foreach (string searchBase in searchBases)
                {
                    // Get results from current base
                    List <FileHelper.SimplifyStringResults> currSearches = FileHelper.SimplifyString(searchBase);
                    currSearches.Add(new FileHelper.SimplifyStringResults(searchBase, new Dictionary <FileWordType, List <string> >(), ContentSearchMod.None));

                    // Add each result to full list of searches
                    foreach (FileHelper.SimplifyStringResults results in currSearches)
                    {
                        // Check if search already exist
                        bool exists = false;
                        foreach (FileHelper.SimplifyStringResults s in searches)
                        {
                            if (s.SimplifiedString == results.SimplifiedString)
                            {
                                exists = true;
                                break;
                            }
                        }

                        // If doesn't exist add it to searches
                        if (!exists && !string.IsNullOrWhiteSpace(results.SimplifiedString))
                        {
                            searches.Add(results);
                        }
                    }
                }
            }
            searches.Sort();

            // Create new status
            int         currSeachCnt;
            MatchStatus status;

            lock (searchLock)
            {
                currSeachCnt = ++searchCount;
                status       = new MatchStatus(searches.Count, this.ContentType);
                searchStatus.Add(currSeachCnt, status);
            }

            ContentSearchMod lowMods;
            Content          lowestModsMatch;

            // Add thread to pool for each search that need to be performed
            int searchNum = 0;

            while (searchNum < searches.Count)
            {
                // Check for any search results so far
                if (status.GetSearchResultWithLowestMods(out lowMods, out lowestModsMatch))
                {
                    // If search results have no mods or just year removed use them as final results
                    if (lowMods == ContentSearchMod.None || lowMods == ContentSearchMod.YearRemoved)
                    {
                        match = lowestModsMatch;
                        return(true);
                    }
                }

                // Limit number of search threads created
                if (status.NumStarted - status.NumCompleted >= Settings.General.NumSimultaneousSearches)
                {
                    Thread.Sleep(100);
                    continue;
                }

                // Build search arguments
                object[] args = { currSeachCnt, searchNum, searches[searchNum].SimplifiedString, folderPath, rootFolder, dirYear, searches[searchNum].Modifications, knownContent };

                // Threaded: add a search to thread pool
                if (threaded)
                {
                    ThreadPool.QueueUserWorkItem(new WaitCallback(SearchThread), args);
                    lock (searchLock)
                        status.SetSearchStarted(searchNum);
                }
                // Synchronized: call search method
                else
                {
                    SearchThread(args);
                }

                searchNum++;
            }

            // Wait for all search to complete
            while (status.NumCompleted < searches.Count)
            {
                // Check for any search results so far
                if (status.GetSearchResultWithLowestMods(out lowMods, out lowestModsMatch))
                {
                    // If search results have no mods or just year removed use them as final results
                    if (lowMods == ContentSearchMod.None || lowMods == ContentSearchMod.YearRemoved)
                    {
                        match = lowestModsMatch;
                        return(true);
                    }
                }

                Thread.Sleep(100);
            }

            // Clear status
            lock (searchLock)
                searchStatus.Remove(currSeachCnt);

            // Return result with lowest mods to search string
            if (status.GetSearchResultWithLowestMods(out lowMods, out lowestModsMatch))
            {
                match = lowestModsMatch;
                return(true);
            }
            else
            {
                match = emptyContent;
                return(false);
            }
        }