diff --git a/CONTROL/control b/CONTROL/control index 5606a4d..bcca849 100644 --- a/CONTROL/control +++ b/CONTROL/control @@ -1,9 +1,9 @@ Package: sweeper Priority: optional Section: misc -Version: 2.0.14 +Version: 2.0.15 Architecture: mipsel Maintainer: af123@hummypkg.org.uk -Depends: webif(>=1.2.5) +Depends: webif(>=1.2.5-10) Description: Sweeper is a package for managing recordings in a variety of ways using custom rules [See forum for details] Tags: http://hummy.tv/forum/threads/5138/ diff --git a/webif/plugin/sweeper/auto.hook b/webif/plugin/sweeper/auto.hook index 9489714..1930e37 100644 --- a/webif/plugin/sweeper/auto.hook +++ b/webif/plugin/sweeper/auto.hook @@ -39,6 +39,7 @@ set ::sweeper::expand_fns { regsub {2} asfilename {1 {inline}} format {2 {inline}} + var {1 {inline}} } proc ::sweeper::expand_replace {ts &ret search replace} { @@ -65,6 +66,14 @@ proc ::sweeper::expand_format {ts &ret format arg} { return $r } +proc ::sweeper::expand_var {ts &ret arg} { + if {![dict exists $::sweeper::stack $arg]} { + log "Error. Variable does not exist." + return "" + } + return $::sweeper::stack($arg) +} + # Expand a string containing tokens proc ::sweeper::expand {ts str {orig ""}} { if {[string first "%" $str] == -1} { @@ -193,7 +202,14 @@ proc ::sweeper::expand {ts str {orig ""}} { "%emm" $emm \ ] -# log $map 2 + #log "STACK: $::sweeper::stack" 2 + + foreach {key val} $::sweeper::stack { + #log "MAP(%%$key) -> $val" 2 + set map(%%$key) $val + } + + #log $map 2 set ret [string map $map $ret] log " Expanded \[$str] -> \[$ret]" 2 @@ -512,13 +528,18 @@ proc ::sweeper::series {ts flag folder} { return 1 } +proc ::sweeper::varset {ts var folder} { + return [dict exists $::sweeper::stack $var] +} + proc ::sweeper::textmatch {ts str folder} { if {![regexp -- {^([^~]+)~~(.*)$} $str x target pattern]} { log "No pattern in textmatch." 1 return 0 } log "Textmatch ($target) against ($pattern)" 2 - return [::sweeper::strcontains [::sweeper::expand $ts $target] $pattern] + return [::sweeper::strcontains [::sweeper::expand $ts $target] \ + [::sweeper::expand $ts $pattern]] } proc ::sweeper::intmatch {ts str folder} { @@ -779,10 +800,24 @@ proc ::sweeper::action_log {ts cmd arg folder} { return 0 } +proc ::sweeper::action_set {ts cmd arg folder} { + set val [join [lassign [split $arg =] var] "="] + set val [::sweeper::expand $ts $val] + log "Set '$var'='$val'" + if {![string length $val]} { + unset -nocomplain $::sweeper::stack($var) + } else { + set ::sweeper::stack($var) $val + } + return 0 +} + proc ::sweeper::action_flag {ts cmd arg folder} { log "Flagged [$ts get file] as $arg" 0 if {!$::sweeper::dryrun} { $ts setflag $arg + } else { + $ts setflag $arg 1 } return 0 } @@ -791,6 +826,8 @@ proc ::sweeper::action_unflag {ts cmd arg folder} { log "Unflagged [$ts get file] as $arg" 0 if {!$::sweeper::dryrun} { $ts unsetflag $arg + } else { + $ts unsetflag $arg 1 } return 0 } @@ -934,6 +971,7 @@ proc ::sweeper::apply {dir cf} { continue } + set ::sweeper::stack {} foreach rule $rules { if {[string index $rule 0] eq "#" || [llength $rule] < 2} continue @@ -998,6 +1036,7 @@ proc ::sweeper::apply {dir cf} { continue } + set ::sweeper::stack {} foreach rule $rules { if {[string index $rule 0] eq "#" || [llength $rule] < 3} continue diff --git a/webif/plugin/sweeper/macro.js b/webif/plugin/sweeper/macro.js index 61ffa2a..94dd065 100644 --- a/webif/plugin/sweeper/macro.js +++ b/webif/plugin/sweeper/macro.js @@ -44,6 +44,155 @@ var macros = { } ] }, + dedup: { + desc: 'De-duplicate/tidy', + rules: [ + { + "raw": "!flag Deduped intmatch {%episode~~> 0} action {set {newname=s%format:%02d:%series:e%format:%02d:%episode: - %epname}}", + "name": "For a new recording with detected episode number, create a new title and store it in the 'newname' variable", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 1, + "cmd": "flag", + "arg": "Deduped" + }, + { + "negate": 0, + "cmd": "intmatch", + "arg": "%episode~~> 0" + } + ], + "action": { + "cmd": "set", + "arg": "newname=s%format:%02d:%series:e%format:%02d:%episode: - %epname" + } + }, + { + "raw": "lastrule "" action {set newfile=%asfilename/%%newname/}", + "name": "Set the new filename too", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 0, + "cmd": "lastrule", + "arg": "" + } + ], + "action": { + "cmd": "set", + "arg": "newfile=%asfilename/%%newname/" + } + }, + { + "raw": "lastrule "" textmatch %basename~~%%newfile action {flag dedup}", + "name": "If this recording already has this name, then flag as already de-duped.", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 0, + "cmd": "lastrule", + "arg": "" + }, + { + "negate": 0, + "cmd": "textmatch", + "arg": "%basename~~%%newfile" + } + ], + "action": { + "cmd": "flag", + "arg": "dedup" + } + }, + { + "raw": "!flag Deduped varset newfile fileexists %%newfile.ts action delete", + "name": "If still not de-duped and another file with the new name already exists, then delete this one.", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 1, + "cmd": "flag", + "arg": "Deduped" + }, + { + "negate": 0, + "cmd": "varset", + "arg": "newfile" + }, + { + "negate": 0, + "cmd": "fileexists", + "arg": "%%newfile.ts" + } + ], + "action": { + "cmd": "delete", + "arg": "" + } + }, + { + "raw": "!flag Deduped varset newname action {settitle %%newname}", + "name": "Otherwise set the recording title.", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 1, + "cmd": "flag", + "arg": "Deduped" + }, + { + "negate": 0, + "cmd": "varset", + "arg": "newname" + } + ], + "action": { + "cmd": "settitle", + "arg": "%%newname" + } + }, + { + "raw": "lastrule "" action {renamefile %%newname}", + "name": "and recording filename.", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 0, + "cmd": "lastrule", + "arg": "" + } + ], + "action": { + "cmd": "renamefile", + "arg": "%%newname" + } + }, + { + "raw": "lastrule "" action {flag dedup}", + "name": "Finally, flag recording as de-duped.", + "type": "file", + "enabled": "1", + "criteria": [ + { + "negate": 0, + "cmd": "lastrule", + "arg": "" + } + ], + "action": { + "cmd": "flag", + "arg": "dedup" + } + } +] + }, example: { desc: 'Example rules', rules: [ diff --git a/webif/plugin/sweeper/schema.js b/webif/plugin/sweeper/schema.js index 156f658..19acb12 100644 --- a/webif/plugin/sweeper/schema.js +++ b/webif/plugin/sweeper/schema.js @@ -216,6 +216,14 @@ var schema = { negate: true, def: "0" }, + varset: { + 'class': 'all', + type: 'string', + desc: 'Variable is set', + idesc: 'Variable is not set', + negate: true, + def: "" + }, 'or': { 'class': 'all', type: 'composite', @@ -337,6 +345,12 @@ var schema = { desc: 'Log...', continues: true }, + 'set': { + 'class': 'all', + argtype: 'string', + desc: 'Set variable...', + continues: true + } } };