/// <summary> /// Loads and compile Parsing Expressions and also String Replacements /// </summary> /// <returns></returns> public static bool reLoadExpressions() { // build a list of all the regular expressions to apply bool error = false; try { MPTVSeriesLog.Write("Compiling Parsing Expressions"); sExpressions.Clear(); regularExpressions.Clear(); replacementRegexAfter.Clear(); replacementRegexBefore.Clear(); DBExpression[] expressions = DBExpression.GetAll(); foreach (DBExpression expression in expressions) { if (expression[DBExpression.cEnabled] != 0) { String sExpression = String.Empty; switch ((String)expression[DBExpression.cType]) { case DBExpression.cType_Simple: sExpression = ConvertSimpleExpressionToRegEx(expression[DBExpression.cExpression]); break; case DBExpression.cType_Regexp: sExpression = expression[DBExpression.cExpression]; break; } sExpression = sExpression.ToLower(); // replace series, season and episode by the valid DBEpisode column names sExpression = sExpression.Replace("<series>", "<" + DBSeries.cParsedName + ">"); sExpression = sExpression.Replace("<season>", "<" + DBEpisode.cSeasonIndex + ">"); sExpression = sExpression.Replace("<episode>", "<" + DBEpisode.cEpisodeIndex + ">"); sExpression = sExpression.Replace("<episode2>", "<" + DBEpisode.cEpisodeIndex2 + ">"); sExpression = sExpression.Replace("<title>", "<" + DBEpisode.cEpisodeName + ">"); sExpression = sExpression.Replace("<firstaired>", "<" + DBOnlineEpisode.cFirstAired + ">"); // we precompile the expressions here which is faster in the end try { regularExpressions.Add(new Regex(sExpression, RegexOptions.IgnoreCase | RegexOptions.ExplicitCapture | RegexOptions.Compiled)); sExpressions.Add(sExpression); } catch (Exception e) { // wrong regex MPTVSeriesLog.Write("Cannot use the following Expression: " + e.Message); } } } MPTVSeriesLog.Write("Finished Compiling Parsing Expressions, found " + sExpressions.Count.ToString() + " valid expressions"); } catch (Exception ex) { MPTVSeriesLog.Write("Error loading Parsing Expressions: " + ex.Message); error = true; } // now go for the replacements try { MPTVSeriesLog.Write("Compiling Replacement Expressions"); foreach (DBReplacements replacement in DBReplacements.GetAll()) { try { if (replacement[DBReplacements.cEnabled]) { String searchString = replacement[DBReplacements.cToReplace]; searchString = searchString .Replace("<space>", " "); string regexSearchString = searchString; if (!replacement[DBReplacements.cIsRegex]) { regexSearchString = Regex.Escape(searchString); } String replaceString = replacement[DBReplacements.cWith]; replaceString = replaceString .Replace("<space>", " ") .Replace("<empty>", ""); var replaceRegex = new Regex(regexSearchString, RegexOptions.Compiled | RegexOptions.IgnoreCase); if (replacement[DBReplacements.cBefore]) { replacementRegexBefore.Add(replaceRegex, replaceString); } else { replacementRegexAfter.Add(replaceRegex, replaceString); } if (replacement[DBReplacements.cTagEnabled]) { tags.Add(searchString); } } } catch (Exception e) { MPTVSeriesLog.Write("Cannot use the following Expression: " + e.Message); } } MPTVSeriesLog.Write("Finished Compiling Replacement Expressions, found " + (replacementRegexBefore.Count + replacementRegexAfter.Count).ToString() + " valid expressions"); return(error); } catch (Exception ex) { MPTVSeriesLog.Write("Error loading String Replacements: " + ex.Message); return(false); } }
static DBReplacements() { s_FieldToDisplayNameMap.Add(cEnabled, "Enabled"); s_FieldToDisplayNameMap.Add(cTagEnabled, "Used As Tag"); s_FieldToDisplayNameMap.Add(cBefore, "Run before matching"); s_FieldToDisplayNameMap.Add(cToReplace, "Replace this.."); s_FieldToDisplayNameMap.Add(cWith, "With this"); s_FieldToDisplayNameMap.Add(cIsRegex, "Is Regex"); DBReplacements dummy = new DBReplacements(); int nCurrentDBVersion = cDBVersion; int nUpgradeDBVersion = DBOption.GetOptions(DBOption.cDBReplacementsVersion); while (nUpgradeDBVersion != nCurrentDBVersion) // take care of the upgrade in the table { DBReplacements replacement = new DBReplacements(); switch (nUpgradeDBVersion) { case 2: //Add tagEnabled colum DBReplacements.GlobalSet(new DBReplacements(), DBReplacements.cTagEnabled, new DBValue(0), new SQLCondition()); replacement = new DBReplacements(3); replacement[DBReplacements.cTagEnabled] = 1; replacement.Commit(); replacement = new DBReplacements(4); replacement[DBReplacements.cTagEnabled] = 1; replacement.Commit(); replacement = new DBReplacements(5); replacement[DBReplacements.cTagEnabled] = 1; replacement.Commit(); //adding new replacement DBReplacements[] replacements = DBReplacements.GetAll(); replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = replacements.Length; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 1; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "DSR"; replacement[DBReplacements.cWith] = @"<empty>"; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = replacements.Length + 1; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 1; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "HR-HDTV"; replacement[DBReplacements.cWith] = @"<empty>"; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = replacements.Length + 2; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 1; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "HR.HDTV"; replacement[DBReplacements.cWith] = @"<empty>"; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = replacements.Length + 3; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 1; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "HDTV"; replacement[DBReplacements.cWith] = @"<empty>"; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = replacements.Length + 4; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 1; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "DVDMux"; replacement[DBReplacements.cWith] = @"<empty>"; try { replacement.Commit(); } catch (Exception) { } nUpgradeDBVersion++; break; case 3: //Disable regex setting for all DBReplacements.GlobalSet(new DBReplacements(), DBReplacements.cIsRegex, new DBValue(0), new SQLCondition()); nUpgradeDBVersion++; break; case 4: // add the part/roman stuff - defaults for comments var newIndex = DBReplacements.GetAll().Length; replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = newIndex++; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 0; replacement[DBReplacements.cBefore] = "1"; replacement[DBReplacements.cToReplace] = @"(?<=(\s?\.?P[ar]*t\s?)) (X)?(IX|IV|V?I{0,3})"; replacement[DBReplacements.cWith] = @"<RomanToArabic>"; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = newIndex++; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 0; replacement[DBReplacements.cBefore] = "1"; replacement[DBReplacements.cToReplace] = @"(?<!(?:S\d+.?E\\d+\-E\d+.*|S\d+.?E\d+.*|\s\d+x\d+.*))P[ar]*t\s?(\d+)(\s?of\s\d{1,2})?"; replacement[DBReplacements.cWith] = @" S01E${1} "; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = newIndex++; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 0; replacement[DBReplacements.cBefore] = "1"; replacement[DBReplacements.cToReplace] = @"(?<!(?:S\d+.?E\\d+\-E\d+.*|S\d+.?E\d+.*|\s\d+x\d+\s.*))(\d{1,2})\s?of\s\d{1,2}"; replacement[DBReplacements.cWith] = @" S01E${1} "; replacement[DBReplacements.cIsRegex] = true; try { replacement.Commit(); } catch (Exception) { } nUpgradeDBVersion++; break; case 5: newIndex = DBReplacements.GetAll().Length; replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = newIndex++; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 0; replacement[DBReplacements.cBefore] = "1"; replacement[DBReplacements.cToReplace] = @"(?-i)([A-Z])\.(?=[A-Z])"; replacement[DBReplacements.cWith] = @"${1}"; replacement[DBReplacements.cIsRegex] = true; try { replacement.Commit(); } catch (Exception) { } replacement = new DBReplacements(); replacement[DBReplacements.cIndex] = newIndex++; replacement[DBReplacements.cEnabled] = 1; replacement[DBReplacements.cTagEnabled] = 0; replacement[DBReplacements.cBefore] = "0"; replacement[DBReplacements.cToReplace] = "<space><space>"; replacement[DBReplacements.cWith] = @"<space>"; replacement[DBReplacements.cIsRegex] = false; try { replacement.Commit(); } catch (Exception) { } nUpgradeDBVersion++; break; default: { // no replacements in the db => put the default ones AddDefaults(); nUpgradeDBVersion = 6; } break; } } DBOption.SetOptions(DBOption.cDBReplacementsVersion, nCurrentDBVersion); }