質問編集履歴
1
書式を少し改善しました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
**twitterで自分の固定ツイートをリツイートした人に自動でDmを送るpythonのプログラムを作ったのですが**
|
2
2
|
|
3
3
|
|
4
|
+
```python
|
4
5
|
import json
|
5
6
|
import time
|
6
7
|
import random
|
7
8
|
from requests_oauthlib import OAuth1Session
|
8
9
|
import tweepy
|
9
|
-
|
10
10
|
|
11
11
|
api_key = ‘——‘
|
12
12
|
api_secret = ‘—‘
|
@@ -19,8 +19,8 @@
|
|
19
19
|
auth.set_access_token(access_key, access_secret)
|
20
20
|
api = tweepy.API(auth)
|
21
21
|
|
22
|
-
random_min_time = 300
|
22
|
+
random_min_time = 300 # sleepでとり得る最小値
|
23
|
-
random_max_time = 400
|
23
|
+
random_max_time = 400 # sleepでとり得る最大値
|
24
24
|
|
25
25
|
endpoint = 'https://api.twitter.com/2/users?ids=-----
|
26
26
|
|
@@ -36,26 +36,27 @@
|
|
36
36
|
|
37
37
|
tweet_id = dic_me['data'][0]['pinned_tweet_id']
|
38
38
|
|
39
|
-
endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' +
|
39
|
+
endpoint = 'https://api.twitter.com/1.1/statuses/retweeters/ids.json?id=' +
|
40
|
-
|
40
|
+
tweet_id + '&stringify_ids=true'
|
41
41
|
|
42
42
|
res = twitter.get(endpoint)
|
43
43
|
|
44
44
|
dic_users = json.loads(res.text)
|
45
45
|
|
46
46
|
for num in range(len(dic_users['ids'])):
|
47
|
-
|
47
|
+
account_name = dic_users['ids'][num]
|
48
|
-
|
48
|
+
user_id = api.user_timeline(account_name, count=1)[0].user.id
|
49
|
-
|
49
|
+
try:
|
50
|
-
|
50
|
+
api.send_direct_message(recipient_id=user_id, text=‘hello)
|
51
|
-
|
51
|
+
print('メッセージを送信しました:', account_name)
|
52
|
-
|
52
|
+
except Exception as e:
|
53
|
-
|
53
|
+
print("Error!!!", e)
|
54
|
-
|
54
|
+
except StopIteration:
|
55
|
-
|
55
|
+
continue
|
56
|
-
|
56
|
+
sleep_random_time = random.randint(random_min_time, random_max_time)
|
57
|
-
|
57
|
+
print(account_name, ': ', ' 次のDM送信まで', sleep_random_time, '秒待ちます')
|
58
|
-
|
58
|
+
time.sleep(sleep_random_time)**
|
59
|
+
```
|
59
60
|
|
60
61
|
Traceback (most recent call last):
|
61
62
|
File "automackforalpha.py", line 55, in <module>
|