Google 사이트 Python API로 첨부 컨텐츠를 업데이트하려면 어떻게해야합니까?
-
18-09-2019 - |
문제
Google 사이트를 통해 생성되고 관리되는 웹 사이트에서 일부 첨부 파일을 자동으로 업데이트하는 스크립트를 작성하려고합니다. 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
좋아, API는 이상하다고 문서는 명확하지 않습니다. 여기 내가 알아 낸 것이 있습니다. 처음으로 첨부 파일을 업로드 할 때는 업로드 타치 메소드를 통해 첨부 파일을 수행하지만 후속 시도에서는 업데이트를 호출해야합니다. 다음은 다음과 같은 코드입니다.
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 방법, 작동해야합니다. 당신은 또한 확인하고 싶을 수도 있습니다 샘플 코드 사이트 API의 경우 해당 방법을 사용합니다.