forked from evilsocket/pwnagotchi-plugins-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmastodon.py
76 lines (66 loc) · 2.73 KB
/
mastodon.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
import logging
import os
try:
from mastodon import Mastodon
except ImportError:
logging.error('[mastodon] Could not import python library.')
from pwnagotchi.voice import Voice
import pwnagotchi.plugins as plugins
class MastodonStatus(plugins.Plugin):
__author__ = '[email protected]'
__version__ = '1.0.0'
__license__ = 'GPL3'
__description__ = 'Periodically post status updates. Based on twitter plugin by evilsocket'
def on_loaded(self):
logging.info("[mastodon] Plugin loaded.")
# Called when there's available internet
def on_internet_available(self, agent):
config = agent.config()
display = agent.view()
last_session = agent.last_session
api_base_url = self.options['instance_url']
email = self.options['email']
password = self.options['password']
visibility = self.options['visibility']
client_cred = '/root/.mastodon.client.secret'
user_cred = '/root/.mastodon.user.secret'
if last_session.is_new() and last_session.handshakes > 0:
logging.info("[mastodon] Detected internet and new activity: time to post!")
if not os.path.isfile(user_cred) or not os.path.isfile(client_cred):
# Runs only if there are any missing credential files
Mastodon.create_app(
config['main']['name'],
api_base_url=api_base_url,
to_file=client_cred
)
picture = '/root/pwnagotchi.png'
display.on_manual_mode(last_session)
display.image().save(picture, 'png')
display.update(force=True)
try:
logging.info('[mastodon] Connecting to Mastodon API...')
mastodon = Mastodon(
client_id=client_cred,
api_base_url=api_base_url
)
mastodon.log_in(
email,
password,
to_file=user_cred
)
mastodon = Mastodon(
access_token=user_cred,
api_base_url=api_base_url
)
message = Voice(lang=config['main']['lang']).on_last_session_tweet(last_session)
mastodon.status_post(
message,
media_ids=mastodon.media_post(picture),
visibility=visibility
)
last_session.save_session_id()
logging.info(f"[mastodon] Posted: {message}")
display.set('status', 'Posted!')
display.update(force=True)
except Exception as e:
logging.exception(f"[mastodon] Error while posting: {e}")