Skip to content

Instantly share code, notes, and snippets.

@pridkett
Created February 5, 2010 02:01
Show Gist options
  • Save pridkett/295408 to your computer and use it in GitHub Desktop.
Save pridkett/295408 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
#
# sites_uploader.py
#
# a simple script to upload revised versions of documents onto google sites
__author__ = "Patrick Wagstrom <patrick@wagstrom.net>"
import mimetypes
import gdata
import gdata.sites.client
import gdata.sites.data
import atom.data
import atom.core
from optparse import OptionParser
import os
import sys
SOURCE_APP_NAME = 'wagstrom-SimpleSiteUploader-v0.1'
class SitesUploader(object):
def __init__(self, site_name=None, site_domain=None, ssl=False, username=None, password=None):
mimetypes.init()
self.client = gdata.sites.client.SitesClient(source=SOURCE_APP_NAME,
site=site_name,
domain=site_domain)
self.client.ssl = ssl
try:
self.client.ClientLogin(username, password, source=SOURCE_APP_NAME)
except gdata.client.BadAuthentication:
exit('Invalid user credentials given.')
except gdata.client.Error:
exit('Login Error')
def list_all_entries(self):
feed = self.client.get_content_feed()
for entry in feed.entry:
print '["%s", "%s", "%s"]' % (entry.title.text, entry.kind(), entry.id.text)
def get_entry(self, entryId):
return self.client.get_entry(entryId)
def get_entry_kind(self, entry):
for tentry in self.client.get_content_feed().entry:
if entry.id.text == tentry.id.text:
return tentry.kind()
def update_entry(self, entry, filename):
fileEx = os.path.splitext(filename)[-1]
contentType = mimetypes.types_map[fileEx]
self.client.update(entry)
return
if entry.kind() == "attachment":
self.client.update(entry)
elif entry.kind() == "webpage":
self.client.update(entry)
if __name__ == "__main__":
parser = OptionParser()
parser.add_option("-s", "--site", action="store",
dest="site_name", default=None,
help="Site name to edit")
parser.add_option("-d", "--domain", action="store",
dest="site_domain", default=None,
help="Site domain to edit")
parser.add_option("--ssl", action="store_true",
dest="ssl", default=False,
help="Require SSL")
parser.add_option("-u", "--user", action="store",
dest="user", default=None,
help="Username")
parser.add_option("-p", "--password", action="store",
dest="password", default=None,
help="Password")
parser.add_option("-l", "--list", action="store_true",
dest="list", default=False,
help="List documents in site and exit")
(options, args) = parser.parse_args()
uploader = SitesUploader(site_name=options.site_name, site_domain=options.site_domain,
ssl=options.ssl, username=options.user, password=options.password)
if options.list:
uploader.list_all_entries()
sys.exit()
entry = uploader.get_entry(args[0])
if uploader.get_entry_kind(entry) == "webpage":
entry.content = atom.data.Content(open(args[1]).read())
updated_entry = uploader.client.Update(entry)
elif uploader.get_entry_kind(entry) == "attachment":
fileEx = os.path.splitext(args[1])[-1]
contentType = mimetypes.types_map[fileEx]
ms = gdata.data.MediaSource(file_path=args[1], content_type=contentType)
updated_attachment = uploader.client.Update(entry, media_source=ms)
@joudinet
Copy link

joudinet commented Mar 8, 2013

This is exactly what I was looking for before deciding to host my academic website on Google.Sites. I'll try it asap. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment