Skip to content

Instantly share code, notes, and snippets.

@myano
Created April 21, 2011 03:56
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/933688 to your computer and use it in GitHub Desktop.
Save myano/933688 to your computer and use it in GitHub Desktop.
use warnings;
use JSON;
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 $json_url = \sprintf('http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=cat&sl=en&tl=en', $word);
my $json_text = get_file_contents(\sprintf('http://www.google.com/dictionary/json?callback=dict_api.callbacks.id100&q=cat&sl=en&tl=en', $word));
$json_text = ${$json_text};
$json_text =~ s/dict_api\.callbacks\.id100\(//;
$json_text =~ s/\\x3cem\\x3ecat\\x3c\/em\\x3e//g;
$json_text =~ s/\,200\,null\)//;
my $json = new JSON;
my $newcontent = $json->allow_nonref->utf8->relaxed->decode($json_text);
my $episode_num = 1;
my %ep_hash = ();
foreach my $episode(@{$json_text->{primaries}->{entries}->{terms}->{text}}){
sophia_log('sophia', $episode);
}
sophia_log('sophia', "I made it" );
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment