3 # Upload documents to Google Documents.
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 # --------------------------------------------------------------
14 use HTTP::Request::Common;
16 use JSON -support_by_pp;
17 use Media::Type::Simple;
21 die "For info type 'perldoc $0'\n" unless $#ARGV > 0;
23 my (@files, $email, $pwd);
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");
32 system('stty','-echo') if $^O eq 'linux';
34 system('stty','echo') if $^O eq 'linux';
37 my $ua = LWP::UserAgent -> new;
38 my $url = 'https://www.google.com/accounts/ClientLogin';
40 my %request = ('accountType', 'HOSTED_OR_GOOGLE',
44 'source', 'GoogleDocsUploader-GoogleDocsUploader-00',
47 my $response = $ua -> request(POST $url, [%request]) -> as_string;
48 my $auth = (split /=/, (split /\n/, (split /\n\n/, $response)[1])[2])[1];
50 my $status = (split / /,(split /\n/, $response)[0])[1];
51 die("ERROR: Unauthorized.\n") if $status == 403;
53 $url = "https://docs.google.com/feeds/documents/private/full?alt=json";
55 $ua -> default_header('Authorization' => "GoogleLogin auth=$auth");
57 foreach my $file(@files) {
59 if (!open(FILE, $file)) {
60 print "ERROR: Unable to open '$file' file.\n";
64 my $data = join("", <FILE>);
67 my $mime = type_from_ext(($file =~ m/([^.]+)$/)[0]);
69 $ua -> default_header('Slug' => $file);
71 my $request = HTTP::Request -> new(POST => $url);
72 $request -> content_type($mime);
73 $request -> content($data);
75 my $response = $ua -> request($request) -> as_string;
77 $status = (split / /,(split /\n/, $response)[0])[1];
78 my $body = (split /\n\n/, $response)[1];
87 my $json_text = $json -> decode($body);
89 my $title = $json_text -> {entry} -> {title} -> {'$t'};
90 my $link = $json_text -> {entry} -> {link}[0] -> {href};
92 print "Document successfully created with title '$title'.\nLink:\n$link\n";
100 GoogleDocsUploader.pl - Uploads documents to Google Documents.
104 GoogleDocsUploader [OPTIONS]
110 =item -e Specifies the login email (e.g. example@gmail.com).
112 =item -f Specifies the file to upload (can be more than one).
116 =head1 MULTIPLE FILES UPLOAD
118 You can upload multiple files by setting multiple '-f' options.
122 Allowed file types (checked with MIME) are:
125 TSV text/tab-separated-values
126 TAB text/tab-separated-values
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
135 SXW application/vnd.sun.xml.writer
137 XLS application/vnd.ms-excel
138 XLSX application/vnd.openxmlformats-officedocument.
141 PPT application/vnd.ms-powerpoint
142 PPS application/vnd.ms-powerpoint