1# documentation: http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html
2
3package require rest
4
5package require tls
6::http::register https 443 [list ::tls::socket]
7
8set gdocs(auth) {
9    url https://www.google.com/accounts/ClientLogin
10    method post
11    req_args { Email: Passwd: }
12    opt_args { source:tclrest }
13    static_args { service writely }
14    post_transform {
15        regexp {Auth=(.*)\n} $result -> result
16        return $result
17    }
18}
19
20set gdocs(doclist) {
21    url http://docs.google.com/feeds/default/private/full
22    headers {
23        GData-Version 3.0
24        Authorization {GoogleLogin auth=%token%}
25    }
26    format tdom
27    post_transform {
28        return [list $result [$result getElementsByTagName entry]]
29    }
30}
31
32set gdocs(upload) {
33    url http://docs.google.com/feeds/default/private/full
34    method post
35    body mime_multipart
36    headers {
37        GData-Version 3.0
38        Authorization {GoogleLogin auth=%token%}
39    }
40    opt_args { ocr: }
41}
42
43set gdocs(export) {
44    url http://docs.google.com/feeds/download/documents/Export
45    headers {
46        GData-Version 3.0
47        Authorization {GoogleLogin auth=%token%}
48    }
49    body { arg docID }
50    opt_args { exportFormat: }
51    format raw
52}
53
54rest::create_interface gdocs
55
56proc ::gdocs::create_doc_metadata {args} {
57    set defaults [dict create \
58        text ""
59    ]
60    set args [lindex [::rest::parse_opts {} {title:} {} $args] 0]
61    set args [dict merge $defaults $args]
62
63    set xml {}
64    lappend xml "<?xml version='1.0' encoding='UTF-8'?>"
65    lappend xml "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:gd='http://schemas.google.com/g/2007'>"
66    lappend xml "<category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/g/2005#event'/>"
67    lappend xml "<title>[dict get $args title]</title>"
68    lappend xml "</entry>"
69    return [join $xml \n]
70}
71
72
73# Example usage
74#source gdocs
75#set auth [gdocs::auth -Email me@gmail.com -Passwd passwd]
76#gdocs::set_static_args -token $auth
77#
78#set file IMG_0848.jpg
79#set meta [gdocs::create_doc_metadata -title [file rootname $file]]
80#
81#set fh [open $file]
82#fconfigure $fh -encoding binary -translation lf
83#set filedata [read $fh]
84#close $fh
85#
86#gdocs::upload -ocr true -- [list {Content-Type application/atom+xml} $meta] [list {Content-Type image/jpeg} $filedata]
87
88