-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patholdParseFeedback.pl
86 lines (72 loc) · 2.04 KB
/
oldParseFeedback.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/perl -w
use strict;
use warnings;
if (scalar(@ARGV) != 1) {
print "Usage: parseFeedback.pl StateFolder\n";
exit;
}
my $stateFolder = $ARGV[0];
open(OUTFILE, ">$stateFolder/data/ratingsFeedback.txt");
print OUTFILE "RatingID\tSubmitted by\tDate\tTime\tContent\n";
opendir(DIR, "$stateFolder/ratings/feedback");
my @allFiles = grep(/.htm/, readdir(DIR));
closedir(DIR);
print "Starting to parse feedback\n";
our $filename;
foreach $filename (@allFiles) {
open(INPUT, "<$stateFolder/ratings/feedback/$filename");
my @lines = <INPUT>;
close(INPUT);
my $ratingID;
if ($filename =~ m/feedback(\d+)/) {
$ratingID = $1;
} else { generateError("Could not find ratingID in filename"); }
my $i;
for ($i = 0; $i < scalar(@lines); $i++) {
if ($lines[$i] =~ m/Submitted by \<a[^>]+\>([^<]+)\<\/a\>[^,]+, (\d+\/\d+\/\d+) - (\d+:\d+)/) {
my ($submittedBy, $date, $time, $content);
$submittedBy = $1;
$date = $2;
$time = $3;
$i += 1;
if ($lines[$i] =~ m/content"\>(.*)/) {
$content = $1;
} else {
$i += 2;
if ($lines[$i] =~ m/content"\>(.*)/) {
$content = $1;
} else { generateError("Could not find content in $lines[$i]"); }
}
while ($lines[$i] !~ m/^\s*\<\/div\>\s*$/) {
$content .= $lines[$i];
$i++;
}
$i++;
print OUTFILE tabSeparate($ratingID, $submittedBy, $date, $time, $content);
}
}
}
print "Done parsing feedback\n";
# Generate clean tab separated output, without tabs inside of elements.
sub tabSeparate
{
my $i;
my $output = "";
my $tmp;
for ($i = 0; $i < scalar(@_) - 1; $i++) {
$tmp = $_[$i];
if (!defined($tmp)) { generateError("Got undefined value $output !"); }
$tmp =~ s/\s+/ /g;
$output = $output . $tmp . "\t";
}
$tmp = $_[$i];
$tmp =~ s/\s+/ /g;
$output = $output . $tmp . "\n";
return $output;
}
sub generateError
{
print "Error in processing $filename: $_[0]";
close(OUTFILE);
exit(3);
}