Repository navigation
Expand file tree
/
Copy pathwitchesGate.py
More file actions
85 lines (62 loc) · 2.47 KB
/
Copy pathwitchesGate.py
File metadata and controls
85 lines (62 loc) · 2.47 KB
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
import os
import random
import argparse
from mastodon import Mastodon
def main():
_DATA_DIR = './data'
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--instance',
required=False, default='https://mastodon.social')
parser.add_argument('-l', '--login',
required=False, default='')
parser.add_argument('-p', '--password',
required=False, default='')
parser.add_argument('-c', '--client-cred',
required=False, default='./clientcred.txt')
parser.add_argument('-u', '--user-cred',
required=False, default='./usercred.txt')
parser.add_argument('-a', '--action',
required=False, choices=['open', 'close', 'rules'])
args = parser.parse_args()
if not os.path.isfile(args.client_cred):
Mastodon.create_app(
'witchesGate',
api_base_url=args.instance,
to_file=args.client_cred
)
if not os.path.isfile(args.user_cred):
mastodon = Mastodon(client_id=args.client_cred,
api_base_url=args.instance)
mastodon.log_in(args.login, args.password,
to_file=args.user_cred)
mastodon = Mastodon(client_id=args.client_cred,
access_token=args.user_cred,
api_base_url=args.instance)
def get_random_gif(directory):
search_path = os.path.join(_DATA_DIR, directory)
gif = random.choice(os.listdir(search_path))
return os.path.join(search_path, gif)
def post_status(content, media = None):
if media:
media = mastodon.media_post(media_file=media)
return mastodon.status_post(status=content, media_ids=[media],
visibility='public')
def open_the_gates():
gif = get_random_gif('open')
return post_status('#WitchesTown', media=gif)
def close_the_gates():
gif = get_random_gif('close')
return post_status('#witchesTown', media=gif)
def show_the_rules():
with open(os.path.join(_DATA_DIR, 'rules.txt'), 'r') as content_file:
content = content_file.read()
return post_status(content)
switcher = {
'open': open_the_gates,
'close': close_the_gates,
'rules': show_the_rules
}
func = switcher.get(args.action, lambda: None)
func()
if __name__ == '__main__':
main()