-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.pl
74 lines (69 loc) · 1.95 KB
/
generate.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
#!/usr/bin/env perl
use 5.020;
use utf8;
use warnings;
use autodie;
use feature qw/signatures postderef/;
no warnings qw/experimental::postderef/;
use open qw(:std :utf8);
use Mojo::UserAgent;
use Data::Dumper;
use DDP;
$Data::Dumper::Sortkeys = 1;
my $ua = Mojo::UserAgent->new;
my $dom = $ua->get('http://hq.hust.edu.cn/ysfw/stfw.htm')->result->dom;
my @result = $dom
->at('.wznr')
->find('tr')
->map(sub ($dom) {
$dom->find('td')->[1]
}
)
->each(sub {
for my $dom ($_->find('span')->each) {
$dom->replace($dom->all_text);
}
for my $dom ($_->find('strong')->each) {
$dom->replace($dom->all_text);
}
}
)
->map(sub ($dom) {
[ $dom->find('p')->map('text')->each ]
}
)
->map(sub ($fragments) {
my $info = {
name => undef,
position => undef,
breakfast => undef,
lunch => undef,
dinner => undef,
contact => undef,
};
for (@$fragments) {
s/^\s+|\s+$//;
if (/^食堂地址[[:punct:]]?(.*)/) {
$info->{position} = $1;
next;
}
if (/(\d+[::]\d+)-+(\d+[::]\d+)/) {
my $times = { begin => $1, end => $2 };
$info->{breakfast} = $times if /早/;
$info->{lunch} = $times if /[中午]/;
$info->{dinner} = $times if /晚/;
next;
}
if (/(\d{3}-?\d{8})/) {
$info->{contact} = $1;
next;
}
s/^\d+、?//;
$info->{name} = $_;
}
$info
}
)
->each;
my $head = sprintf "#!/usr/bin/env perl\nmy \$data = eval(<<'EOF');\n%s\nEOF", Dumper \@result;
say $head;