-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.pl
61 lines (45 loc) · 1.5 KB
/
events.pl
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/perl -w
# Event ID,Class ID,Calendar Date,Start Time,End Time,Day,Block,Room
my @inputFiles = ('ClassCalSync_Events.csv');
# Database
$DBSVR = 'localhost';
$DBNAME = 'classCalSync';
$DBUSER = '**username**';
$DBPASS = '**password**';
$DBTABLE = 'tmpEvents';
use Digest::MD5 qw(md5_base64);
use HTML::Entities qw(decode_entities);
use utf8;
use Text::CSV;
my $csvIn = Text::CSV_XS->new ( { sep_char => ',' } );
#Prepare use of SQL database
use DBI;
my $dsn = "DBI:mysql:$DBNAME:$DBSVR";
my $dbh = DBI->connect($dsn, $DBUSER, $DBPASS) or die ("Cannot connect");
$dbh->do("TRUNCATE TABLE $DBTABLE");
#Insert data into database
$insert = $dbh->prepare("INSERT into $DBTABLE
(eventID, classID, eventDate, start, end, description, room)
values (?, ?, ?, ?, ?, ?, ?)");
foreach $inputFile (@inputFiles)
{
open( my $readRows, '<:encoding(utf-8)', $inputFile)
or die "Could not open $inputFile: $!\n";
while (my $line = <$readRows>)
{
chomp $line;
if ($csvIn->parse($line))
{
my ($eventID, $classID, $eventDate, $start, $end, $day, $block, $room)
= $csvIn->fields();
next if ($classID =~ m/Class ID/);
my ($description) = "Day: $day";
if ($block !~ m/None Specified/) { $description .= ", Block: $block" }
$eventHash = md5_base64($classID . $eventDate . $start . $end);
$insert->execute($eventHash, $classID, $eventDate, $start, $end, $description, $room);
}
else { warn "Line could not be parsed: $line\n"; }
}
}
$insert->finish();
exit;