-
Notifications
You must be signed in to change notification settings - Fork 0
/
tomoyodon.py
142 lines (115 loc) · 3.5 KB
/
tomoyodon.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
# -*- coding: utf-8 -*-
import io
import sys
import time
from argparse import ArgumentParser
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import ConfigParser
from logging import basicConfig, getLogger, INFO
from mastodon import Mastodon
from retry import retry
from watchdog.events import PatternMatchingEventHandler
from watchdog.observers import Observer
parser = ArgumentParser(
prog="TOMOYODon"
)
group = parser.add_mutually_exclusive_group()
group.add_argument(
'-t', '--token',
action='store_true',
help='get access token'
)
basicConfig(level=INFO)
logger = getLogger(__name__)
def get_access_token():
config = ConfigParser()
config.readfp(io.open('token.ini', 'r', encoding='utf_8_sig'))
section = 'Token'
api_base_url = config.get(section, 'api_base_url')
username = config.get(section, 'username')
password = config.get(section, 'password')
client_id = config.get(section, 'client_id')
access_token = config.get(section, 'access_token')
scopes = ['read', 'write']
Mastodon.create_app(
'TOMOYODon',
scopes=scopes,
api_base_url=api_base_url,
to_file=client_id
)
mastodon = Mastodon(
client_id=client_id,
api_base_url=api_base_url
)
mastodon.log_in(
username=username,
password=password,
scopes=scopes,
to_file=access_token
)
class ChangeHandler(PatternMatchingEventHandler):
def __init__(self, mastodon, toot_str, sensitive, visibility,
patterns=None, ignore_patterns=None,
ignore_directories=False, case_sensitive=False):
super(ChangeHandler, self).__init__(
patterns, ignore_patterns, ignore_directories, case_sensitive
)
self.mastodon = mastodon
self.toot_str = toot_str
self.sensitive = sensitive
self.visibility = visibility
def on_created(self, event):
logger.info(event.src_path)
media_ids = []
media = self.media_post(event.src_path)
media_ids.append(media)
self.mastodon.status_post(
self.toot_str,
media_ids=media_ids,
sensitive=self.sensitive,
visibility=self.visibility
)
@retry(IOError, delay=1)
def media_post(self, path):
return self.mastodon.media_post(path)
def toot():
config = ConfigParser()
config.readfp(io.open('tomoyodon.ini', 'r', encoding='utf_8_sig'))
section_api = 'API'
mastodon = Mastodon(
client_id=config.get(section_api, 'client_id'),
access_token=config.get(section_api, 'access_token'),
api_base_url=config.get(section_api, 'api_base_url')
)
target_path = config.get('Path', 'target')
section_toot = 'TOOT'
toot_str = config.get(section_toot, 'toot_str')
sensitive = config.getboolean(section_toot, 'sensitive')
visibility = config.get(section_toot, 'visibility')
event_handler = ChangeHandler(
mastodon,
toot_str,
sensitive,
visibility,
ignore_directories=True
)
observer = Observer()
observer.schedule(event_handler, target_path, recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
def main():
args = parser.parse_args(sys.argv[1:])
if args.token:
get_access_token()
sys.exit(0)
else:
toot()
if __name__ == '__main__':
main()