Pythonでマストドンからテキストデータを引き出して、外部プログラムに渡すプログラムを作っています このGithubのプログラムを動かそうとしていますが、AttributeErrorに悩まされています。調べたところ、タイプミスが原因とのことですが、
Githubをクローンしたので、タイプミスは考えにくいです。どうしたら動くでしょうか?
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.local_stream(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.local_stream(self) 8AttributeError: 'Mastodon' object has no attribute 'local_stream'
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/06 05:41
2018/11/06 05:56
2018/11/06 05:57
2018/11/06 05:57