开发者

How do I update attachment content with the google sites python API?

I'm trying to write a script that will automatically update some attachments on a website created and managed through Google Sites. This should be possible as Google released the Sites API in September and the Python GData API claims to support sites. However, the closest method I can find is called client.update, which allows me to update the metadata o开发者_如何转开发f an attachment, but not the content.

In the Java API updating an attachment is done by creating a new MediaFileSource and then calling entry.setMediaFileSource(source) followed by entry.updateMedia(). However, I can't find anything similar in the Python API. Am I dumb and just missing something, or is it really not possible to update a google sites attachment using the python API?


The documentation here provides an example on how to update an attachment's content and metadata (subsection Replacing an attachment's content and metadata)

The only thing left out is to get existing_attachment which can be done easily with something like this:

existing_attachment = None
uri = '%s?kind=%s' % (client.MakeContentFeedUri(), 'attachment')
feed = client.GetContentFeed(uri=uri)
for entry in feed.entry:
  if entry.title.text == title:
    print '%s [%s]' % (entry.title.text, entry.Kind())
    existing_attachment = entry


sites api has been updated to v1.1; this is probably a new addition

http://code.google.com/apis/sites/docs/1.0/developers_guide_python.html#UpdatingContent


Ok, the API there is weird, and the documentation is not very clear. Here is what I have figured out. First time you upload an attachment, you do it through UploadAttachment method, but on the follow-up attempts, you need to call Update. Here's the code that does it:

class AttachmentUploader(object):
  """Uploads a given attachment to a given filecabinet in Google Sites."""

  def __init__(self, site, username, password):
    self.client = gdata.sites.client.SitesClient(
        source="uploaderScript", site=site)
    self.client.ssl = True
    try:
      self.client.ClientLogin(username, password, "some.key")
    except:
      traceback.print_exc()
      raise

  def FindAttachment(self, title):
    uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "attachment")
    feed = self.client.GetContentFeed(uri=uri)
    for entry in feed.entry:
      if entry.title.text == title:
        return entry
    return None

  def FindCabinet(self, title):
    uri = "%s?kind=%s" % (self.client.MakeContentFeedUri(), "filecabinet")
    feed = self.client.GetContentFeed(uri=uri)
    for entry in feed.entry:
      if entry.title.text == title:
        return entry
    return None

  def Upload(self, cabinet_title, title, file_path, description):
    """Upload the given file as attachment."""
    ms = gdata.data.MediaSource(file_path=file_path, content_type="text/ascii")

    existing_attachment = self.FindAttachment(title)
    if existing_attachment is not None:
      existing_attachment.summary.text = description
      updated = self.client.Update(existing_attachment, media_source=ms)
      print "Updated: ", updated.GetAlternateLink().href
    else:
      cabinet = self.FindCabinet(cabinet_title)
      if cabinet is None:
        print "OUCH: cabinet %s does not exist" % cabinet_title
        return
      attachment = self.client.UploadAttachment(
          ms, cabinet, title=title, description=description)
      print "Uploaded: ", attachment.GetAlternateLink().href


There is an upload_attachment method, that should work. You may also want to check out the sample code for Sites API, it uses that method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜