view convert_spede_mn_tuksu.tcl @ 178:9b8ec700ede4

Clean up the weather data parser backend a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 03 Jun 2014 15:10:47 +0300
parents 7b03971c6d28
children
line wrap: on
line source

#!/usr/bin/tclsh
# TCL script for converting Spede, MN and Tuksu databases from
# flatfile format to SQLite3 database.
#
# Written by Matti 'ccr' Hamalainen <ccr@tnsp.org>
# (C) Copyright 2011 Tecnic Software productions (TNSP)
#
package require sqlite3
source [file dirname [info script]]/util_convert.tcl

### Check commandline arguments
if {$argc != 3} {
  puts "Usage: $argv0 <input_flat_file_db> <output_sqlite_db_file> <table_name>"
  exit 0
}

set db_input [lindex $argv 0]
set db_output [lindex $argv 1]
set db_table [lindex $argv 2]

### Ask for confirmation
puts "Conversion of '$db_input' to SQLite database '$db_output', to table '$db_table'."
puts "NOTICE! This WILL destroy the current data in table '$db_table'!"

if {![confirm_yesno "Proceed"]} {
  exit 0
}

### Open flatfile for reading
if {[catch {set fd [open $db_input r]} uerrmsg]} {
  puts "Could not open file '$db_input' for reading: $uerrmsg"
  exit 1
}

### Open SQLite database, drop old tables, create new
open_db $db_output
drop_table $db_table
create_table_quotes_votes $db_table 1

### Convert data
puts -nonewline "Converting database, please wait "
set nline 0
seek $fd 0 start
while {![eof $fd]} {
  incr nline
  set line [gets $fd]
  if {$line != ""} {
    set items [split $line "|"]
    set host [lindex $items 3]
    set uid [lindex $items 4]
    set sql "INSERT INTO $db_table (id,utime,utext,user,host,chan) VALUES ([lindex $items 0], [lindex $items 2], '[lescape $items 1]', '[lescape $items 3]', '[lescape $items 4]', '[lescape $items 5]')"
    if {[catch {dbh eval $sql} uerrmsg]} {
      puts "\nError ($nline): $uerrmsg on:\n$sql"
      exit 15
    }
  }
  if {[expr $nline % 10] == 1} {
    puts -nonewline "."
    flush stdout
  }
}

dbh close
close $fd

puts "DONE."