Pythonでマストドンからテキストデータを引き出して、外部プログラムに渡すプログラムを作っています。AttributeErrorに悩まされています。
解決済
回答 1
投稿
- 評価
- クリップ 0
- VIEW 683
Pythonでマストドンからテキストデータを引き出して、外部プログラムに渡すプログラムを作っています このGithubのプログラムを動かそうとしていますが、AttributeErrorに悩まされています。調べたところ、タイプミスが原因とのことですが、
Githubをクローンしたので、タイプミスは考えにくいです。どうしたら動くでしょうか?
import datetime
import os
import subprocess
import xml.etree.ElementTree as ET
from getpass import getpass
from typing import NamedTuple
from mastodon import Mastodon, StreamListener
from config import generate_from_file
from utility import strip_html_tags
class Comment(NamedTuple):
user: str
html_text: str
unix_time: int
icon_url: str
@property
def text(self) -> str:
return strip_html_tags(self.html_text)
def construct_execute_command(execute_command_format: str, comment: Comment):
return execute_command_format.format(
user=comment.user,
text=comment.text,
unix_time=comment.unix_time,
)
class Runner(StreamListener):
def __init__(self, config):
self.config = config
self.highlight_list = self.config.highlight
self.path_xml = self.config.path_xml_html5_comment_generator
self.mastodon = Mastodon(
client_id='app.secret',
access_token='user.secret',
api_base_url=config.api_base_url,
)
def make_comment(self, toot):
user = toot['account']['display_name']
icon_url = toot['account']['avatar']
text = toot['content']
ok = False
if self.highlight_list is None or len(self.highlight_list) == 0:
ok = True
for highlight in self.highlight_list:
if highlight in text:
ok = True
if not ok:
return None
unix_time = int(datetime.datetime.now().timestamp())
return Comment(user, text, unix_time, icon_url)
def make_xml_element(self, root_xml, comment: Comment):
last_no = int(list(root_xml)[-1].attrib['no'])
attr = dict(
no=str(last_no + 1),
time=str(comment.unix_time + 3),
handle=comment.user,
icon_url=comment.icon_url,
)
element = ET.Element('comment', attrib=attr)
element.text = comment.text
return element
def on_update(self, toot):
comment = self.make_comment(toot)
if comment is None:
return
# execute command
for f in self.config.execute_command:
subprocess.run(construct_execute_command(f, comment).split(' '))
# get last number of xml
tree = ET.parse(self.path_xml)
root_xml = tree.getroot()
element = self.make_xml_element(root_xml, comment)
# add xml
root_xml.append(element)
tree.write(self.path_xml, encoding='utf-8')
def run(self):
print('running...')
self.mastodon.local_stream(self)
path_config = "./config.json"
config = generate_from_file(path_config)
assert config.api_base_url is None or len(config.api_base_url), "config.jsonのapi_base_urlを指定してください。"
if not os.path.exists('./app.secret'):
Mastodon.create_app(
'nicolive-mastodon',
api_base_url=config.api_base_url,
to_file='app.secret',
)
if not os.path.exists('./user.secret'):
username = input("ユーザー名(e-mailアドレス)を入力してください... ")
password = getpass("パスワードを入力してください... ")
mastodon = Mastodon(
client_id='app.secret',
api_base_url=config.api_base_url,
)
mastodon.log_in(
username,
password,
to_file='user.secret'
)
runner = Runner(
config=config,
)
runner.run()
エラーメッセージは以下の通りです。
C:\Users\user\Desktop\nicolive-mastodon-1.1.2>python run.py
running...
Traceback (most recent call last):
File "run.py", line 126, in <module>
runner.run()
File "run.py", line 95, in run
self.mastodon.local_stream(self)
AttributeError: 'Mastodon' object has no attribute 'local_stream'
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
+1
Mastodon.pyのドキュメントを読む限り、確かにlocal_stream
というメソッドは存在しません。(stream_localならありますね。。)
推測ですが、当該GitHubリポジトリの最終更新は11か月前ですので、Mastodon.pyの更新に追いついていないだけではないでしょうか。
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.09%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2018/11/06 14:41
2018/11/06 14:56
2018/11/06 14:57
2018/11/06 14:57