twitterで自分の固定ツイートをリツイートした人に自動でDmを送るpythonのプログラムを作ったのですが
python
1import json 2import time 3import random 4from requests_oauthlib import OAuth1Session 5import tweepy 6 7api_key = ‘——‘ 8api_secret = ‘—‘ 9access_key = ‘—‘ 10access_secret = ‘—‘ 11 12twitter = OAuth1Session(api_key, api_secret, access_key, access_secret) 13 14auth = tweepy.OAuthHandler(api_key, api_secret) 15auth.set_access_token(access_key, access_secret) 16api = tweepy.API(auth) 17 18random_min_time = 300 # sleepでとり得る最小値 19random_max_time = 400 # sleepでとり得る最大値 20 21endpoint = 'https://api.twitter.com/2/users?ids=----- 22 23params = {'expansions': 'pinned_tweet_id'} 24 25res = twitter.get(endpoint, params=params) 26 27dic_me = json.loads(res.text) 28 29print(dic_me['data'][0]['pinned_tweet_id']) 30 31print(dic_me['includes']['tweets'][0]['text']) 32 33tweet_id = dic_me['data'][0]['pinned_tweet_id'] 34 35endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' + 36tweet_id + '&stringify_ids=true' 37 38res = twitter.get(endpoint) 39 40dic_users = json.loads(res.text) 41 42for num in range(len(dic_users['ids'])): 43account_name = dic_users['ids'][num] 44user_id = api.user_timeline(account_name, count=1)[0].user.id 45try: 46api.send_direct_message(recipient_id=user_id, text=‘hello) 47print('メッセージを送信しました:', account_name) 48except Exception as e: 49print("Error!!!", e) 50except StopIteration: 51continue 52sleep_random_time = random.randint(random_min_time, random_max_time) 53print(account_name, ': ', ' 次のDM送信まで', sleep_random_time, '秒待ちます') 54time.sleep(sleep_random_time)**
Traceback (most recent call last):
File "automackforalpha.py", line 55, in <module>
user_id = api.user_timeline(account_name, count=1)[0].user.id
File "/usr/local/lib/python3.8/dist-packages/tweepy/api.py", line 33, in wrapper
return method(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/tweepy/api.py", line 46, in wrapper
return method(*args, **kwargs)
TypeError: user_timeline() takes 1 positional argument but 2 were given
というエラーが出て直りません
引数が違うらしいのでselfをつけてみても直りません
user_timeline の API ドキュメントを眺めてみると、
tweepy 4.1.0 documentation
https://docs.tweepy.org/en/v4.1.0/api.html#tweepy.API.user_timeline
positional parameter を受け付けない様です("1 positional argument" は self を指しています)。
なので、
user_id = api.user_timeline(screen_name=account_name, count=1)[0].user.id
を試してみて下さい。
このままではコードが読めないので、質問を編集し、</>(コードの挿入)ボタンを押し、出てくる’’’の枠の中にコードを貼り付けてください
@melianさんの回答の様に
変えてみたら
/usr/local/lib/python3.7/dist-packages/tweepy/binder.py in _call(*args, **kwargs)
251 return method
252 else:
--> 253 return method.execute()
254 finally:
255 method.session.close()
/usr/local/lib/python3.7/dist-packages/tweepy/binder.py in execute(self)
232 raise RateLimitError(error_msg, resp)
233 else:
--> 234 raise TweepError(error_msg, resp, api_code=api_error_code)
235
236 # Parse the response payload
TweepError: [{'code': 34, 'message': 'Sorry, that page does not exist.'}]
というのが出てきて実行できなくなっていました
"that page does not exist." と表示されていますので、その account_name に対応するユーザがいない、ということかと思います。
あなたの回答
tips
プレビュー