summaryrefslogtreecommitdiff
path: root/utils/fetch-transifex.pl
blob: 4d40062c9caa75021e7a0f7ab0000fb6056cb483 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/perl
#
# Copyright © 2013 Vincent Sanders <vince@netsurf-browser.org>
#
# 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|? );

# ensure no locale translation is applied and leave it all in UTF-8
use bytes;

# 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 <<TXT );
usage:
     $0 -l lang-code \
           [-o output-file] [-r resource] [-p project] [-u user] [-w password] 

     lang-code  : en fr ko ...  (no default)
     project    : transifex project (default 'netsurf')
     resource   : transifex resource (default 'messagesany')
     user       : transifex resource (default 'netsurf')
     password   : transifex resource (no default)
     output-file: defaults to standard output
TXT
    exit(1);
}

sub output_stream ()
{
    if( $opt{output} )
    {
        my $ofh;

        sysopen( $ofh, $opt{output}, O_CREAT|O_EXCL|O_APPEND|O_WRONLY ) ||
          die( "$0: Failed to open output file $opt{output}: $!\n" );

        return $ofh;
    }

    return \*STDOUT;
}