webif/var/mongoose/cgi-bin/dedup.jim
hummypkg fa9905d8bd replace glob with readdir in some cases. Move paste button to clipboard
git-svn-id: file:///root/webif/svn/humax/pkg/src/webif/trunk@711 2a923420-c742-0410-a762-8d5b09965624
2012-02-01 21:02:43 +00:00

123 lines
2.4 KiB
Plaintext
Executable File

#!/mod/bin/jimsh
package require cgi
source /mod/var/mongoose/lib/setup
require ts.class pretty_size altrow
puts "Content-Type: text/html"
puts ""
cgi_input
#cgi_dump
set dir [cgi_get dir "/media/My Video"]
set doit [cgi_get doit 0]
header
puts "<fieldset style=\"display: inline\">
<legend>De-duplicate <span id=dir>$dir</span></legend>
<table class=borders>
<tr>
<th>File</th>
<th>Proposed Filename</th>
<th>Proposed Title</th>
<th>Status</th>
</tr>
"
set seen {}
foreach file [readdir $dir] {
set file "$dir/$file"
if {[file extension $file] ne ".hmt"} { continue }
altrow
set ts [ts fetch $file 1]
set base [file tail [file rootname $file]]
puts "<td nowrap>$base</td>"
set syn [$ts get synopsis]
regsub -nocase -all -- {^new series\.* *} $syn "" syn
regsub -nocase -all -- {^cbeebies\. *} $syn "" syn
regsub -all -- { *[:].*$} $syn "" syn
if {[string length $syn] > 40} {
lassign [split $syn "."] v w
set syn $v
if {[string length $syn] < 6 && [string length $w] < 6} {
append syn "_$w"
}
}
if {[string length $syn] < 10} {
if {[string match "${syn}*" [$ts get title]]} {
set syn [$ts get title]
} else {
append syn " [$ts get title]"
}
}
regsub -all -- {[\/ &]} $syn "_" fn
puts "<td>$fn</td>"
puts "<td>$syn</td>"
puts "<td>"
if {$fn in $seen} {
puts -nonewline "Duplicate"
if {$doit} {
set dupdir "$dir/_duplicates"
if {![file exists $dupdir]} { file mkdir $dupdir }
while {[file exists $dupdir/$fn.hmt]} {
append fn "~"
}
ts renamegroup $file "_duplicates/$fn"
puts -nonewline " - Renamed to _duplicates/$fn"
}
} elseif {[string length $syn] > 40} {
puts -nonewline "Cannot process"
} elseif {$base eq $fn} {
puts -nonewline "Nothing to do"
} elseif {[file exists "$dir/$fn.ts"]} {
puts -nonewline "Preserving"
} elseif {$doit} {
# Dooooo, it.
$ts settitle $syn
ts renamegroup $file $fn
puts -nonewline "Done"
}
lappend seen $fn
puts "</td>"
puts "</tr>"
}
puts {
</table>
</fieldset>
<div style="padding-top: 2em">
<small>
<button id=browse>Back to media browser</button>
}
if {!$doit} { puts "<button id=dedup>Process folder</button>" }
puts {
</small>
</div>
<script type=text/javascript>
$('#browse').button().click(function() {
window.location = '/cgi-bin/browse.jim?dir=' +
encodeURIComponent($('#dir').text());
});
}
if {!$doit} {
puts {
$('#dedup').button().click(function() {
window.location = window.location + '&doit=1';
});
}
}
puts "</script>"
footer