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')
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/06 22:38
2018/11/06 22:40 編集
2018/11/06 22:48
2018/11/06 22:51 編集
2018/11/06 22:57
2018/11/06 23:07
2018/11/06 23:14
2018/11/06 23:18 編集
2018/11/06 23:31