如何使用 google 站点 python API 更新附件内容?

发布于 2024-08-16 09:12:47 字数 579 浏览 4 评论 0原文

我正在尝试编写一个脚本,该脚本将自动更新通过 Google 协作平台创建和管理的网站上的一些附件。这应该是可能的,因为 Google 在 9 月份发布了 Sites APIPython GData API 声称支持网站。但是,我能找到的最接近的方法称为 client.update,它允许我更新附件的元数据,但不能更新内容。

在 Java API 中,更新附件的方法是创建一个新的 MediaFileSource,然后调用 entry.setMediaFileSource(source),然后调用 entry.updateMedia() >。但是,我在 Python API 中找不到类似的东西。我是愚蠢的,只是错过了一些东西,还是真的无法使用 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 of 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

轮廓§ 2024-08-23 09:12:47

此处提供了有关如何更新附件的内容和元数据(替换附件的内容和元数据小节)

唯一遗漏的是获取 existing_attachment ,这可以通过以下方式轻松完成:

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

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
将军与妓 2024-08-23 09:12:47

好吧,那里的 API 很奇怪,而且文档也不是很清楚。这是我的想法。第一次上传附件时,您可以通过UploadAttachment方法来完成,但在后续尝试中,您需要调用Update。这是执行此操作的代码:

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

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
待天淡蓝洁白时 2024-08-23 09:12:47

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文