Skip to content

Instantly share code, notes, and snippets.

@myano
Created April 21, 2011 02:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myano/933540 to your computer and use it in GitHub Desktop.
Save myano/933540 to your computer and use it in GitHub Desktop.
web/dict.pm
use strict;
use warnings;
sophia_module_add('web.dict', '1.0', \&init_web_dict, \&deinit_web_dict);
sub init_web_dict {
sophia_command_add('web.dict', \&web_dict, 'Provides definitions of a given word.', '');
sophia_global_command_add('dict', \&web_dict, 'Provides definitions of a given word.', '');
return 1;
}
sub deinit_web_dict {
delete_sub 'init_web_dict';
delete_sub 'web_dict';
sophia_command_del 'web.dict';
delete_sub 'deinit_web_dict';
}
sub web_dict {
my $param = $_[0];
my @args = @{$param};
my ($where, $content) = @args[ARG1 .. ARG2];
my $idx = index $content, ' ';
$content = $idx > -1 ? substr($content, $idx + 1) : "";
my $word = $content;
my $objHTTP = get_file_contents(\sprintf('http://encarta.msn.com/dictionary_/%s.html', $word));
$objHTTP = ${$objHTTP};
sophia_write( \$where->[0], $word );
my $num_of_defs = 0;
my $numdef = 1;
my @defs;
while ( $num_of_defs < 2 )
{
my $findef = sprintf('<span class="ResultBodyBlack">%d.&nbsp;<b>', $numdef);
my $def_start = index($objHTTP, $findef) + 41;
my $def_end = index($objHTTP, ':&nbsp;</b></span>');
my $length = $def_end - $def_start;
my $def = substr($objHTTP, $def_start, $length);
if ($def) {
sophia_write( \$where->[0], $def );
push(@defs,$def);
#$num_of_defs += 1;
}
$num_of_defs += 1;
$numdef += 1;
}
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment