2012-04-07 108 views
5

使用Google文檔API,我試圖創建新文檔以及在我的Google文檔的特定文件夾中提供所有當前文檔的列表。我剛開始使用python開發,所以我仍然有點粗糙。使用Google Docs API和Python

事情我試圖做的事:

  1. 與名稱,創建一個集合(或文件夾)的文件夾名稱]只有當 名稱不存在,但
  2. 內創建一個文件[文件夾名稱]
  3. 僅從[文件夾名稱]獲得的文檔列表的鏈接一起 文檔本身

我相信我使用谷歌文檔AP我3.0和我正在使用gdata-2.0.16蟒蛇的幫手。

到目前爲止的代碼:

 

    import gdata.docs.data 
    import gdata.docs.client 

    class SampleConfig(object): 
     APP_NAME = 'GDataDocumentsListAPISample-v1.0' 
     DEBUG = False 

    client = gdata.docs.client.DocsClient() 
    client.ClientLogin('[email_address]','[password]',source=SampleConfig.APP_NAME) 

    col = gdata.docs.data.Resource(type='folder', title='Folder Name') 
    col = client.CreateResource(col) 

    doc = gdata.docs.data.Resource(type='document', title='I did this') 
    doc = client.CreateResource(doc, collection=col) 

所以現在到的問題:我在哪裏卡住絕望:

  1. 我如何檢查,如果[文件夾名稱]的存在呢?
  2. 如何檢索ONLY [文件夾名稱]的內容?
  3. 如何獲取我在此文件夾中創建的所有文檔的絕對鏈接?

我知道我距離完成這裏很遠,但任何幫助或建議,你可以給予很大的。

在此先感謝!

回答

3

You can query for a folder or document。一旦你有了文件夾,你可以列出它的內容。下面是Python庫的例子:

# Create a query matching exactly a title, and include collections 
q = gdata.docs.client.DocsQuery(
    title='EFD', 
    title_exact='true', 
    show_collections='true' 
) 

# Execute the query and get the first entry (if there are name clashes with 
# other folders or files, you will have to handle this). 
folder = client.GetResources(q=q).entry[0] 

# Get the resources in the folder 
contents = client.GetResources(uri=folder.content.src) 

# Print out the title and the absolute link 
for entry in contents.entry: 
    print entry.title.text, entry.GetSelfLink().href 

輸出

My posted doc https://docs.google.com/... 
subtestcoll2 https://docs.google.com/... 
guestimates_1 https://docs.google.com/... 
phase 2 delivery plan - draft https://docs.google.com/... 
Meeting agenda June 09 https://docs.google.com/... 
Phase 2 spec for Graeme 2 March 2009 https://docs.google.com/... 
EFD Meeting 2nd June https://docs.google.com/... 
+0

感謝您的回答細節。真的很感激,我認爲我已經開始根據你的例子開展工作。然而,entry.GetSelfLink()。href給了我一個鏈接,格式爲:https://docs.google.com/feeds/default/private/full/folder%....在瀏覽器中使用get我「無效的請求URI」 – user791793 2012-04-11 11:58:21