質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

Q&A

1回答

1559閲覧

Pythonでマストドン読み上げするプログラムを作っています。一つ読んだ後インデックスエラーで止まってしまいます。回避方法はどうしたらよいでしょうか?

BURI55

総合スコア25

Windows 10

Windows 10は、マイクロソフト社がリリースしたOSです。Modern UIを標準画面にした8.1から、10では再びデスクトップ主体に戻され、UIも変更されています。PCやスマホ、タブレットなど様々なデバイスに幅広く対応していることが特徴です。

Python 3.x

Python 3はPythonプログラミング言語の最新バージョンであり、2008年12月3日にリリースされました。

0グッド

0クリップ

投稿2018/11/06 18:45

編集2018/11/07 13:14

Python3.7にて、マストドン読み上げのプログラムをこちらを参考に作っています。mastodon.pyを入れ、config.jsonを設定し、動かしてみましたが、mastodon.stream_localが、mastodon.Local_streamになっていたほかは間違いなく動きましたが、一つだけ読み上げてインデックスエラーで止まってしまいます。連続で読み上げさせるにはどうしたらよいでしょうか?
プログラム

Python

1import datetime 2import os 3import subprocess 4import xml.etree.ElementTree as ET 5from getpass import getpass 6from typing import NamedTuple 7 8from mastodon import Mastodon, StreamListener 9 10from config import generate_from_file 11from utility import strip_html_tags 12 13 14class Comment(NamedTuple): 15 user: str 16 html_text: str 17 unix_time: int 18 icon_url: str 19 20 @property 21 def text(self) -> str: 22 return strip_html_tags(self.html_text) 23 24 25def construct_execute_command(execute_command_format: str, comment: Comment): 26 return execute_command_format.format( 27 user=comment.user, 28 text=comment.text, 29 unix_time=comment.unix_time, 30 ) 31 32 33class Runner(StreamListener): 34 def __init__(self, config): 35 self.config = config 36 self.highlight_list = self.config.highlight 37 self.path_xml = self.config.path_xml_html5_comment_generator 38 39 self.mastodon = Mastodon( 40 client_id='app.secret', 41 access_token='user.secret', 42 api_base_url=config.api_base_url, 43 ) 44 45 def make_comment(self, toot): 46 user = toot['account']['display_name'] 47 icon_url = toot['account']['avatar'] 48 text = toot['content'] 49 50 ok = False 51 if self.highlight_list is None or len(self.highlight_list) == 0: 52 ok = True 53 for highlight in self.highlight_list: 54 if highlight in text: 55 ok = True 56 57 if not ok: 58 return None 59 60 unix_time = int(datetime.datetime.now().timestamp()) 61 return Comment(user, text, unix_time, icon_url) 62 63 def make_xml_element(self, root_xml, comment: Comment): 64 last_no = int(list(root_xml)[-1].attrib['no']) 65 attr = dict( 66 no=str(last_no + 1), 67 time=str(comment.unix_time + 3), 68 handle=comment.user, 69 icon_url=comment.icon_url, 70 ) 71 element = ET.Element('comment', attrib=attr) 72 element.text = comment.text 73 return element 74 75 def on_update(self, toot): 76 comment = self.make_comment(toot) 77 if comment is None: 78 return 79 80 # execute command 81 for f in self.config.execute_command: 82 subprocess.run(construct_execute_command(f, comment).split(' ')) 83 84 # get last number of xml 85 tree = ET.parse(self.path_xml) 86 root_xml = tree.getroot() 87 element = self.make_xml_element(root_xml, comment) 88 89 # add xml 90 root_xml.append(element) 91 tree.write(self.path_xml, encoding='utf-8') 92 93 def run(self): 94 print('running...') 95 self.mastodon.stream_local(self) 96 97 98path_config = "./config.json" 99config = generate_from_file(path_config) 100 101assert config.api_base_url is None or len(config.api_base_url), "config.jsonのapi_base_urlを指定してください。" 102 103if not os.path.exists('./app.secret'): 104 Mastodon.create_app( 105 'nicolive-mastodon', 106 api_base_url=config.api_base_url, 107 to_file='app.secret', 108 ) 109 110if not os.path.exists('./user.secret'): 111 username = input("ユーザー名(e-mailアドレス)を入力してください... ") 112 password = getpass("パスワードを入力してください... ") 113 mastodon = Mastodon( 114 client_id='app.secret', 115 api_base_url=config.api_base_url, 116 ) 117 mastodon.log_in( 118 username, 119 password, 120 to_file='user.secret' 121 ) 122 123runner = Runner( 124 config=config, 125) 126runner.run()

エラーメッセージ

cmd

1C:\Users\user\Desktop\nicolive-mastodon-1.1.2>python run.py 2running... 3Traceback (most recent call last): 4 File "run.py", line 126, in <module> 5 runner.run() 6 File "run.py", line 95, in run 7 self.mastodon.stream_local(self, run_async=False) 8 File "<decorator-gen-85>", line 2, in stream_local 9 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 77, in wrapper 10 return function(self, *args, **kwargs) 11 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 2001, in stream_local 12 return self.__stream('/api/v1/streaming/public/local', listener, run_async=run_async, timeout=timeout, reconnect_async=reconnect_async, reconnect_async_wait_sec=reconnect_async_wait_sec) 13 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 2393, in __stream 14 listener.handle_stream(r) 15 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\streaming.py", line 72, in handle_stream 16 self._dispatch(event) 17 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\streaming.py", line 145, in _dispatch 18 handler(payload) 19 File "run.py", line 87, in on_update 20 element = self.make_xml_element(root_xml, comment) 21 File "run.py", line 64, in make_xml_element 22 last_no = int(list(root_xml)[-1].attrib['no']) 23IndexError: list index out of range 24 25C:\Users\user\Desktop\nicolive-mastodon-1.1.2>

この辺のxml生成があやしいのですがよくわかりません。

python

1def on_update(self, toot): 2 comment = self.make_comment(toot) 3 if comment is None: 4 return 5 6 # execute command 7 for f in self.config.execute_command: 8 subprocess.run(construct_execute_command(f, comment).split(' ')) 9 10 # get last number of xml 11 tree = ET.parse(self.path_xml) 12 root_xml = tree.getroot() 13 element = self.make_xml_element(root_xml, comment) 14 15 # add xml 16 root_xml.append(element) 17 tree.write(self.path_xml, encoding='utf-8')

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

エラーメッセージを読みましょう

last_no = int(list(root_xml)[-1].attrib['no'])

配列の添字は0から始まります。-1 は許されません

投稿2018/11/06 22:28

y_waiwai

総合スコア87749

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

BURI55

2018/11/06 22:38

修正したところエラーでは止まらなくなりましたが、実行時に一つしか読まないのは相変わらずです。
can110

2018/11/06 22:40 編集

pythonのリストでは添え字[-1]は末尾を指します。 print([1,2,3][-1]) # 3
BURI55

2018/11/06 22:48

last_no = int(list(root_xml)[-1].attrib['no'])だと末尾の数字を取得しているようですから、あっているようですね。
can110

2018/11/06 22:51 編集

ただしlist(root_xml)が空だと提示エラーが発生しえます。 空になることが論理的にありえないのであれば、上位レベルのコードに問題あります。 ありえるのであればlen(list(root_xml)) が1以上で処理を切り分けるべきでしょう。(空なら処理中断するなど)
BURI55

2018/11/06 22:57

書き込みの数を数えているのではないかと思いました、としたら、常に書き込みがあるわけではありませんから空になることもあるのかもしれませんね。
y_waiwai

2018/11/06 23:07

ああ、んじゃListのナカミがないってことですな root_xmlがナニモノか見てみることでしょうね
BURI55

2018/11/06 23:14

root_xml = tree.getroot()ですね。このコードの後にif len(root_xml) > 1 else入れてみましたが、うまくいきませんね。
can110

2018/11/06 23:18 編集

コード詳細確認してません(oot_xmlってナニモノ?)が、list化してないとlenでは要素数は分からないのでは?
BURI55

2018/11/06 23:31

if len(list(root_xml)) > 1 else""ですね。失礼しました。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問