-
Notifications
You must be signed in to change notification settings - Fork 5
/
federabot.py
152 lines (130 loc) · 4.96 KB
/
federabot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
from common import get_api
from common import list_append
from common import list_read
from common import list_write
from datetime import datetime, timedelta
def get_message(user_domain):
return "¡Hola! Veo que es tu primera vez en Mastodon, ¡te doy la bienvenida!\n\nTe recomiendo que empieces escribiendo una publicación con la etiqueta #presentación y tus intereses para darte a conocer.\n\n¡Espero que tengas un buen comienzo! Si necesitas ayuda, ¡cuenta conmigo!"
excluded_domains = [
'masto.es',
# Relay tkz.one
'mst.universoalterno.es',
'masto.friki.lol',
'mastodon.com.py',
'comunidad.nvda.es',
'mast.lat',
'viajes.social',
'ferrocarril.net',
'tkz.one',
'mastorol.es',
'shrimply.social',
'mstdn.jmiguel.eu',
'mastorock.com',
'frikiverse.zone',
'41020.social',
'tuiter.rocks',
'mastorock.com',
'meetiko.org',
'mastodon.cr',
'fedi.lat',
# Relay nobigtech.es
'sindicato.social',
'mastodon.uy',
'red.niboe.info',
'nobigtech.es',
'loa.masto.host',
'bizkaia.social',
'mstdn.mx',
'federa.social',
# Non-spanish accounts >:(
'sportsbots.xyz',
'press.coop',
'fedibird.com',
# Mirrors
'respublicae.eu',
'bots.fedi.cr '
]
bot_name = 'federabot'
api_mastoes = get_api('masto.es', 'rober')
following = list_read(bot_name)
date_recent = datetime.today() - timedelta(days=30)
follows_limited = list_read(bot_name + '_follows_limited')
dms_limited = list_read(bot_name + '_messages_limited')
list_write(bot_name + "_follows_limited", [])
list_write(bot_name + "_messages_limited", [])
def try_follow(user_id):
try:
api_mastoes.account_follow(user_id)
except:
list_append(bot_name + '_follows_limited', str(user_id))
print("Fail to follow. Will retry next time")
def try_dm(username, user_domain):
try:
api_mastoes.status_post("@" + username + " " + get_message(user_domain), visibility="direct")
print("Welcome new user: " + username)
except:
list_append(bot_name + '_messages_limited', username)
print("Fail to welcome new user: " + username + ". Will retry next time")
def check_follows():
notifications = api_mastoes.notifications(types=["follow"])
for n in notifications:
username = n['account']['acct']
user_domain = username.split("@")[1] if "@" in username else "masto.es"
date_created = n['account']['created_at'].replace(tzinfo=None)
if username not in following:
print("Following: " + username)
try_follow(n['account']['id'])
following.append(username)
list_append(bot_name, username)
if date_created > date_recent and user_domain == 'mastodon.social':
try_dm(username, user_domain)
def check_timeline(domain, timeline_name = 'public', api_external=None):
if api_external is None:
api_external = get_api(domain)
last_id = list_read(bot_name + "_last_id_" + domain)[0]
timeline = api_external.timeline(
timeline=timeline_name,
since_id=last_id,
remote=(True if timeline_name == 'public' else False)
)
for post in timeline:
if timeline_name == 'local':
username = post['account']['acct'] + "@" + domain
user_domain = domain
else:
username = post['account']['acct']
user_domain = username.split("@")[1]
if (
post['language'] == 'es'
and not "nobot" in post['account']['note']
and user_domain not in excluded_domains
and username not in following
):
date_created = post['account']['created_at'].replace(tzinfo=None)
if date_created > date_recent and timeline_name == 'local' and user_domain == 'mastodon.social':
try_dm(username, user_domain)
print("Following: " + username)
user = api_mastoes.search_v2("@" + username + " ", result_type="accounts")["accounts"][0]
# Retrieve the post, it could be the first
api_mastoes.search_v2(post['url'], result_type="posts")
following.append(username)
list_append(bot_name, username)
try_follow(user['id'])
if len(timeline) > 0:
list_write(bot_name + "_last_id_" + domain, [timeline[0]['id']])
print('\nChecking previous attempts...')
for user_id in follows_limited:
try_follow(user_id)
for username in dms_limited:
user_domain = username.split("@")[1]
try_dm(username, user_domain)
print('\nChecking follows...')
check_follows()
api=get_api('mastodon.social', bot_name + "_mastodon_social")
print('\nChecking mastodon.social local TL')
check_timeline('mastodon.social', 'local', api_external=api)
print('\nChecking mastodon.social federated TL')
check_timeline('mastodon.social', 'public', api_external=api)
print('\nChecking masto.es federated TL')
api=get_api('masto.es', bot_name)
check_timeline('masto.es', api_external=api)