From 7dd28075719424532398666defa9645505e15cd7 Mon Sep 17 00:00:00 2001 From: m33m33 <m33@tok715.net> Date: Mon, 17 Aug 2020 13:17:19 +0200 Subject: [PATCH] Added token app authentication method --- README.md | 18 ++++++++++- radiobot.py | 86 ++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 76 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 086d775..0ec9d30 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,18 @@ # 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. diff --git a/radiobot.py b/radiobot.py index 522ef8c..8922691 100755 --- a/radiobot.py +++ b/radiobot.py @@ -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 -- GitLab