2010-01-04 37 views
2

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

在Java API中,更新附件是通过创建一个新的MediaFileSource,然后调用entry.setMediaFileSource(source)后跟entry.updateMedia()来完成的。但是,我在Python API中找不到类似的东西。我是否愚蠢,只是缺少一些东西,还是真的不可能使用python API更新谷歌网站附件?

回答

0

有一个upload_attachment方法,应该工作。您可能还想查看Site API的sample code,它使用该方法。

+0

不幸的是,而适用于上传的附件,它不允许一个更新的附件,这是我需要做的。调用'upload_attachment'有重复的文件名返回错误: 服务器回应:409,重复插入 - 节点名称为"的test.txt "已经存在 - 现有节点ID是4770556649999999999 – Pridkett

+0

我认为唯一的办法就是使用Java如何库确实:http://code.google.com/apis/sites/docs/1.0/developers_guide_protocol.html#UpdatingAnAttachment 当前的Python库代码使用gdata协议,该协议没有记录。解决方法是从gdata客户端获取ClientLogin授权并通过PUT方法自行发出http请求。或者删除旧文件并上传新文件,这非常糟糕。 – livibetter

+0

我的印象是,Java库也使用GData协议。所以,从它的声音来看,目前在Python中不可能做到这一点。我想过删除和重新添加,但这真的很糟糕,可能会破坏一些链接。也许我只需要等待一段时间... – Pridkett

1

确定,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 
+0

感谢您为详细的课程提供帮助。原来的问题是,API当时不支持更新附件。你仍然可以得到积分,因为这对于未来寻求实施的人来说确实很有帮助。 – Pridkett

4

文档here提供了有关如何更新附件的内容和元数据的例子(第更换附件的内容和元数据)

冷落的唯一的事情是让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 
相关问题