diff --git a/tootbot.py b/tootbot.py index 053e4f846e8b03dd9945aa79e36bd85515cef868..e72cd66608cc69a290dbdb92b82bd1617a5c9ccf 100755 --- a/tootbot.py +++ b/tootbot.py @@ -8,8 +8,8 @@ import feedparser from mastodon import Mastodon import requests -if len(sys.argv) < 4: - print("Usage: python3 tootbot.py twitter_account mastodon_login mastodon_passwd mastodon_instance [max_days [footer_tags [delay]]]") # noqa +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) # sqlite db to store processed tweets (and corresponding toots ids) @@ -20,38 +20,36 @@ db.execute('''CREATE TABLE IF NOT EXISTS tweets (tweet text, toot text, if len(sys.argv) > 4: instance = sys.argv[4] -else: - instance = 'amicale.net' if len(sys.argv) > 5: days = int(sys.argv[5]) -else: - days = 1 if len(sys.argv) > 6: tags = sys.argv[6] -else: - tags = None if len(sys.argv) > 7: delay = int(sys.argv[7]) -else: - delay = 0 +if len(sys.argv) > 8: + mastodon_account_email = sys.argv[8] -source = sys.argv[1] +source = sys.argv[1] mastodon = sys.argv[2] -passwd = sys.argv[3] +passwd = sys.argv[3] mastodon_api = None if source[:4] == 'http': + print("Parsing source ",source," ...", end='') d = feedparser.parse(source) twitter = None + print("ok.") else: d = feedparser.parse('http://twitrss.me/twitter_user_to_rss/?user='+source) twitter = source +print("Processing", end='') + for t in reversed(d.entries): # check if this tweet has been processed db.execute('SELECT * FROM tweets WHERE tweet = ? AND twitter = ? and mastodon = ? and instance = ?', (t.id, source, mastodon, instance)) # noqa @@ -63,33 +61,38 @@ for t in reversed(d.entries): if last is None and age < timedelta(days=days) and age > timedelta(days=delay): if mastodon_api is None: # Create application if it does not exist - if not os.path.isfile(instance+'.secret'): - if Mastodon.create_app( - 'tootbot', - api_base_url='https://'+instance, - to_file=instance+'.secret' - ): - print('tootbot app created on instance '+instance) - else: - print('failed to create app on instance '+instance) - sys.exit(1) + try: + Mastodon.create_app('tootbot', api_base_url='https://'+instance, to_file='/var/run/lock/'+instance+'.secret') + except: + print('ERROR: failed to create app on instance '+instance) + sys.exit(1) try: + print("Trying to connect with ",instance+'.secret'," to ",'https://'+instance," ...", end='') mastodon_api = Mastodon( - client_id=instance+'.secret', + client_id='/var/run/lock/'+instance+'.secret', api_base_url='https://'+instance ) + print("ok.") + except: + print("ERROR: can't connect") + sys.exit(1) + print("Login with email ",mastodon_account_email," ...", end='') + try: mastodon_api.log_in( - username=mastodon, - password=passwd, - scopes=['read', 'write'], - to_file=mastodon+".secret" + mastodon_account_email, + passwd, + to_file='/var/run/lock/'+instance+'.secret' ) + print("ok.") except: - print("ERROR: First Login Failed!") + print("ERROR: First Login Failed! ",) sys.exit(1) c = t.title + print("============================================================") + print("[Posting]:",c) + print(" ") if twitter and t.author.lower() != ('(@%s)' % twitter).lower(): c = ("RT https://twitter.com/%s\n" % t.author[2:-1]) + c toot_media = [] @@ -98,6 +101,7 @@ 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) @@ -132,3 +136,7 @@ for t in reversed(d.entries): db.execute("INSERT INTO tweets VALUES ( ? , ? , ? , ? , ? )", (t.id, toot["id"], source, mastodon, instance)) sql.commit() + else: + print(".",end='') + +print("Bye!")