-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch-audio-mw
More file actions
executable file
·119 lines (90 loc) · 2.42 KB
/
Copy pathfetch-audio-mw
File metadata and controls
executable file
·119 lines (90 loc) · 2.42 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#! /usr/bin/perl
use warnings;
use strict;
use HTML::Parser;
use LWP::UserAgent;
use URI::URL;
my $URL = "http://www.merriam-webster.com/dictionary";
my $POPWIN = qr/
popWin
\s* \( \s*
["'] (.+?) ["']
\s* \)
/ix;
my @words = qw(
a all an and are as at be but by
can do each for from had have he his how
I if in is it not of on one or
said she that the their there they this to use
was we were what when which with words you your
);
$| = 1;
my %audio;
my $ua = LWP::UserAgent->new;
use Data::Dumper;
$Data::Dumper::Indent = 1;
my $p = HTML::Parser->new(api_version => 3);
WORD:
foreach my $word (qw/ you your /) { # (@words) {
my $audio;
my @lookup;
my $store;
my @todo = ("$URL/$word");
my $onclick = sub {
my($tag,$attr,$p) = @_;
return unless $tag eq "a" && ($attr->{class} || "") =~ /lookup|audio/;
if ($attr->{class} && $attr->{class} eq "lookup") {
push @lookup => $attr->{href};
}
elsif ($attr->{onclick} && $attr->{onclick} =~ /$POPWIN/) {
if (index($1, lc "=$word") == length($1) - length($word) - 1) {
$audio = $1;
$p->eof;
}
}
};
my $embed = sub {
my($tag,$attr,$p) = @_;
return unless $tag eq "embed" && $attr->{src};
$store = $attr->{src};
$p->eof;
};
$p->eof;
$p->handler(start => $onclick, "tagname,attr,self");
while (@todo) {
my $url = shift @todo;
print "$0: fetching $url...\n";
my $res = $ua->request(
HTTP::Request->new(GET => $url),
sub { $p->parse($_[0]) }
);
unless ($res->is_success) {
warn "$0: word '$word':\n" . $res->content;
next;
}
my $base = $res->base;
if (defined $store) {
my $path = "words/$word";
$path .= ".$1" if $store =~ /^.*\.(.+)$/;
print "$0: storing $path...\n";
my $abs = absurl($store, $base);
my $res = $ua->request(HTTP::Request->new(GET => $abs), $path);
warn "$0: word '$word':\n" . $res->content
unless $res->is_success;
next WORD;
}
elsif (defined $audio) {
push @todo => absurl($audio, $base);
$p->handler(start => $embed, "tagname,attr,self");
next;
}
else {
push @todo => map absurl($_,$base) => @lookup;
}
}
}
exit;
sub absurl {
my($url,$base) = @_;
url($url,$base)->abs;
}