From dc6ea96d2251892e390fe0d2c7567b4667498a21 Mon Sep 17 00:00:00 2001 From: m33m33 <m33@tok715.net> Date: Sat, 15 Aug 2020 21:25:04 +0200 Subject: [PATCH] Add debug on/off flag --- tootbot.py | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/tootbot.py b/tootbot.py index 9442eff..1bc7b79 100755 --- a/tootbot.py +++ b/tootbot.py @@ -9,6 +9,9 @@ import feedparser from mastodon import Mastodon import requests +# Set to 1 to get some messages, 0 for error messages only +debug=1 + if len(sys.argv) < 8: print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance max_days footer_tags delay mastodon_account_email") # noqa sys.exit(1) @@ -41,15 +44,15 @@ passwd = sys.argv[3] mastodon_api = None if source[:4] == 'http': - print("Parsing source ",source," ...", end='') + if debug: print("Parsing source ",source," ...", end='') d = feedparser.parse(source) twitter = None - print("ok.") + if debug: print(" ok.") else: d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+source) twitter = source -print("Processing", end='') +if debug: print("Processing", end='') # Cut the source URL to avoid reposts source=source.split("/search/")[0] @@ -72,31 +75,32 @@ for t in reversed(d.entries): sys.exit(1) try: - print("Trying to connect with ",instance+'.secret'," to ",'https://'+instance," ...", end='') + if debug: print("Trying to connect with ",instance+'.secret'," to ",'https://'+instance," ...", end='') mastodon_api = Mastodon( client_id='/var/run/lock/'+instance+'.secret', api_base_url='https://'+instance ) - print("ok.") + if debug: print(" ok.") except: print("ERROR: can't connect") sys.exit(1) - print("Login with email ",mastodon_account_email," ...", end='') + + if debug: print("Login with email ",mastodon_account_email," ...", end='') try: mastodon_api.log_in( mastodon_account_email, passwd, to_file='/var/run/lock/'+instance+'.secret' ) - print("ok.") + if debug: print(" ok.") except: print("ERROR: First Login Failed! ",) sys.exit(1) c = t.title - print("============================================================") - print("[Posting]:",c) - print(" ") + if debug: print("============================================================") + if debug: print("[Posting]:",c) + if debug: print(" ") if twitter and t.author.lower() != ('(@%s)' % twitter).lower(): c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c toot_media = [] @@ -105,7 +109,6 @@ for t in reversed(d.entries): media = requests.get(p.group(0)) media_posted = mastodon_api.media_post(media.content, mime_type=media.headers.get('content-type')) toot_media.append(media_posted['id']) - print("Added media",id) # replace short links by original URL m = re.search(r"http[^ \xa0]*", c) @@ -142,6 +145,6 @@ for t in reversed(d.entries): sql.commit() sleep(0.1) else: - print(".",end='') + if debug: print(".",end='') -print("done.") +if debug: print(" done.") -- GitLab