~aleteoryx/bundlemania

bundlemania/s/pick -rwxr-xr-x 874 bytes
2a09b9a3Aleteoryx comment in s/pick 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
35
36
37
38
#!/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,comment 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] / 3)) * 3 }]

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

puts "Random pick: $name"
puts "- link: $href"
if {$comment != ""} {
  puts "- comment: $comment"
}