radiobot.py 3.69 KB
Newer Older
M33's avatar
First!  
M33 committed
1
2
3
4
5
6
7
import os.path
import sys
import re
from time import sleep
from mastodon import Mastodon
import requests

M33's avatar
M33 committed
8
9
10
11
12
# Program configuration
auth_file = "radiobot.auth"
auth_session = auth_file+'.session'
mastodon_api = None

M33's avatar
First!  
M33 committed
13
14
15
16
17
18
19
20
21
22
# Set to 1 to get some messages, 0 for error messages only
debug=1

# Posting delay (s), wait between mastodon posts, reduces the "burst" effect on timoeline, and instance workload if you hit rate limiters
posting_delay=1

# Solar image
solar_pics=["http://www.hamqsl.com/solarmap.php", "http://www.hamqsl.com/solar101vhfper.php", "http://www.spacew.com/www/realtime.gif"]
toot_body="Solar data from http://www.hamqsl.com http://www.spacew.com\n\n#HamRadio #SolarData #Propagation #AmateurRadio #CBradio"

M33's avatar
M33 committed
23
24
25
26
# Program logic below this line

if len(sys.argv) < 2:
    print("Usage: radiobot.py [config file]")
M33's avatar
First!  
M33 committed
27
28
    sys.exit(1)

M33's avatar
M33 committed
29
30
auth_file=sys.argv[1]
auth_session=auth_file+'.secret'
M33's avatar
First!  
M33 committed
31

M33's avatar
M33 committed
32
33
34
35
36
37
# Returns the parameter from the specified file
def get_config(parameter, file_path):
    # Check if secrets file exists
    if not os.path.isfile(file_path):
        print("ERROR: Config file (%s) not found"%file_path)
        sys.exit(0)
M33's avatar
First!  
M33 committed
38

M33's avatar
M33 committed
39
40
41
42
43
    # Find parameter in file
    with open( file_path ) as f:
        for line in f:
            if line.startswith( parameter ):
                return line.replace(parameter + ":", "").strip()
M33's avatar
First!  
M33 committed
44

M33's avatar
M33 committed
45
46
47
48
    # Cannot find parameter, exit
    print(file_path + "  Missing parameter %s "%parameter)
    sys.exit(0)
# end get_config()
M33's avatar
First!  
M33 committed
49

M33's avatar
M33 committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# Look for credentials in the coniguration file
auth_type = get_config("auth_type",auth_file)
if "token" in auth_type:
    # We are using an application token (developer options in the mastodon account, new app...)
    app_client_id = get_config("app_client_id",auth_file)
    app_client_secret = get_config("app_client_secret",auth_file)
    app_access_token  = get_config("app_access_token",auth_file)
else:
    if "email" in auth_type:
        # We are using theuser account credential
        mastodon_email_account = get_config("mastodon_email_account",auth_file)
        mastodon_email_password  = get_config("mastodon_email_password",auth_file)
    else:
        print("ERROR: Check the configuration file, no authentication method found")
        sys.exit(1)

instance = get_config("instance",auth_file)

if debug:
    if "token" in auth_type: print("Trying to connect with app client id:",app_client_id," on ",instance,"...", end='')
    else: print("Trying to login with email ",mastodon_email_account," on ",instance,"...", end='')
M33's avatar
First!  
M33 committed
71
try:
M33's avatar
M33 committed
72
73
74
75
76
77
    if "token" in auth_type:
        mastodon_api = Mastodon(client_id=app_client_id, client_secret=app_client_secret, access_token=app_access_token, api_base_url='https://'+instance)
    else:
        Mastodon.create_app('radiobot', api_base_url='https://'+instance, to_file=auth_session)
        mastodon_api = Mastodon(client_id=auth_session, api_base_url='https://'+instance)
        mastodon_api.log_in(mastodon_email_account, mastodon_email_password, to_file=auth_session)
M33's avatar
First!  
M33 committed
78
except:
M33's avatar
M33 committed
79
    print("ERROR: Can't connect to Mastodon instance")
M33's avatar
First!  
M33 committed
80
    sys.exit(1)
M33's avatar
M33 committed
81
if debug: print("ok")
M33's avatar
First!  
M33 committed
82
83
84
85
86
87
88
89
90
91
92

# get the solar image
if debug: print("Getting solar data...",end='')
toot_media = []
try:
    for solar in solar_pics:
        media = requests.get(solar)
        media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type'))
        toot_media.append(media_posted)
    if debug: print("done.")
except:
M33's avatar
M33 committed
93
    print("ERROR: Authentication error, or error fetching the media !")
M33's avatar
First!  
M33 committed
94
95
96
97
98
99
100

if debug: print("Tooting...",end='')
try:
    toot = mastodon_api.status_post(toot_body, in_reply_to_id=None, media_ids=toot_media,sensitive=False,visibility='public',spoiler_text=None)
    if debug: print(" done.")
except:
    print("ERROR: Can't toot !")
M33's avatar
M33 committed
101
102
103

sys.exit(0)
# end