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

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

新規登録して質問してみよう
ただいま回答率
85.50%
Python 3.x

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

Q&A

解決済

1回答

1354閲覧

Pythonでマストドンからテキストデータを引き出して、外部プログラムに渡すプログラムを作っています。AttributeErrorに悩まされています。

BURI55

総合スコア25

Python 3.x

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

GitHub

GitHubは、Gitバージョン管理システムを利用したソフトウェア開発向けの共有ウェブサービスです。GitHub商用プランおよびオープンソースプロジェクト向けの無料アカウントを提供しています。

Python

Pythonは、コードの読みやすさが特徴的なプログラミング言語の1つです。 強い型付け、動的型付けに対応しており、後方互換性がないバージョン2系とバージョン3系が使用されています。 商用製品の開発にも無料で使用でき、OSだけでなく仮想環境にも対応。Unicodeによる文字列操作をサポートしているため、日本語処理も標準で可能です。

0グッド

0クリップ

投稿2018/11/06 04:44

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'

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

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

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

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

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

guest

回答1

0

ベストアンサー

Mastodon.pyのドキュメントを読む限り、確かにlocal_streamというメソッドは存在しません。(stream_localならありますね。。)

推測ですが、当該GitHubリポジトリの最終更新は11か月前ですので、Mastodon.pyの更新に追いついていないだけではないでしょうか。

投稿2018/11/06 05:23

kazto

総合スコア7196

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

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

BURI55

2018/11/06 05:41

ありがとうございます。Mastodon.pyのドキュメントで確認しました。95行をself.mastodon.stream_local(self)に書き換えてみましたが、反映されないようです。
BURI55

2018/11/06 05:56

ミスでした。アトリビュートエラーは出なくなりました。
kazto

2018/11/06 05:57

反映されない、とは具体的にどのようになりますか?エラーログは出ますか?
kazto

2018/11/06 05:57

おっと入れ違いでした
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問