~aleteoryx/bundlemania

ref: dcee34571bbfda679496cbe3e07eb08e1b561b7e bundlemania/pick.tcl -rwxr-xr-x 769 bytes
dcee3457Aleteoryx comment 9 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#!/bin/env tclsh

package require sqlite3
package require tcl::chan::random
package require tcl::randomseed

proc fatal {reason {code -1}} {
  puts stderr "$argv0: fatal: $reason"
  exit $code
}

proc usage {} {
  global argv0
  puts stderr "usage: $argv0 DB-PATH"
  exit -1
}
if {[llength $argv] != 1} usage
set dbpath [lindex $argv 0]

if {[catch {sqlite3 db $dbpath -create false}]} {
  fatal "can't open \"$dbpath\""
}

set unplayed [db eval {SELECT name,href FROM titles WHERE played ISNULL AND ignore = 0}]

set chan [::tcl::chan::random [::tcl::randomseed]]
binary scan [read $chan 4] i1 n
set idx [expr { (abs($n) % ([llength $unplayed] / 2)) * 2 }]

set name [lindex $unplayed $idx]
set href [lindex $unplayed $idx+1]

puts "Random pick: $name"
puts "- $href"