Improve DDR Europe parsing; we now understand groups and rounds
[ccbs] / parse / parse-ddreurope-tournament.pl
1 #! /usr/bin/perl
2 use strict;
3 use warnings;
4 use Date::Manip;
5
6 # Parses tournament results from the DDR Europe result list
7
8 my $season = shift;
9 my ($name, $round, @rsongs);
10 die "Missing season (give on command line)." if (!defined($season));
11
12 print "begin;\n";
13
14 while (<>) {
15         if (/<h2> \s* (.*?) \s* <\/h2>/x) {
16                 $name = $1;
17                 next;
18         }
19         if (/<br>Country: \s* (.*?) \s*
20              <br>Location: \s* (.*?) \s*
21              <br>Date: \s* (.*?) \s*
22              <br>Mix: \s* (.*?) \s*
23              <br>ScoringSystem: \s* (.*?) \s* <br>
24             /x) {
25                 my ($country, $location, $date, $mix, $system) = ($1, $2, $3, $4, $5);
26                 $mix =~ s/Euromix/EuroMix/;
27
28                 print "INSERT INTO tournaments \n";
29                 print "  (season, name, country, location, \"date\", machine, scoringsystem) VALUES (\n";
30                 print "    (SELECT season FROM seasons WHERE name='$season'),\n";
31                 print "    '$name',\n";
32                 print "    (SELECT country FROM countries WHERE name='$country'),\n";
33                 print "    '$location',\n";
34                 printf "    '%s',\n", Date::Manip::UnixDate($date, '%Y-%m-%d');
35                 print "    (SELECT machine FROM machines WHERE name='$mix'),\n";
36                 print "    (SELECT scoringsystem FROM scoringsystems WHERE name='$system')\n";
37                 print ");\n";
38         }
39
40         # New round
41         if (/<h3 \s* class=dthr> Round \s* (\d+) \s* <\/h3>/x) {
42                 $round = $1;
43
44                 print "INSERT INTO rounds (tournament, round, randomsongs, chosensongs) \n";
45                 print "  VALUES (\n";
46                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
47                 print "   $round,\n";
48                 print "   0, 0);\n";  # Don't worry, we'll fix it later :-P
49         }
50         
51         # New group, and the random songs for it
52         if (/<th \s* class=dthp> ( Players | Group (\d+) )<\/th>/x) {
53                 # bit of an evil hack here
54                 my $group = $2;
55                 $group = 0 if ($1 eq 'Players');
56
57                 print "INSERT INTO groups (tournament, round, parallel) VALUES (\n";
58                 print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
59                 print "   $round, $group);\n";
60
61                 # Find the random songs, if any
62                 @rsongs = ();
63
64                 while (s/<th \s* class=dthpsong> .*? "color:white"> (.*?) <\/a><\/th>//x) {
65                         push @rsongs, $1;
66
67                         print "INSERT INTO roundrandomsongs (tournament, round, parallel, song) VALUES (\n";
68                         print "   (SELECT tournament FROM tournaments WHERE name='$name'),\n";
69                         print "   $round, $group,\n";
70                         print "   (SELECT song FROM songs WHERE title='$1')\n";
71                         print ");\n";
72                 }
73
74                 # Correct the random songs in the table
75                 printf "UPDATE rounds SET randomsongs=%u WHERE \n", scalar @rsongs;
76                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
77                 print "  round=$round;\n";
78         }
79
80         # Header for chosen song
81         if (/<th \s* class=dthp \s* colspan=2>&nbsp<\/th>/x) {
82                 printf "UPDATE rounds SET chosensongs=chosensongs+1 WHERE \n";
83                 print "  tournament=(SELECT tournament FROM tournaments WHERE name='$name') AND \n";
84                 print "  round=$round;\n";
85         }
86 }
87
88 printf "commit;\n";