4e15fcce0105ed51ecd95ad0e22cb84c9decbd35
[MojoFacets.git] / script / GoogleDocsUploader.pl
1 #!/usr/bin/perl
2
3 # Upload documents to Google Documents.
4
5 # Copyright 2010 Alessandro Ghedini <al3xbio@gmail.com>
6 # --------------------------------------------------------------
7 # "THE BEER-WARE LICENSE" (Revision 42):
8 # Alessandro Ghedini wrote this file. As long as you retain this 
9 # notice you can do whatever you want with this stuff. If we 
10 # meet some day, and you think this stuff is worth it, you can 
11 # buy me a beer in return.
12 # --------------------------------------------------------------
13
14 use HTTP::Request::Common;
15 use LWP::UserAgent;
16 use JSON -support_by_pp;
17 use Media::Type::Simple;
18
19 use strict;
20
21 die "For info type 'perldoc $0'\n" unless $#ARGV > 0;
22
23 my (@files, $email, $pwd);
24
25 for (my $i = 0; $i < $#ARGV + 1; $i++) {
26         push(@files, $ARGV[$i+1]) if ($ARGV[$i] eq "-f");
27         $email  = $ARGV[$i+1] if ($ARGV[$i] eq "-e");
28         die "For info type 'perldoc $0'\n" if ($ARGV[$i] eq "-h");
29 }
30
31 print("Password: ");
32 system('stty','-echo') if $^O eq 'linux';
33 chop($pwd = <STDIN>);
34 system('stty','echo') if $^O eq 'linux';
35 print "\n";
36
37 my $ua = LWP::UserAgent -> new;
38 my $url = 'https://www.google.com/accounts/ClientLogin';
39
40 my %request = ('accountType', 'HOSTED_OR_GOOGLE',
41                'Email', $email,
42                'Passwd', $pwd,
43                'service', 'writely',
44                'source', 'GoogleDocsUploader-GoogleDocsUploader-00',
45               );
46
47 my $response = $ua -> request(POST $url, [%request]) -> as_string;
48 my $auth = (split /=/, (split /\n/, (split /\n\n/, $response)[1])[2])[1];
49
50 my $status = (split / /,(split /\n/, $response)[0])[1];
51 die("ERROR: Unauthorized.\n") if $status == 403;
52
53 $url = "https://docs.google.com/feeds/documents/private/full?alt=json";
54
55 $ua -> default_header('Authorization' => "GoogleLogin auth=$auth");
56
57 foreach my $file(@files) {
58
59         if (!open(FILE, $file)) {
60                 print "ERROR: Unable to open '$file' file.\n";
61                 next;
62         }
63         
64         my $data = join("", <FILE>);
65         close FILE;
66
67         my $mime = type_from_ext(($file =~ m/([^.]+)$/)[0]);
68         
69         $ua -> default_header('Slug' => $file);
70
71         my $request = HTTP::Request -> new(POST => $url);
72         $request -> content_type($mime);
73         $request -> content($data);
74
75         my $response = $ua -> request($request) -> as_string;
76
77         $status = (split / /,(split /\n/, $response)[0])[1];
78         my $body = (split /\n\n/, $response)[1];
79
80         if ($status != 201) {
81                 print "ERROR: $body";
82                 next;
83         }
84
85         my $json = new JSON;
86
87         my $json_text = $json -> decode($body);
88
89         my $title = $json_text -> {entry} -> {title} -> {'$t'};
90         my $link = $json_text -> {entry} -> {link}[0] -> {href};
91
92         print "Document successfully created with title '$title'.\nLink:\n$link\n";
93
94 }
95
96 __END__
97
98 =head1 NAME
99
100 GoogleDocsUploader.pl - Uploads documents to Google Documents.
101
102 =head1 USAGE
103
104 GoogleDocsUploader [OPTIONS]
105
106 =head1 OPTIONS
107
108 =over
109                 
110 =item -e        Specifies the login email (e.g. example@gmail.com).
111
112 =item -f        Specifies the file to upload (can be more than one).
113
114 =back
115
116 =head1 MULTIPLE FILES UPLOAD
117
118 You can upload multiple files by setting multiple '-f' options.
119
120 =head1 FILE TYPE
121
122 Allowed file types (checked with MIME) are:
123
124         CSV     text/csv
125         TSV     text/tab-separated-values
126         TAB     text/tab-separated-values
127         HTML    text/html
128         HTM     text/html
129         DOC     application/msword
130         DOCX    application/vnd.openxmlformats-officedocument.
131                                         wordprocessingml.document
132         ODS     application/x-vnd.oasis.opendocument.spreadsheet
133         ODT     application/vnd.oasis.opendocument.text
134         RTF     application/rtf
135         SXW     application/vnd.sun.xml.writer
136         TXT     text/plain
137         XLS     application/vnd.ms-excel
138         XLSX    application/vnd.openxmlformats-officedocument.
139                                                 spreadsheetml.sheet
140         PDF     application/pdf
141         PPT     application/vnd.ms-powerpoint
142         PPS     application/vnd.ms-powerpoint
143
144 =cut
145
146