좋아이며, 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
가이드가 약간 잘못되었지만 승자가 있습니다. 'entry.content.text = "당신의 새로운 HTML"을 사용하는 대신에'entry.content = atom.data.Content ("YOUR NEW HTML")'을 사용해야합니다. 그렇지 않으면 그냥 데이터 앞에 추가됩니다. 귀하의 포인터는 이것을 알아 내는데 엄청난 도움이되었습니다. – Pridkett