2010-01-04 39 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 
相關問題