2012-04-07 224 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

相关问题