~aleteoryx/bundlemania

ref: 2a09b9a3e081e43c7aedbb2692ae65df9da82305 bundlemania/s/import -rwxr-xr-x 737 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
#!/bin/env tclsh

package require json
package require sqlite3

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

proc usage {} {
  global argv0
  puts stderr "usage: $argv0 DB-PATH DATAFILE ?DATAFILE ...?"
  exit -1
}
if {[llength $argv] < 2} usage
set datafiles [lassign $argv dbpath]

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

foreach datafile $datafiles {
  set fd [open $datafile]
  set data [::json::json2dict [read $fd]]
  close $fd

  foreach title $data {
    set name [dict get $title title]
    set href [dict get $title href]
    puts $name
    db eval {
      INSERT OR IGNORE INTO titles(name, href)
        VALUES($name, $href);
    }
  }
}