hard-code text/tab-separated-values mime type
[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         my $mime = 'text/tab-separated-values';
69         
70         $ua -> default_header('Slug' => $file);
71
72         my $request = HTTP::Request -> new(POST => $url);
73         $request -> content_type($mime);
74         $request -> content($data);
75
76         my $response = $ua -> request($request) -> as_string;
77
78         $status = (split / /,(split /\n/, $response)[0])[1];
79         my $body = (split /\n\n/, $response)[1];
80
81         if ($status != 201) {
82                 print "ERROR: $body";
83                 next;
84         }
85
86         my $json = new JSON;
87
88         my $json_text = $json -> decode($body);
89
90         my $title = $json_text -> {entry} -> {title} -> {'$t'};
91         my $link = $json_text -> {entry} -> {link}[0] -> {href};
92
93         print "Document successfully created with title '$title'.\nLink:\n$link\n";
94
95 }
96
97 __END__
98
99 =head1 NAME
100
101 GoogleDocsUploader.pl - Uploads documents to Google Documents.
102
103 =head1 USAGE
104
105 GoogleDocsUploader [OPTIONS]
106
107 =head1 OPTIONS
108
109 =over
110                 
111 =item -e        Specifies the login email (e.g. example@gmail.com).
112
113 =item -f        Specifies the file to upload (can be more than one).
114
115 =back
116
117 =head1 MULTIPLE FILES UPLOAD
118
119 You can upload multiple files by setting multiple '-f' options.
120
121 =head1 FILE TYPE
122
123 Allowed file types (checked with MIME) are:
124
125         CSV     text/csv
126         TSV     text/tab-separated-values
127         TAB     text/tab-separated-values
128         HTML    text/html
129         HTM     text/html
130         DOC     application/msword
131         DOCX    application/vnd.openxmlformats-officedocument.
132                                         wordprocessingml.document
133         ODS     application/x-vnd.oasis.opendocument.spreadsheet
134         ODT     application/vnd.oasis.opendocument.text
135         RTF     application/rtf
136         SXW     application/vnd.sun.xml.writer
137         TXT     text/plain
138         XLS     application/vnd.ms-excel
139         XLSX    application/vnd.openxmlformats-officedocument.
140                                                 spreadsheetml.sheet
141         PDF     application/pdf
142         PPT     application/vnd.ms-powerpoint
143         PPS     application/vnd.ms-powerpoint
144
145 =cut
146
147