#!/usr/bin/perl # Yen-Ming Lee # 2010/06/01 # # ---------------------------------------------------------------------------- # "THE PEARL-TEA-WARE LICENSE", based on "THE BEER-WARE LICENSE": # wrote this file. As long as you retain this notice you # can do whatever you want with this stuff. If we meet some day, and you think # this stuff is worth it, you can buy me a pearl tea in return. Yen-Ming Lee # ---------------------------------------------------------------------------- use FindBin qw($Script); use Term::ANSIColor qw(:constants); use LWP::Simple; use URI::Escape; use Data::Dumper; use Getopt::Std; use strict; use utf8; binmode(STDOUT, ':encoding(utf8)'); my %opt; getopts('vd', \%opt); my $word = shift; die "usage: $Script word\n" if !$word; my $red = RED; my $bold = BOLD; my $cyan = CYAN; my $reset = RESET; my $green = GREEN; my $yellow = YELLOW; my %URL; my $base = 'http://tw.dictionary.search.yahoo.com'; ydict("$base/search?fr=ydict&p=" . uri_escape($word)); sub ydict { my $url = shift; warn "GET $url\n" if $opt{d}; my $html = get($url); $html =~ s{\r}{}g; $html =~ s{\n}{ }g; my @URL = grep { !m{(tab|\*)} } ($html =~ m{(http[^"]+docid=[^"]+)}sg); if ($html =~ m{(\w+)}) { my ($url2, $correction) = ($1, $2); print "$bold$yellow\nCorrection: $correction$reset\n"; return ydict("$base$url2"); } if ($html =~ m{isZrp: true}) { print "Not found\n"; exit; } my $pronunciation = $1 if $html =~ m{
(.*?)
}; $pronunciation =~ s{]+>}{}g; print "\n${bold}{$yellow}Pronunciation: $pronunciation${reset}\n" if $pronunciation; my $indent = " "; $html =~ s{.*?
  • }{}; # behead $html =~ s{.*}{}; # curtail $html =~ s{
  • .*}{}; # curtail $html =~ s{
      .*}{}; # curtail $html =~ s{}{$bold}g; $html =~ s{}{ }g; $html =~ s{}{$reset$cyan}g; $html =~ s{]*>}{\n$indent $cyan}g; $html =~ s{

      }{$reset\n * $green}g; $html =~ s{

      }{$reset\n$bold$red}g; $html =~ s{

      }{$reset}g; $html =~ s{'}{'}g; $html =~ s{\\}{}g; # bug? $html =~ s{]*>}{}g; # all tags print "$html\n"; #shift @URL; #map { ydict($_) unless $URL{$_}++ } (@URL); }