Cardigann: Add .Today.Year to template variables. Resolves #8258 (#8269)

This commit is contained in:
Cory 2020-04-19 19:16:19 -05:00 committed by GitHub
parent 3830c15bd4
commit c9aec23304
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 16 additions and 31 deletions

View File

@ -224,41 +224,26 @@ namespace Jackett.Common.Indexers
} }
} }
protected Dictionary<string, object> getTemplateVariablesFromConfigData() protected Dictionary<string, object> GetBaseTemplateVariables()
{ {
var variables = new Dictionary<string, object> var variables = new Dictionary<string, object>
{ {
[".Config.sitelink"] = SiteLink, [".Config.sitelink"] = SiteLink,
[".True"] = "True", [".True"] = "True",
[".False"] = null [".False"] = null,
[".Today.Year"] = DateTime.Today.Year.ToString()
}; };
foreach (var Setting in Definition.Settings) foreach (var setting in Definition.Settings)
{ variables[".Config." + setting.Name] = configData.GetDynamic(setting.Name) switch
var item = configData.GetDynamic(Setting.Name);
// CheckBox item is an array of strings
if (item.GetType() == typeof(CheckboxItem))
{ {
variables[".Config." + Setting.Name] = ((CheckboxItem)item).Values; CheckboxItem checkbox => checkbox.Values,
} BoolItem boolItem => variables[boolItem.Value ? ".True" : ".False"],
else SelectItem selectItem => selectItem.Value,
{ StringItem stringItem => stringItem.Value,
string value; // Throw exception here to match original functionality.
if (item.GetType() == typeof(BoolItem)) // Currently this will only throw for ImageItem.
{ _ => throw new NotSupportedException()
value = (((BoolItem)item).Value == true ? "true" : ""); };
}
else if (item.GetType() == typeof(SelectItem))
{
value = ((SelectItem)item).Value;
}
else
{
value = ((StringItem)item).Value;
}
variables[".Config." + Setting.Name] = value;
}
}
return variables; return variables;
} }
@ -269,7 +254,7 @@ namespace Jackett.Common.Indexers
{ {
if (variables == null) if (variables == null)
{ {
variables = getTemplateVariablesFromConfigData(); variables = GetBaseTemplateVariables();
} }
// handle re_replace expression // handle re_replace expression
@ -1226,7 +1211,7 @@ namespace Jackett.Common.Indexers
var Search = Definition.Search; var Search = Definition.Search;
// init template context // init template context
var variables = getTemplateVariablesFromConfigData(); var variables = GetBaseTemplateVariables();
variables[".Query.Type"] = query.QueryType; variables[".Query.Type"] = query.QueryType;
variables[".Query.Q"] = query.SearchTerm; variables[".Query.Q"] = query.SearchTerm;
@ -1764,7 +1749,7 @@ namespace Jackett.Common.Indexers
if (Definition.Download != null) if (Definition.Download != null)
{ {
var Download = Definition.Download; var Download = Definition.Download;
var variables = getTemplateVariablesFromConfigData(); var variables = GetBaseTemplateVariables();
AddTemplateVariablesFromUri(variables, link, ".DownloadUri"); AddTemplateVariablesFromUri(variables, link, ".DownloadUri");
if (Download.Before != null) if (Download.Before != null)
{ {