Google Sites Python APIで添付ファイルのコンテンツを更新するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/2001664

  •  18-09-2019
  •  | 
  •  

質問

Googleサイトを介して作成および管理されているWebサイトでいくつかの添付ファイルを自動的に更新するスクリプトを作成しようとしています。これは、Googleがリリースしたので可能です サイトAPI 9月と python gdata api サイトをサポートする主張。しかし、私が見つけることができる最も近い方法は呼ばれます client.update, 、コンテンツではなく、添付ファイルのメタデータを更新することができます。

Java APIで添付ファイルを更新することは、新しいものを作成することによって行われます MediaFileSource そして、呼び出します entry.setMediaFileSource(source) に続く entry.updateMedia(). 。ただし、Python APIに似たものは見つかりません。私は愚かで何かが足りないのですか、それともPython APIを使用してGoogleサイトの添付ファイルを更新することは本当に不可能ですか?

役に立ちましたか?

解決

サイトAPIはv1.1に更新されました。これはおそらく新しい追加です

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

他のヒント

ドキュメント ここ 添付ファイルのコンテンツとメタデータを更新する方法についての例を提供します(添付ファイルのコンテンツとメタデータを置き換えるサブセクション)

残された唯一のことは得ることです 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

OK、APIそこには奇妙で、ドキュメントはあまり明確ではありません。これが私が理解したものです。添付ファイルを初めてアップロードするときは、uploadattachmentメソッドを使用して行いますが、フォローアップの試みでは、更新を呼び出す必要があります。これがそれを行うコードです:

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

あります upload_attachment 方法、それは機能するはずです。また、チェックアウトすることもできます サンプルコード Sites APIの場合、その方法を使用します。

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top