forked from joyeusenoelle/masto-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
73 lines (55 loc) · 2.71 KB
/
bot.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
# -*- coding: utf-8 -*-
import argparse
import sys
import random
import time
from mastodon import Mastodon
# parse command line arguments
argument_parser = argparse.ArgumentParser(description="Simple Bot for the Mastodon social network")
argument_parser.add_argument("-cc", "--client-credentials", help="filename the client credentials should be stored to", type=str, default=".masto-bot.clientcred.secret")
argument_parser.add_argument("-uc", "--user-credentials", help="filename the user credentials should be stored to", type=str, default=".masto-bot.usercred.secret")
argument_parser.add_argument("-i", "--input-file", default="quotes.txt", help="The file the quotes are read from. Quotes are separated by an empty line, followed by three minus signs, followed by an empty line.")
argument_parser.add_argument("-u", "--username", help="fully qualified username", type=str, required=True)
argument_parser.add_argument("-t", "--time-interval", help="Time (in seconds) to wait in between toots.", type=int, default=6*60*60)
argument_parser.add_argument("-v", "--visibility", help="Visibility scope of toots.", choices=('public', 'unlisted', 'private'), default='unlisted')
args = argument_parser.parse_args()
try:
(username, hostname) = args.username.split("@")
except ValueError:
sys.exit("username must be of format username@servername, e.g. <[email protected]>")
except:
sys.exit("unexpected error while parsing username")
# establish connection to Mastodon server
mastodon_connection = Mastodon(client_id = args.client_credentials, access_token = args.user_credentials, api_base_url = "https://%s"%(hostname,))
# toot stuff
def not_too_long(toot):
if len(toot) > 500:
return False
elif len(toot) == 0:
return False
else:
return True
# read file that is given as first command line argument,
# split it along empty lines,
# throw out texts that are longer than 500 characters,
# and have it be a list (filter returns an interable, which is not compatible with random.choice(),
## because it as no pre-known length)
toot_list = [ x for x in filter(not_too_long, open(args.input_file).read().split('\n---\n')) ]
active = True
while active:
try:
toot_text = random.choice(toot_list)
except:
print('Could not generate toot_text. Check your text file and try again.')
active = False
break
try:
status = mastodon_connection.status_post(status = toot_text, visibility = args.visibility)
print('Awoooooooo!')
print('This toot:\n{}\n...was posted at {}.'.format(toot_text, status['created_at']))
print()
except:
print('Failed to toot. :(')
active = False
break
time.sleep(args.time_interval)