From 04c486ab545aa59d6cca0826c12e3bea9cfeeab1 Mon Sep 17 00:00:00 2001 From: Vincent Sanders Date: Mon, 6 May 2013 14:31:56 +0100 Subject: perl script to fetch translated resources from transifex --- utils/fetch-transifex.pl | 124 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 utils/fetch-transifex.pl (limited to 'utils/fetch-transifex.pl') diff --git a/utils/fetch-transifex.pl b/utils/fetch-transifex.pl new file mode 100644 index 000000000..d8d588285 --- /dev/null +++ b/utils/fetch-transifex.pl @@ -0,0 +1,124 @@ +#!/usr/bin/perl +# +# Copyright © 2013 Vincent Sanders +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# * The above copyright notice and this permission notice shall be included in +# all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +# THE SOFTWARE. + +=head1 + +retrive resource from transifex service + +=cut + +use strict; +use Getopt::Long (); +use LWP::UserAgent; +use JSON qw( decode_json ); +use Data::Dumper; +use Fcntl qw( O_CREAT O_EXCL O_WRONLY O_APPEND O_RDONLY O_WRONLY ); + +use constant GETOPT_OPTS => qw( auto_abbrev no_getopt_compat bundling ); +use constant GETOPT_SPEC => + qw( output|o=s + lang|l=s + resource|res|r=s + project|prj|p=s + user|u=s + password|w=s + help|h|? ); + +# default option values: +my %opt = qw( resource messagesany project netsurf user netsurf ); + +sub output_stream (); +sub usage (); + +sub main () +{ + my $output; + my $opt_ok; + + # option parsing: + Getopt::Long::Configure( GETOPT_OPTS ); + $opt_ok = Getopt::Long::GetOptions( \%opt, GETOPT_SPEC ); + + if( $opt_ok ) + { + $output = output_stream(); + } + + # double check the options are sane (and we weren't asked for the help) + if( !$opt_ok || $opt{help} || $opt{lang} !~ /^[a-z]{2}$/ ) + { + usage(); + } + + my $transifexurl = "https://www.transifex.com/api/2/project/" . $opt{project} . "/resource/" . $opt{resource} . "/translation/" . $opt{lang} . "/"; + + my $ua = LWP::UserAgent->new; + $ua->credentials( + 'www.transifex.com:443', + 'Transifex API', + $opt{user} => $opt{password} + ); + + my $response = $ua->get( $transifexurl ); + if (!$response->is_success) { + die $response->status_line . " When fetching " . $transifexurl; + } + + # Decode the entire JSON + my $decoded_json = decode_json( $response->decoded_content ); + + print ( $output $decoded_json->{'content'} ); +} + +main(); + +sub usage () +{ + print(STDERR <