Pythonでマストドンの読み上げプログラムを作っています。以下のコードを実行したところ、今度はパーミッションエラーに悩まされています。これはウインドウズ特有のエラーらしく、管理者権限でコマンドプロンプトを実行したり、Pythonのインストールフォルダなどのアクセス許可を確認したりしましたが、だめでした。UACは下げてあります。ファイル属性を読み取り専用から解除しました。
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() 127
実行結果とエラーメッセージ
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) 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 82, in on_update 20 subprocess.run(construct_execute_command(f, comment).split(' ')) 21 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 466, in run 22 with Popen(*popenargs, **kwargs) as process: 23 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 769, in __init__ 24 restore_signals, start_new_session) 25 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1172, in _execute_child 26 startupinfo) 27PermissionError: [WinError 5] アクセスが拒否されました。
プリントデバックしています。
cmd
1C:\Users\user\Desktop\nicolive-mastodon-1.1.2>python run.py 2running... 3['.'] 4Traceback (most recent call last): 5 File "run.py", line 127, in <module> 6 runner.run() 7 File "run.py", line 96, in run 8 self.mastodon.stream_local(self) 9 File "<decorator-gen-85>", line 2, in stream_local 10 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 77, in wrapper 11 return function(self, *args, **kwargs) 12 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 2001, in stream_local 13 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) 14 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\Mastodon.py", line 2393, in __stream 15 listener.handle_stream(r) 16 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\streaming.py", line 72, in handle_stream 17 self._dispatch(event) 18 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\site-packages\mastodon\streaming.py", line 145, in _dispatch 19 handler(payload) 20 File "run.py", line 83, in on_update 21 subprocess.run(construct_execute_command(f, comment).split(' ')) 22 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 466, in run 23 with Popen(*popenargs, **kwargs) as process: 24 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 769, in __init__ 25 restore_signals, start_new_session) 26 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\subprocess.py", line 1172, in _execute_child 27 startupinfo) 28PermissionError: [WinError 5] アクセスが拒否されました。
config.jsonをいじったところ、だいぶエラーが減ったようです。
cmd
1C:\Users\user\Desktop\nicolive-mastodon-1.1.2>python run.py 2Traceback (most recent call last): 3 File "run.py", line 100, in <module> 4 config = generate_from_file(path_config) 5 File "C:\Users\user\Desktop\nicolive-mastodon-1.1.2\config\config.py", line 18, in generate_from_file 6 info = json.load(open(path, encoding='utf-8')) 7 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 296, in load 8 parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) 9 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 348, in loads 10 return _default_decoder.decode(s) 11 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 337, in decode 12 obj, end = self.raw_decode(s, idx=_w(s, 0).end()) 13 File "C:\Users\user\AppData\Local\Programs\Python\Python37\lib\json\decoder.py", line 355, in raw_decode 14 raise JSONDecodeError("Expecting value", s, err.value) from None 15json.decoder.JSONDecodeError: Expecting value: line 4 column 23 (char 104)
subprocess.run(construct_execute_command(f, comment).split(' '))の行でエラーが出ているらしいので、すぐ上の行にprint(construct_execute_command(f, comment).split(' '))とか入れて(インデントを揃えて)、どんなコマンドが実行されようとしているのか確認してみてください
削除
削除
['.']を実行しようとしたのが原因ですね。カレントディレクトリに実行権限はありません
self.config.execute_commandが怪しいらしく、さかのぼっていくとけっきょくconfig.jsonが何かしら間違っているのでは? という気がします
>config.jsonをいじったところ、だいぶエラーが減ったようです。 今まで発生していたエラーが出る前に、jsonのパースで落ちてますからね・・・
回答1件
あなたの回答
tips
プレビュー