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