Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

MyMemory API

by welle (Beadle)
on May 12, 2012 at 12:30 UTC ( #970179=perlquestion: print w/ replies, xml ) Need Help??
welle has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks

I am trying to access translations provided by MyMemory (http://mymemory.translated.net/). There is a simple API (http://mymemory.translated.net/doc/spec.php). I am not an expert in doing such tasks... I just thought to access the data using LWP::UserAgent, pars it and print the results. However I get an error (unauthorized) even if the service is (now) free and accessible without any sort of registration (maybe of interest for other monks?). Am I doing programmatically something wrong in the following code? If what I am doing is okay, I would certainly contact MyMemory.

use LWP::UserAgent; use strict; my $ua = LWP::UserAgent->new; #my $mymemory_url="http://mymemory.translated.net/api/get?q=Hello Wo +rld!&langpair=en|it"; my $req = HTTP::Request->new(GET => 'http://mymemory.translated.net/ +api/get?q=Hello World!&langpair=en|it'); my $res = $ua->request($req); if ($res->is_success) { my $translation= $res->content; print "Mymemory: $translation\n"; } else { print "Impossible retrieving from MyMemory\n"; }

Comment on MyMemory API
Download Code
Re: MyMemory API
by Old_Gray_Bear (Bishop) on May 12, 2012 at 13:04 UTC
    Reading a little further down in the spec, there are two parameters 'user' and 'key'. User is listed as 'optional' and 'key' is "mandatory only if 'user' has been specified". The spec links to their keygen page, so give it a try.

    I suspect that the user-id is used under the covers and isn't truly optional. Otherwise my updates would step all over your data as soon as there is more than one user.

    ----
    I Go Back to Sleep, Now.

    OGB

Re: MyMemory API
by sauoq (Abbot) on May 12, 2012 at 14:55 UTC
    Am I doing programmatically something wrong in the following code?

    No, you aren't.

    The problem seems to be that they don't like user agents that identify themselves as "libwww-perl".

    Set your user agent to something else and try it...

    my $ua = LWP::UserAgent->new( agent => "something else" );
    ... worked for me. I got the unauthorized message before the change and afterward the response I got was:
    Mymemory: {"responseData":{"translatedText":"Ciao mondo"},"responseDet +ails":"","responseStatus":200,"matches":[{"id":"424913311","segment": +"Hello World","translation":"Ciao mondo","quality":"74","reference":" +","usage-count":50,"subject":"All","created-by":"","last-updated-by": +null,"create-date":"2011-12-29 19:14:22","last-update-date":"2011-12- +29 19:14:22","match":1},{"id":"0","segment":"Hello World","translatio +n":"Ciao a tutti","quality":"70","reference":"Machine Translation pro +vided by Google, Microsoft, Worldlingo or the MyMemory customized eng +ine.","usage-count":1,"subject":"All","created-by":"MT!","last-update +d-by":null,"create-date":"2012-05-12","last-update-date":"2012-05-12" +,"match":0.85}]}

    -sauoq
    "My two cents aren't worth a dime.";

      Thank you. This works great!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970179]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2014-08-30 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls