2018年9月28日金曜日

google_home_speechのエラー AttributeError: 'NoneType' object has no attribute 'group'

ラズパイマガジン2018年8月号の記事で紹介した、Google Homeにテキストを読み上げさせるプログラム、google_home_speechが、私の環境では9月19日以降エラーで動かなくなっていることに気づきました。

エラーの内容や経緯には興味無く、解決する方法をすぐに知りたい方は、この記事の「解決方法」まで読み飛ばしても構いません。

エラー内容

curlコマンドで、「こんにちは」と喋らせようとすると、下記のようなメッセージが出力されて、処理が中断され、Google Homeは何も反応しません。

127.0.0.1 - - [27/Sep/2018 06:45:35] "GET / HTTP/1.1" 404 -Host: localhost:8000User-Agent: curl/7.52.1Accept: */*Content-Length: 26Content-Type: application/x-www-form-urlencoded

POST requestBody={"text":"こんにちは"}
**JSON**
{
    "text":"\u3053\u3093\u306b\u3061\u306f"
}
get speech data for こんにちは(ja)
Traceback (most recent call last):
  File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
    self.handle()
  File "/usr/lib/python3.5/http/server.py", line 422, in handle
    self.handle_one_request()
  File "/usr/lib/python3.5/http/server.py", line 410, in handle_one_request
    method()
  File "./google_home_speech.py", line 105, in do_POST
    do_speech(text, language)
  File "./google_home_speech.py", line 66, in do_speech
    tts.save(speech_filename)
  File "/home/pi/.local/lib/python3.5/site-packages/gtts/tts.py", line 247, in save
    self.write_to_fp(f)
  File "/home/pi/.local/lib/python3.5/site-packages/gtts/tts.py", line 187, in write_to_fp
    part_tk = self.token.calculate_token(part)
  File "/home/pi/.local/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 28, in calculate_token
    seed = self._get_token_key()
  File "/home/pi/.local/lib/python3.5/site-packages/gtts_token/gtts_token.py", line 62, in _get_token_key
    a = re.search("a\\\\x3d(-?\d+);", tkk_expr).group(1)
AttributeError: 'NoneType' object has no attribute 'group'

Googleのテキスト読み上げサービスを使って音声ファイルを取得するgTTSパッケージ配下でエラーを出しているようです。何度やっても同じエラーで失敗しました。

エラーが発生しているのは私だけ?

数ヶ月にわたって安定して動いていたのに、急に全く動かなくなったのは、Googleのテキスト読み上げサービス側に何か変更があったのが疑わしいです。その場合、同じパッケージを使っている他の人にも影響があるはず。gTTSパッケージのgithubサイトのissuesを見ると、案の定、私と同じ時期に同じエラーを報告している人が複数いることがわかりました。

https://github.com/pndurette/gTTS/issues/138
https://github.com/pndurette/gTTS/issues/60

ソースのここを直したら良いなどの情報が見つかりますが、すでに本家のパッケージがアップデートされているため、パッケージのアップデートで解決するようです。おそらくもともとあった問題が、Google側になんらかの変更があったことで顕在化したのだと想像します。

アップデートが必要なパッケージはgTTS-tokenです。バージョン1.1.1が問題あり。1.1.2で解決されています。インストールされているパッケージはpip3 freezeで確認できます。

パッケージのバージョン

問題発生時


$ pip3 freeze

beautifulsoup4==4.6.0

bs4==0.0.1

certifi==2018.4.16

chardet==3.0.4

click==6.7
cryptography==1.7.1
gTTS==2.0.0
gTTS-token==1.1.1
idna==2.7
keyring==10.1
keyrings.alt==1.3
netifaces==0.10.6
protobuf==3.6.0
pyasn1==0.1.9
PyChromecast==2.2.0
pycrypto==2.6.1
pygobject==3.22.0
python-apt==1.1.0b5
pyxdg==0.25
requests==2.19.1
SecretStorage==2.3.1
six==1.11.0
urllib3==1.23
zeroconf==0.20.0

問題解決後 


$ pip3 freeze

beautifulsoup4==4.6.0

bs4==0.0.1

certifi==2018.8.24

chardet==3.0.4

click==6.7
cryptography==1.7.1
gTTS==2.0.0
gTTS-token==1.1.2
idna==2.7
keyring==10.1
keyrings.alt==1.3
netifaces==0.10.6
protobuf==3.6.0
pyasn1==0.1.9
PyChromecast==2.2.0
pycrypto==2.6.1
pygobject==3.22.0
python-apt==1.1.0b5
pyxdg==0.25
requests==2.19.1
SecretStorage==2.3.1
six==1.11.0
urllib3==1.23
zeroconf==0.20.0

解決方法

google_home_speechをインストールしたディレクトリで、下記のコマンドでgTTS-tokenパッケージをアップデートしてください。
pip3 install gTTS-token --upgrade

アップデートが完了したら、google_home_speechを再起動してください。

ラズパイでLet's Encryptの証明書更新に失敗

ラズパイを自宅用の各種サーバー(DNS、メール、ウェブ他)として使っています。httpsのサーバー証明書の発行・更新にはLet's Encriptを使っています。今日たまたま証明書を更新しようとすると下記のエラーに陥りました。何度やっても失敗します。 ma...