Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Update: Now file type is taken from the file extension, and then converted to MIME using Media::Type::Simple. Tested on Windows (virtual machine), it seems to work fine. I also added a check on the OS when password is requested. The previous version can be downloaded from here.

Hello everyone! I wrote a script to upload file to Google Documents. Documentation is included.

There is also a git repository.

Any hints are obviously welcome.

#!/usr/bin/perl # Upload documents to Google Documents. # # Copyright 2010 Alessandro Ghedini <al3xbio@gmail.com> # -------------------------------------------------------------- # "THE BEER-WARE LICENSE" (Revision 42): # Alessandro Ghedini wrote this file. As long as you retain this # notice you can do whatever you want with this stuff. If we # meet some day, and you think this stuff is worth it, you can # buy me a beer in return. # -------------------------------------------------------------- use HTTP::Request::Common; use LWP::UserAgent; use JSON -support_by_pp; use Media::Type::Simple; use strict; die "For info type 'perldoc $0'\n" unless $#ARGV > 0; my (@files, $email, $pwd); for (my $i = 0; $i < $#ARGV + 1; $i++) { push(@files, $ARGV[$i+1]) if ($ARGV[$i] eq "-f"); $email = $ARGV[$i+1] if ($ARGV[$i] eq "-e"); die "For info type 'perldoc $0'\n" if ($ARGV[$i] eq "-h"); } print("Password: "); system('stty','-echo') if $^O eq 'linux'; chop($pwd = <STDIN>); system('stty','echo') if $^O eq 'linux'; print "\n"; my $ua = LWP::UserAgent -> new; my $url = 'https://www.google.com/accounts/ClientLogin'; my %request = ('accountType', 'HOSTED_OR_GOOGLE', 'Email', $email, 'Passwd', $pwd, 'service', 'writely', 'source', 'GoogleDocsUploader-GoogleDocsUploader-00', ); my $response = $ua -> request(POST $url, [%request]) -> as_string; my $auth = (split /=/, (split /\n/, (split /\n\n/, $response)[1])[2])[ +1]; my $status = (split / /,(split /\n/, $response)[0])[1]; die("ERROR: Unauthorized.\n") if $status == 403; $url = "https://docs.google.com/feeds/documents/private/full?alt=json" +; $ua -> default_header('Authorization' => "GoogleLogin auth=$auth"); foreach my $file(@files) { if (!open(FILE, $file)) { print "ERROR: Unable to open '$file' file.\n"; next; } my $data = join("", <FILE>); close FILE; my $mime = type_from_ext(($file =~ m/([^.]+)$/)[0]); $ua -> default_header('Slug' => $file); my $request = HTTP::Request -> new(POST => $url); $request -> content_type($mime); $request -> content($data); my $response = $ua -> request($request) -> as_string; $status = (split / /,(split /\n/, $response)[0])[1]; my $body = (split /\n\n/, $response)[1]; if ($status != 201) { print "ERROR: $body"; next; } my $json = new JSON; my $json_text = $json -> decode($body); my $title = $json_text -> {entry} -> {title} -> {'$t'}; my $link = $json_text -> {entry} -> {link}[0] -> {href}; print "Document successfully created with title '$title'.\nLink:\n +$link\n"; } __END__ =head1 NAME GoogleDocsUploader.pl - Uploads documents to Google Documents. =head1 USAGE GoogleDocsUploader [OPTIONS] =head1 OPTIONS =over =item -e Specifies the login email (e.g. example@gmail.com). =item -f Specifies the file to upload (can be more than one). =back =head1 MULTIPLE FILES UPLOAD You can upload multiple files by setting multiple '-f' options. =head1 FILE TYPE Allowed file types (checked with MIME) are: CSV text/csv TSV text/tab-separated-values TAB text/tab-separated-values HTML text/html HTM text/html DOC application/msword DOCX application/vnd.openxmlformats-officedocument. wordprocessingml.document ODS application/x-vnd.oasis.opendocument.spreadsheet ODT application/vnd.oasis.opendocument.text RTF application/rtf SXW application/vnd.sun.xml.writer TXT text/plain XLS application/vnd.ms-excel XLSX application/vnd.openxmlformats-officedocument. spreadsheetml.sheet PDF application/pdf PPT application/vnd.ms-powerpoint PPS application/vnd.ms-powerpoint =cut
Alex's Log - http://alexlog.co.cc

In reply to Google Docs Uploader by alexbio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found