Google App Engineで簡単に利用したいのでtext形式でダウンロードがしかくなったのです。
以下のような感じで取れました。
Djangoでの例です。
とりあえず前件取得してランダムで取得してみます。
# -*- coding: utf-8 -*-
import random
from django.http import HttpResponseRedirect
from django.utils.translation import ugettext as _
from ragendja.template import render_to_response
from django.conf import settings
import gdata.gauth
import gdata.docs.client
import gdata.docs.data
APP_NAME = 'googleDocumentTextGetTest'
def index(request):
payload = dict()
client = gdata.docs.client.DocsClient(source=APP_NAME)
client.ssl = True
client.ClientLogin('googleアカウントのメールアドレス', 'そのパスワード', client.source)
payload['feed'] = client.get_everything()
feed_len = len(payload['feed'])
while True:
get_doc = random.randint(0, feed_len-1)
payload['selection'] = payload['feed'][get_doc]
#取れたものがスプレッドシートとかだったらもう一回取り直す
doc_type = payload['selection'].GetDocumentType()
if doc_type == "document":
break
doc_uri = '/feeds/download/documents/Export?docID=' + payload['selection'].resource_id.text + '&exportFormat=txt'
payload['content'] = client.get_file_content(doc_uri)
return render_to_response(request, 'index.html',payload)
参考までにテンプレートは以下のような感じです。
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html
xmlns="http://www.w3.org/1999/xhtml"
dir="{% if LANGUAGE_BIDI %}rtl{% else %}ltr{% endif %}"
xml:lang="{% firstof LANGUAGE_CODE 'en' %}"
lang="{% firstof LANGUAGE_CODE 'en' %}">
<head>
<title>test</title>
</head>
<body>
{{ selection.title.text }}<br>
<pre>
{{ content }}
</pre>
<ul>
{% for entry in feed.entry %}
<li>{{ entry.title.text }}</li>
{% endfor %}
</ul>
<body>
<html>
本当だったら、全件取得でなく以下のような感じで
/feeds/default/private/full/-/document
documentファイルだけを取得したかったのですが、なぜかできませんでした。
0 件のコメント:
コメントを投稿