-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmicrotrace.py
executable file
·126 lines (104 loc) · 4.2 KB
/
microtrace.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
#!/usr/bin/env python3
# Copyright 2018-2022 Bryant Durrell
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
# IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
# HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import argparse
import json
import tomli
import tracery
import tweepy
from mastodon import Mastodon, MastodonError
from tracery.modifiers import base_english
def toot(config, text):
try:
access_token = config["mastodon"]["access_token"]
base_url = config["mastodon"]["base_url"]
except KeyError as e:
print(f"Key {e} not found!")
return False
# Note: this isn't where exceptions are thrown so no error handling here
mastodon = Mastodon(access_token=access_token, api_base_url=base_url)
try:
mastodon.toot(text)
except MastodonError as e:
print(f"Error posting: {e}")
return False
return True
def tweet(config, text):
try:
consumer_key = config["twitter"]["consumer_key"]
consumer_secret = config["twitter"]["consumer_secret"]
access_token = config["twitter"]["access_token"]
access_token_secret = config["twitter"]["access_token_secret"]
except KeyError as e:
print(f"Key {e} not found!")
try:
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
except tweepy.errors.TweepyException as e:
print(f"Failed authorization: {e}")
return False
try:
api.update_status(text)
except tweepy.errors.TweepyException as e:
print(f"Error posting: {e}")
return False
def generate():
# Leaving this as argparse rather than migrating to click because I want
# to keep it simple
parser = argparse.ArgumentParser(description="Tracery-based microblogging bot")
parser.add_argument("--grammar", required=True, help="JSON grammar")
parser.add_argument(
"--maxlen", default=280, type=int, help="Maximum message length"
)
parser.add_argument("--print", help="Print", action="store_true")
parser.add_argument("--toot", help="Toot", action="store_true")
parser.add_argument("--tweet", help="Tweet", action="store_true")
parser.add_argument("--config", help="Config file", required=True)
args = parser.parse_args()
with open(args.config, mode="rb") as config_file:
config = tomli.load(config_file)
with open(args.grammar) as data_file:
rules = json.load(data_file)
grammar = tracery.Grammar(rules)
grammar.add_modifiers(base_english)
text = ""
while len(text) == 0:
text = grammar.flatten("#origin#")
if len(text) > args.maxlen:
text = ""
text = " ".join(text.split())
if args.print:
print(text)
if args.toot:
toot(config, text)
if args.tweet:
tweet(config, text)
if __name__ == "__main__":
generate()