]> git.donarmstrong.com Git - infobot.git/blob - scripts/factpack.pl
* Rename of insertDB.pl to factpack.pl
[infobot.git] / scripts / factpack.pl
1 #!/usr/bin/perl -w
2
3 $| = 1;
4
5 use strict;
6 use vars qw($bot_base_dir $bot_src_dir $bot_misc_dir $bot_state_dir
7   $bot_data_dir $bot_config_dir $bot_log_dir $bot_run_dir
8     $bot_pid $memusage %param
9 );
10
11
12 # Check for arguments
13 if ( !scalar @ARGV ) {
14     print "Usage: $0 <pack1.fact> [<pack2.fact> <pack2.fact> ...]\n";
15     print "Example: $0 areacodes.fact usazips.fact\n";
16     exit 1;
17 }
18
19
20 # set any $bot_*_dir var's
21 $bot_base_dir   = '.';
22 $bot_config_dir = 'files/';
23 $bot_data_dir   = 'files/';
24 $bot_state_dir  = 'files/';
25 $bot_run_dir    = '.';
26 $bot_src_dir    = "$bot_base_dir/src";
27 $bot_log_dir    = "$bot_base_dir/log";
28 $bot_misc_dir   = "$bot_base_dir/files";
29 $bot_pid        = $$;
30
31 require "$bot_src_dir/logger.pl";
32 require "$bot_src_dir/core.pl";
33 require "$bot_src_dir/modules.pl";
34
35 # Initialize enough to get DB access
36 &setupConfig();
37 &loadCoreModules();
38 &loadDBModules();
39 &loadFactoidsModules();
40 &setup();
41
42 if ( !scalar @ARGV ) {
43     print "Usage: $0 <pack1.fact> [<pack2.fact> <pack2.fact> ...]\n";
44     print "Example: $0 areacodes.fact usazips.fact\n";
45     exit 0;
46 }
47
48 foreach (@ARGV) {
49     next unless ( -f $_ );
50     my $file = $_;
51
52     open( IN, $file ) or die "error: cannot open $file\n";
53     print "Opened $file for input...\n";
54
55     print "inserting... ";
56     while (<IN>) {
57         chomp;
58         next if /^#/;
59         next unless (/=>/);
60
61         # Split into "key => value" pairs
62         my ($key, $value) = split(/=>/,$_,2);
63
64         # Strip extra begin/end whitespace
65         $key =~ s/^\s*(.*?)\s*$/$1/;
66         $value =~ s/^\s*(.*?)\s*$/$1/;
67         
68         # convert tabs
69         $key =~ s/\t/ /g;
70         $value =~ s/\t/ /g;
71
72         ### TODO: check if it already exists. if so, don't add.
73         ### TODO: combine 2 setFactInfo's into single
74         &setFactInfo( $key, "factoid_value", $value );
75         &setFactInfo( $key, "created_by", $file );
76         print ":: $key ";
77     }
78
79     close IN;
80 }
81 print "...Done!\n";
82
83 # vim:ts=4:sw=4:expandtab:tw=80