今回はGoogle Drive に入っているテキストファイルをの内容を読み込んでLINE BOTでメッセージを送るとそのファイルの内容が帰ってくるというアプリケーションを作成しようとしたってことです。
作りながら書いているのでまだわからないけどね!ははっ!
pythonでgoogle driveを操作する - in my 雑記
このサイトの記事を参考に、google apiに登録して「クライアントID」、「クライアントシークレット」を取得します。
んで、PyCharmでプロジェクトを作成し、プロジェクトにsetting.yamlっていうのを新規作成して、上記のサイトに書いてあった内容にした。
(でもこれってHerokuに上げられなくね・・・?)(Herokuってプライベートリポジトリでも行けるのかな・・・?)
まぁ最悪環境変数使ってHeroku側でsettings.yamlファイル作らせればいいか
とりあえずGoogleDrive側には[Hello World in TextFile]と書かれたtext.txtファイルをアップロードしておいた。 今回はLINE BOT上でこれが扱えたら勝ちだとしよう。
とりあえずローカルでPyDriveの確認
をしてみました。
とりあえずこんなのをプロジェクトファイルに作った。
# -*- coding: utf-8 -*-
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
file3 = drive.CreateFile()
file3.GetContentFile('text.txt')
実行してみる
Traceback (most recent call last):
File "/home/reud/PycharmProjects/fileload/filetest.py", line 9, in <module>
file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
File "/home/reud/.local/lib/python3.6/site-packages/pydrive/apiattr.py", line 162, in GetList
for x in self:
File "/home/reud/.local/lib/python3.6/site-packages/pydrive/apiattr.py", line 146, in __next__
result = self._GetList()
File "/home/reud/.local/lib/python3.6/site-packages/pydrive/auth.py", line 75, in _decorated
return decoratee(self, *args, **kwargs)
File "/home/reud/.local/lib/python3.6/site-packages/pydrive/files.py", line 64, in _GetList
http=self.http)
File "/home/reud/.local/lib/python3.6/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/reud/.local/lib/python3.6/site-packages/googleapiclient/http.py", line 842, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 403 when requesting https://www.googleapis.com/drive/v2/files?q=%27root%27+in+parents&maxResults=1000&alt=json returned "Access Not Configured. Drive API has not been used in project hoge before or it is disabled. Enable it by visiting https://console.developers.google.com/apis/api/drive.googleapis.com/overview?project=hoge then retry. If you enabled this API recently, wait a few minutes for the action to propagate to our systems and retry.">
Process finished with exit code 1
やばいもう詰んだか・・・?と思ったらAPI有効化してなかっただけだったので指定のURLにアクセスして再度チャレンジした。
結果
非常に微妙な結果となった。なんというか最初にファイル一覧を表示させた所、そこは空になっていた。APIごとにフォルダーみたいなのが出来ていると考えられる。
検証
ファイルのアップロード後、ファイルの一覧を取得させる。
# -*- coding: utf-8 -*-
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file1 = drive.CreateFile({'title': 'Hello.txt'})
file1.SetContentString('Hello')
# アップロード
file1.Upload()
file_list = drive.ListFile({'q': "'root' in parents"}).GetList()
for file1 in file_list:
print('title: %s, id: %s' % (file1['title'], file1['id']))
出力は以下のようになった。
/usr/bin/python3.6 /home/reud/PycharmProjects/fileload/filetest.py title: Hello.txt, id: ーーーーーーーーーーーーーー Process finished with exit code 0
idは一応隠している。 正常に取得出来ていることがわかる。
ここで同じファイルをアップロードしてみる。
先程のプログラムを実行した所、IDが違う同名のファイルが作成されていた。
このことから、今回のテストで出力される文字列を変更したい場合には、その度にIDを取得する必要があると考えられる。
次にこのようなコードを作成した。
# -*- coding: utf-8 -*-
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
gauth = GoogleAuth()
gauth.LocalWebserverAuth()
drive = GoogleDrive(gauth)
file3 = drive.CreateFile({'id': "1vhDdSI7E84"})
file3.GetContentFile('Hello.txt')
idは先程取得した文字列を用いている。
これは正常に動作していた。
Herokuを試す
次にHerokuを試してみる。 今回、LINE BOTはFlaskで実装するため,とりあえずFlaskで簡単なプログラムを実装しそれをHerokuにデプロイする。
FlaskもHerokuも超初心者なので
Re:ゼロからFlaskで始めるHeroku生活 〜環境構築とこんにちは世界〜
を参考に進めていく。
コードや環境構築はPyCharmの新規プロジェクトを選んでFlaskを選択したときに出るハロワプロジェクトを使った。
一応コード
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
見ればわかる感じのコードほんといいですよね。
このタイミングでHerokuに登録した。
pip freeze > requirements.txt
をPyCharmの下の方にあるターミナルから実行してライブラリ一覧ファイルを作った。
んでここでgitにアップロードするんだけど、ここでプライベートリポジトリにしてみる。
理由は出来るかよくわからないから。
んでサイト通りに色々やってたら・・・
だめでした。
色々あったけどH14エラーを回避できません。
とりあえずパブリックレポジトリにして再制作
あ、gunicornいれてねぇかも・・・
色々調べまくってProcfileとかをこのサイトを参考にしてみる。
H20エラーが出てきた。 もういややもう寝る