Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

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 ( There is a simple API ( 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=" Wo +rld!&langpair=en|it"; my $req = HTTP::Request->new(GET => ' +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"; }

Replies are listed 'Best First'.
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}]}

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

      Thank you. This works great!

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.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://970179]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2018-06-18 07:55 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.