summaryrefslogtreecommitdiff
path: root/old/published/Webmonkey/APIs/gdata_documents_list.txt
diff options
context:
space:
mode:
Diffstat (limited to 'old/published/Webmonkey/APIs/gdata_documents_list.txt')
-rw-r--r--old/published/Webmonkey/APIs/gdata_documents_list.txt101
1 files changed, 101 insertions, 0 deletions
diff --git a/old/published/Webmonkey/APIs/gdata_documents_list.txt b/old/published/Webmonkey/APIs/gdata_documents_list.txt
new file mode 100644
index 0000000..b654b81
--- /dev/null
+++ b/old/published/Webmonkey/APIs/gdata_documents_list.txt
@@ -0,0 +1,101 @@
+Google Documents offers free online storage for a variety of common files -- your MS Office documents, spreadsheets, text files, and more.
+
+Even if you don't actually use Google Documents for editing or creating documents, it can serve as a handy backup for your desktop files. Today we'll take a look at the gData APIs that allow you to upload files from your local machine and store them in Google Documents.
+
+
+== Installing the gData API ==
+
+Because most of what we're going to do is shell-based, we'll be using the the Python gData library. If you're not a Python fan there are a number of other client libraries available for interacting with Google Docs including [http://code.google.com/apis/youtube/developers_guide_php.html PHP], [http://code.google.com/apis/youtube/developers_guide_dotnet.html .NET], [http://code.google.com/apis/youtube/developers_guide_java.html Java] and [http://code.google.com/apis/youtube/developers_guide_python.html Python].
+
+To get started go ahead and download the [http://code.google.com/p/gdata-python-client/ Python gData Client Library]. Follow the instructions for installing the Library as well as the dependancies (in this case, ElementTree -- only necessary if you aren't running Python 2.5)
+
+Now, just to make sure you've got everything set up correctly, fire up a terminal window, start Python and try importing the modules we need:
+
+<pre>
+>>> import gdata.docs
+>>> import gdata.docs.service
+</pre>
+
+Assuming those work, you're ready to start working with the API.
+
+== Getting Started ==
+
+The first thing we need to get out of the way is what kinds of documents we can upload. There's a handy static member we can access to get a complete list:
+
+<pre>
+>>> from gdata.docs.service import SUPPORTED_FILETYPES
+>>> SUPPORTED_FILETYPES
+</pre>
+
+Running that command will reveal that these are our supported upload options:
+
+#RTF: application/rtf
+#PPT: application/vnd.ms-powerpoint
+#DOC: application/msword
+#HTM: text/html
+#ODS: application/x-vnd.oasis.opendocument.spreadsheet
+#ODT: application/vnd.oasis.opendocument.text
+#TXT: text/plain
+#PPS: application/vnd.ms-powerpoint
+#HTML: text/html
+#TAB: text/tab-separated-values
+#SXW: application/vnd.sun.xml.writer
+#TSV: text/tab-separated-values
+#CSV: text/csv
+#XLS: application/vnd.ms-excel
+
+Definitely not everything you might want to upload, but between the MS Office options and good old plain text, you should be able to backup at least the majority of your files.
+
+Now let's take a look at authenticating with the gData API.
+
+Create a new file named gdata_uploader.py and save it somewhere on your Python Path. Now open it in your favorite text editor. Paste in this code:
+
+<pre>
+from gdata.docs import service
+
+def create_client():
+ client = service.DocsService()
+ client.email = 'yourname@gmail.com'
+ client.password = 'password'
+ client.ProgrammaticLogin()
+ return client
+
+</pre>
+
+All we've done here is create a wrapper function for easy logins. Now, any time we want to login, we simply call <code>create_client</code>. To make you code a bit more robust you can pull out those hardcoded <code>email</code> and <code>password</code> attributes and define them elsewhere.
+
+== Uploading a document ==
+
+Now we need to add a function that will actually upload a document. Just below the code we created above, paste in this function:
+
+<pre>
+def upload_file(file_path, content_type, title=None):
+ import gdata
+ ms = gdata.MediaSource(file_path = file_path, content_type = content_type)
+ client = create_client()
+ entry = client.UploadDocument(ms,title)
+ print 'Link:', entry.GetAlternateLink().href
+</pre>
+
+Now let's play with this stuff in the shell:
+
+>>> import gdata_upload
+>>> gdata_upload.upload_file('path/to/file.txt','text/plain','Testing gData File Upload')
+Link: http://docs.google.com/Doc?id=<random string of numbers>
+>>>
+
+Note that our <code>upload_file</code> takes an optional parameter "title", if you import Python's date module and pass along the date as a string it's easy to make incremental backups, like: myfile-082908.txt, myfile-083008.txt and so on.
+
+== Where to go from here ==
+
+To automate our backup process you could call the upload file function from a cronjob. For instance I use:
+
+<pre>
+0 21 * * * python path/to/backup_docs.py 2>&1
+</pre>
+
+In this case backup_docs.py is just a three line file that imports our functions from gdata_uploader.py and then uses Python's <code>os</code> module to grab a list of files I want backed up and calls the <code>upload_file</code> function.
+
+While the automated script is a nice extra backup, unfortunately the gData Documents API is somewhat limited. For instance it would be nice if we could automatically move our document to a specific folder, but that currently isn't possible.
+
+There are some read functions available though, have a look through the [http://code.google.com/apis/documents/reference.html official docs] and if you come up with a cool way to use the API, be sure to add it to this page.