Commit 7dd28075 authored by M33's avatar M33 💬
Browse files

Added token app authentication method

parent cfd431b1
# radiobot
Radiobot get solar data images from popular hamradio websites and toots to Mastodon
##Radiobot get solar data images from popular hamradio websites and toots to Mastodon
###Usage
python3 radiobot.py [full path to config file]
Example
python3 radiobot.py /home/bot/radiobot@my_bot@my_instance.conf
###Configuration file
The configuration file should be placed in a safe directory (not world or group readable), and contains the instance and authentication credentials.
You must complete the config file and provide authentication credentials to your desired mastodon account.
A sample config file 'radiobot@my_bot@my_instance.conf' is provided as a starting point.
###Authentication methods
This app support both email and application token authentication.
You are strongly encouraged to create an application with dedicated app id, secret and access token for this bot.
See the "Development" and "New application" pannel in your mastodon account settings.
......@@ -2,10 +2,14 @@ import os.path
import sys
import re
from time import sleep
from mastodon import Mastodon
import requests
# Program configuration
auth_file = "radiobot.auth"
auth_session = auth_file+'.session'
mastodon_api = None
# Set to 1 to get some messages, 0 for error messages only
debug=1
......@@ -16,40 +20,65 @@ posting_delay=1
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"
if len(sys.argv) < 3:
print("Usage: radiobot.py [instance (without http://)] [user] [password]")
# Program logic below this line
if len(sys.argv) < 2:
print("Usage: radiobot.py [config file]")
sys.exit(1)
instance=sys.argv[1]
mastodon_email_account=sys.argv[2]
passwd=sys.argv[3]
auth_file=sys.argv[1]
auth_session=auth_file+'.secret'
mastodon_api = None
instance_file='/var/run/lock/'+instance+'.secret'
# 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)
# Create application if it does not exist
if debug: print("Creating mastodon client to https://" + instance + " " + instance_file)
try:
Mastodon.create_app('radiobot', api_base_url='https://'+instance, to_file=instance_file)
except:
print('ERROR: Failed to create app on instance '+instance)
sys.exit(1)
# Find parameter in file
with open( file_path ) as f:
for line in f:
if line.startswith( parameter ):
return line.replace(parameter + ":", "").strip()
try:
if debug: print("Trying to connect with ",instance_file," to ",'https://'+instance," ...", end='')
mastodon_api = Mastodon(client_id=instance_file,api_base_url='https://'+instance)
if debug: print(" ok.")
except:
print("ERROR: Can't connect to Mastodon instance")
sys.exit(1)
# Cannot find parameter, exit
print(file_path + " Missing parameter %s "%parameter)
sys.exit(0)
# end get_config()
if debug: print("Login with email ",mastodon_email_account," ...", end='')
# 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='')
try:
mastodon_api.log_in(mastodon_email_account,passwd,to_file=instance_file)
if debug: print(" ok.")
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)
except:
print("ERROR: First Login Failed !")
print("ERROR: Can't connect to Mastodon instance")
sys.exit(1)
if debug: print("ok")
# get the solar image
if debug: print("Getting solar data...",end='')
......@@ -61,7 +90,7 @@ try:
toot_media.append(media_posted)
if debug: print("done.")
except:
print("ERROR: Can't get media !")
print("ERROR: Authentication error, or error fetching the media !")
if debug: print("Tooting...",end='')
try:
......@@ -69,3 +98,6 @@ try:
if debug: print(" done.")
except:
print("ERROR: Can't toot !")
sys.exit(0)
# end
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment