Example #1
0
        private static List <DanbooruTag> RemoveIgnoredTags(DanbooruFilenameFormat format, List <DanbooruTag> groupedTags)
        {
            foreach (DanbooruTag ignoredTag in format.IgnoredTags)
            {
                if (format.IgnoreTagsUseRegex)
                {
                    if (!String.IsNullOrWhiteSpace(format.IgnoredTagsRegex))
                    {
                        Regex re = new Regex(format.IgnoredTagsRegex, RegexOptions.IgnoreCase);

                        if (format.IgnoredTagsOnlyForGeneral)
                        {
                            groupedTags.RemoveAll(x => x.Type == DanbooruTagType.General && re.IsMatch(x.Name));
                        }
                        else
                        {
                            groupedTags.RemoveAll(x => re.IsMatch(x.Name));
                        }
                    }
                }
                else
                {
                    groupedTags.RemoveAll(x => x.Name == ignoredTag.Name);
                }
            }

            return(groupedTags);
        }
Example #2
0
        /// <summary>
        /// For constructing TAGS_FILENAME.
        /// </summary>
        /// <param name="baseFolder"></param>
        /// <param name="format"></param>
        /// <param name="post"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public static string MakeFilename(DanbooruFilenameFormat format, DanbooruPost post)
        {
            string filename         = format.FilenameFormat;
            string provider         = post.Provider.Name;
            string query            = post.Query;
            string searchTags       = post.SearchTags;
            string originalFileName = post.FileUrl.Split('/').Last();

            // sanitizing the format
            filename = Helper.SanitizeFilename(filename, true);

            //remove extension
            originalFileName = originalFileName.Substring(0, originalFileName.LastIndexOf('.'));
            originalFileName = Uri.UnescapeDataString(originalFileName);

            filename = filename.Replace("%provider%", Helper.SanitizeFilename(provider));
            filename = filename.Replace("%id%", post.Id);
            filename = filename.Replace("%rating%", post.Rating);
            filename = filename.Replace("%md5%", post.MD5);
            filename = filename.Replace("%searchParam%", Helper.SanitizeFilename(query));
            filename = filename.Replace("%searchtag%", Helper.SanitizeFilename(searchTags));
            filename = filename.Replace("%originalFilename%", Helper.SanitizeFilename(originalFileName));

            string dtFormat = Properties.Settings.Default.DateTimeFormat;

            if (String.IsNullOrWhiteSpace(dtFormat))
            {
                dtFormat = "yyyy-mm-dd HH:mm:ss";
            }
            filename = filename.Replace("%uploadDateTime%", Helper.SanitizeFilename(post.CreatedAtDateTime.ToString(dtFormat)));

            // copy the tags entity to be grouped.
            var groupedTags = post.TagsEntity;

            // custom sort to prioritize some tags based on file definition
            // Issue #46 and #81
            // regex support
            if (File.Exists(PRIORITY_TAGS))
            {
                var priorityTags = ReadTagsFromTextFile(PRIORITY_TAGS);
                groupedTags.Sort((a, b) =>
                {
                    var containA = priorityTags.Exists(x => Regex.IsMatch(a.Name, x.Name));
                    var containB = priorityTags.Exists(x => Regex.IsMatch(b.Name, x.Name));

                    if (containA && !containB)
                    {
                        return(-1);
                    }
                    if (!containA && containB)
                    {
                        return(1);
                    }
                    else
                    {
                        return(a.CompareTo(b));
                    }
                });
            }
            else
            {
                groupedTags.Sort();
            }

            // remove ignored tags
            groupedTags = RemoveIgnoredTags(format, groupedTags);

            // artist
            var artist = FilterTags(post,
                                    groupedTags,
                                    DanbooruTagType.Artist,
                                    format.ArtistGroupLimit,
                                    format.ArtistGroupReplacement,
                                    format.MissingTagReplacement,
                                    format.IsReplaceMode,
                                    format.TagReplaceUnderscoreToSpace);
            var artistStr = Helper.SanitizeFilename(artist).Trim();

            filename = filename.Replace("%artist%", artistStr);

            // copyright
            var copyright = FilterTags(post,
                                       groupedTags,
                                       DanbooruTagType.Copyright,
                                       format.CopyrightGroupLimit,
                                       format.CopyrightGroupReplacement,
                                       format.MissingTagReplacement,
                                       format.IsReplaceMode,
                                       format.TagReplaceUnderscoreToSpace);
            var copyStr = Helper.SanitizeFilename(copyright.Trim());

            filename = filename.Replace("%copyright%", copyStr);

            // character
            var character = FilterTags(post,
                                       groupedTags,
                                       DanbooruTagType.Character,
                                       format.CharacterGroupLimit,
                                       format.CharacterGroupReplacement,
                                       format.MissingTagReplacement,
                                       format.IsReplaceMode,
                                       format.TagReplaceUnderscoreToSpace);
            var charaStr = Helper.SanitizeFilename(character.Trim());

            filename = filename.Replace("%character%", charaStr);

            // cirle
            var circle = FilterTags(post,
                                    groupedTags,
                                    DanbooruTagType.Circle,
                                    format.CircleGroupLimit,
                                    format.CircleGroupReplacement,
                                    format.MissingTagReplacement,
                                    format.IsReplaceMode,
                                    format.TagReplaceUnderscoreToSpace);
            var circleStr = Helper.SanitizeFilename(circle.Trim());

            filename = filename.Replace("%circle%", circleStr);

            // faults
            var faults = FilterTags(post,
                                    groupedTags,
                                    DanbooruTagType.Faults,
                                    format.FaultsGroupLimit,
                                    format.FaultsGroupReplacement,
                                    format.MissingTagReplacement,
                                    format.IsReplaceMode,
                                    format.TagReplaceUnderscoreToSpace);
            var faultStr = Helper.SanitizeFilename(faults.Trim());

            filename = filename.Replace("%faults%", faultStr);

            // general
            var general = FilterTags(post,
                                     groupedTags,
                                     DanbooruTagType.General,
                                     0,
                                     "",
                                     "",
                                     format.IsReplaceMode,
                                     format.TagReplaceUnderscoreToSpace);
            var generalStr = Helper.SanitizeFilename(general.Trim());

            filename = filename.Replace("%general%", generalStr);

            // all tags
            var allTempTags = groupedTags.Select(x => x.Name).ToList();

            if (format.TagReplaceUnderscoreToSpace)
            {
                for (int i = 0; i < allTempTags.Count; i++)
                {
                    allTempTags[i] = allTempTags[i].Replace("_", " ").Trim();
                }
            }
            filename = filename.Replace("%tags%", Helper.SanitizeFilename(string.Join(" ", allTempTags)));

            // append base folder from Save Folder text box
            if (format.BaseFolder.EndsWith(@"\"))
            {
                filename = format.BaseFolder + filename;
            }
            else if (!String.IsNullOrWhiteSpace(format.BaseFolder))
            {
                filename = format.BaseFolder + @"\" + filename;
            }

            filename = filename.Substring(0, filename.Length < format.Limit ? filename.Length : format.Limit).Trim();

            return(filename);
        }
Example #3
0
        /// <summary>
        /// For constructing TAGS_FILENAME.
        /// </summary>
        /// <param name="baseFolder"></param>
        /// <param name="format"></param>
        /// <param name="post"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public static string MakeFilename(DanbooruFilenameFormat format, DanbooruPost post)
        {
            string filename         = format.FilenameFormat;
            string provider         = post.Provider.Name;
            string query            = post.Query;
            string searchTags       = post.SearchTags;
            string originalFileName = post.FileUrl.Split('/').Last();

            // sanitizing the format
            filename = Helper.SanitizeFilename(filename, true);

            //remove extension
            originalFileName = originalFileName.Substring(0, originalFileName.LastIndexOf('.'));
            originalFileName = Uri.UnescapeDataString(originalFileName);

            filename = filename.Replace("%provider%", Helper.SanitizeFilename(provider));
            filename = filename.Replace("%id%", post.Id);
            filename = filename.Replace("%rating%", post.Rating);
            filename = filename.Replace("%md5%", post.MD5);
            filename = filename.Replace("%searchParam%", Helper.SanitizeFilename(query));
            filename = filename.Replace("%searchtag%", Helper.SanitizeFilename(searchTags));
            filename = filename.Replace("%originalFilename%", Helper.SanitizeFilename(originalFileName));

            // copy the tags entity to be grouped.
            var groupedTags = post.TagsEntity;

            groupedTags.Sort();

            // remove ignored tags
            groupedTags = RemoveIgnoredTags(format, groupedTags);

            // artist
            var artist = FilterTags(post,
                                    groupedTags,
                                    DanbooruTagType.Artist,
                                    format.ArtistGroupLimit,
                                    format.ArtistGroupReplacement,
                                    format.MissingTagReplacement,
                                    format.IsReplaceMode,
                                    format.TagReplaceUnderscoreToSpace);
            var artistStr = Helper.SanitizeFilename(artist).Trim();

            if (String.IsNullOrEmpty(artistStr))
            {
                artistStr = DanbooruDownloader3.Properties.Settings.Default.tagNoArtistValue;
            }
            filename = filename.Replace("%artist%", artistStr);

            // copyright
            var copyright = FilterTags(post,
                                       groupedTags,
                                       DanbooruTagType.Copyright,
                                       format.CopyrightGroupLimit,
                                       format.CopyrightGroupReplacement,
                                       format.MissingTagReplacement,
                                       format.IsReplaceMode,
                                       format.TagReplaceUnderscoreToSpace);
            var copyStr = Helper.SanitizeFilename(copyright.Trim());

            if (String.IsNullOrEmpty(copyStr))
            {
                copyStr = DanbooruDownloader3.Properties.Settings.Default.tagNoCopyrightValue;
            }
            filename = filename.Replace("%copyright%", copyStr);

            // character
            var character = FilterTags(post,
                                       groupedTags,
                                       DanbooruTagType.Character,
                                       format.CharacterGroupLimit,
                                       format.CharacterGroupReplacement,
                                       format.MissingTagReplacement,
                                       format.IsReplaceMode,
                                       format.TagReplaceUnderscoreToSpace);
            var charaStr = Helper.SanitizeFilename(character.Trim());

            if (String.IsNullOrEmpty(charaStr))
            {
                charaStr = DanbooruDownloader3.Properties.Settings.Default.tagNoCharaValue;
            }
            filename = filename.Replace("%character%", charaStr);

            // cirle
            var circleSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Circle).Select(x => x.Name);
            var circle          = FilterTags(post,
                                             groupedTags,
                                             DanbooruTagType.Circle,
                                             format.CircleGroupLimit,
                                             format.CircleGroupReplacement,
                                             format.MissingTagReplacement,
                                             format.IsReplaceMode,
                                             format.TagReplaceUnderscoreToSpace);
            var circleStr = Helper.SanitizeFilename(circle.Trim());

            if (String.IsNullOrEmpty(circleStr))
            {
                circleStr = DanbooruDownloader3.Properties.Settings.Default.tagNoCircleValue;
            }
            filename = filename.Replace("%circle%", circleStr);

            // faults
            var faultsSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Faults).Select(x => x.Name);
            var faults          = FilterTags(post,
                                             groupedTags,
                                             DanbooruTagType.Faults,
                                             format.FaultsGroupLimit,
                                             format.FaultsGroupReplacement,
                                             format.MissingTagReplacement,
                                             format.IsReplaceMode,
                                             format.TagReplaceUnderscoreToSpace);
            var faultStr = Helper.SanitizeFilename(faults.Trim());

            if (String.IsNullOrEmpty(faultStr))
            {
                faultStr = DanbooruDownloader3.Properties.Settings.Default.tagNoFaultValue;
            }
            filename = filename.Replace("%faults%", faultStr);

            // all tags
            var allTempTags = groupedTags.Select(x => x.Name).ToList();

            if (format.TagReplaceUnderscoreToSpace)
            {
                for (int i = 0; i < allTempTags.Count; i++)
                {
                    allTempTags[i] = allTempTags[i].Replace("_", " ").Trim();
                }
            }
            filename = filename.Replace("%tags%", Helper.SanitizeFilename(string.Join(" ", allTempTags)));

            // append base folder from Save Folder text box
            if (format.BaseFolder.EndsWith(@"\"))
            {
                filename = format.BaseFolder + filename;
            }
            else if (!String.IsNullOrWhiteSpace(format.BaseFolder))
            {
                filename = format.BaseFolder + @"\" + filename;
            }

            filename = filename.Substring(0, filename.Length < format.Limit ? filename.Length : format.Limit).Trim();

            // check if contains subdirectory
            if (filename.Contains(@"\"))
            {
                string dir = filename.Substring(0, filename.LastIndexOf(@"\"));
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }

            return(filename);
        }
Example #4
0
        /// <summary>
        /// For constructing TAGS_FILENAME.
        /// </summary>
        /// <param name="baseFolder"></param>
        /// <param name="format"></param>
        /// <param name="post"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        public static string MakeFilename(DanbooruFilenameFormat format, DanbooruPost post)
        {
            string filename         = format.FilenameFormat;
            string provider         = post.Provider.Name;
            string query            = post.Query;
            string searchTags       = post.SearchTags;
            string originalFileName = post.FileUrl.Split('/').Last();

            // sanitizing the format
            filename = Helper.SanitizeFilename(filename, true);

            //remove extension
            originalFileName = originalFileName.Substring(0, originalFileName.LastIndexOf('.'));
            originalFileName = Uri.UnescapeDataString(originalFileName);

            filename = filename.Replace("%provider%", Helper.SanitizeFilename(provider));
            filename = filename.Replace("%id%", post.Id);
            filename = filename.Replace("%rating%", post.Rating);
            filename = filename.Replace("%md5%", post.MD5);
            filename = filename.Replace("%searchParam%", Helper.SanitizeFilename(query));
            filename = filename.Replace("%searchtag%", Helper.SanitizeFilename(searchTags));
            filename = filename.Replace("%originalFilename%", Helper.SanitizeFilename(originalFileName));

            // copy the tags entity to be grouped.
            var groupedTags = post.TagsEntity;

            // remove ignored tags
            foreach (DanbooruTag ignoredTag in format.IgnoredTags)
            {
                if (format.IgnoreTagsUseRegex)
                {
                    if (!String.IsNullOrWhiteSpace(format.IgnoredTagsRegex))
                    {
                        //Program.Logger.Debug("Ignore Regex: " + format.IgnoredTagsRegex);
                        Regex re = new Regex(format.IgnoredTagsRegex, RegexOptions.IgnoreCase);
                        groupedTags.RemoveAll(x => re.IsMatch(x.Name));
                    }
                }
                else
                {
                    groupedTags.RemoveAll(x => x.Name == ignoredTag.Name);
                }
            }

            var artistSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Artist).Select(x => x.Name);
            var artist          = "";

            if (artistSelection != null)
            {
                if (artistSelection.Count() >= format.ArtistGroupLimit && !string.IsNullOrWhiteSpace(format.ArtistGroupReplacement))
                {
                    artist = format.ArtistGroupReplacement;
                    groupedTags.RemoveAll(x => x.Type == DanbooruTagType.Artist);
                    groupedTags.Add(new DanbooruTag()
                    {
                        Name = format.ArtistGroupReplacement
                    });
                }
                else
                {
                    artist = string.Join(" ", artistSelection);
                }
            }
            if (string.IsNullOrWhiteSpace(artist))
            {
                artist = format.MissingTagReplacement;
            }
            filename = filename.Replace("%artist%", Helper.SanitizeFilename(artist).Trim());

            var copyrightSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Copyright).Select(x => x.Name);
            var copyright          = "";

            if (copyrightSelection != null)
            {
                if (copyrightSelection.Count() >= format.CopyrightGroupLimit &&
                    format.CopyrightGroupLimit > 1 &&
                    !string.IsNullOrWhiteSpace(format.CopyrightGroupReplacement))
                {
                    copyright = format.CopyrightGroupReplacement;
                    groupedTags.RemoveAll(x => x.Type == DanbooruTagType.Artist);
                    groupedTags.Add(new DanbooruTag()
                    {
                        Name = format.ArtistGroupReplacement
                    });
                }
                else
                {
                    copyright = string.Join(" ", copyrightSelection);
                }
            }
            if (string.IsNullOrWhiteSpace(copyright))
            {
                copyright = format.MissingTagReplacement;
            }
            filename = filename.Replace("%copyright%", Helper.SanitizeFilename(copyright.Trim()));

            var characterSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Character).Select(x => x.Name);
            var character          = "";

            if (characterSelection != null)
            {
                if (characterSelection.Count() >= format.CharacterGroupLimit &&
                    format.CharacterGroupLimit > 1 &&
                    !string.IsNullOrWhiteSpace(format.CharacterGroupReplacement))
                {
                    character = format.CharacterGroupReplacement;
                    groupedTags.RemoveAll(x => x.Type == DanbooruTagType.Character);
                    groupedTags.Add(new DanbooruTag()
                    {
                        Name = format.CharacterGroupReplacement
                    });
                }
                else
                {
                    character = string.Join(" ", characterSelection);
                }
            }
            if (string.IsNullOrWhiteSpace(character))
            {
                character = format.MissingTagReplacement;
            }
            filename = filename.Replace("%character%", Helper.SanitizeFilename(character.Trim()));

            var circleSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Circle).Select(x => x.Name);
            var circle          = "";

            if (circleSelection != null)
            {
                if (circleSelection.Count() >= format.CircleGroupLimit &&
                    format.CircleGroupLimit > 1 &&
                    !string.IsNullOrWhiteSpace(format.CircleGroupReplacement))
                {
                    circle = format.CircleGroupReplacement;
                    groupedTags.RemoveAll(x => x.Type == DanbooruTagType.Circle);
                    groupedTags.Add(new DanbooruTag()
                    {
                        Name = format.CircleGroupReplacement
                    });
                }
                else
                {
                    circle = string.Join(" ", circleSelection);
                }
            }
            if (string.IsNullOrWhiteSpace(circle))
            {
                circle = format.MissingTagReplacement;
            }
            filename = filename.Replace("%circle%", Helper.SanitizeFilename(circle.Trim()));

            var faultsSelection = post.TagsEntity.Where <DanbooruTag>(x => x.Type == DanbooruTagType.Faults).Select(x => x.Name);
            var faults          = "";

            if (faultsSelection != null)
            {
                if (faultsSelection.Count() >= format.FaultsGroupLimit &&
                    format.FaultsGroupLimit > 1 &&
                    !string.IsNullOrWhiteSpace(format.FaultsGroupReplacement))
                {
                    faults = format.FaultsGroupReplacement;
                    groupedTags.RemoveAll(x => x.Type == DanbooruTagType.Faults);
                    groupedTags.Add(new DanbooruTag()
                    {
                        Name = format.FaultsGroupReplacement
                    });
                }
                else
                {
                    faults = string.Join(" ", faultsSelection);
                }
            }
            if (string.IsNullOrWhiteSpace(faults))
            {
                faults = format.MissingTagReplacement;
            }
            filename = filename.Replace("%faults%", Helper.SanitizeFilename(faults.Trim()));

            groupedTags.Sort();
            filename = filename.Replace("%tags%", Helper.SanitizeFilename(string.Join(" ", groupedTags.Select(x => x.Name))));

            // append base folder from Save Folder text box
            if (format.BaseFolder.EndsWith(@"\"))
            {
                filename = format.BaseFolder + filename;
            }
            else if (!String.IsNullOrWhiteSpace(format.BaseFolder))
            {
                filename = format.BaseFolder + @"\" + filename;
            }

            filename = filename.Substring(0, filename.Length < format.Limit ? filename.Length : format.Limit).Trim();

            // check if contains subdirectory
            if (filename.Contains(@"\"))
            {
                string dir = filename.Substring(0, filename.LastIndexOf(@"\"));
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
            }

            return(filename);
        }