Bot to retrieve tweets from a Twitter feed and post them under a Mastodon account.
Go to file
2023-07-13 13:32:38 +02:00
.gitignore Put it in a function 2023-06-13 17:28:05 +02:00
CHANGELOG.md Updated README and CHANGELOG 2023-07-11 21:10:39 +02:00
default.toml Better default in config 2023-07-11 13:40:53 +02:00
LICENSE version 4.0 2023-06-19 20:18:37 +02:00
README.md Update README 2023-07-12 15:07:00 +02:00
twoot.py Add some log messages 2023-07-13 13:32:38 +02:00

Twoot

Twoot is a python script that mirrors tweets from a twitter account to a Mastodon account. It is simple to set-up on a local machine, configurable and feature-rich.

12 JUL 2023 VERSION 4.1

Nitter has recently added a change that highlights tweets that are part of a thread. Twoot cannot handle this modification yet therefore TWEETS THAT ARE PART OF A THREAD ARE CURRENTLY IGNORED. A warning message is added to the log file instead. An update is being worked on. Stay tuned.

A new dependency to python module pytz has been added. Please run pip install pytz in your environment to install it.

  • Added option to display timestamp of the original tweet in toot
  • Tweaked list of nitter instances

Previous updates can be found in CHANGELOG.

Features

  • Fetch timeline of given user from twitter.com (through nitter instance)
  • Scrape html and format tweets for post on mastodon
  • Emojis supported
  • Upload images from tweet to Mastodon
  • Optionally upload videos from tweet to Mastodon
  • Specify maximum age of tweet to be considered
  • Specify minimum delay before considering a tweet for upload
  • Remember tweets already tooted to prevent double posting
  • Optionally update avatar and banner pictures on profile if changed
  • Optionally post reply-to tweets on the mastodon account
  • Optionally ignore retweets
  • Optionally remove redirections (e.g. reveal destination of short URLs)
  • Optionally remove trackers (UTM parameters) from URLs
  • Optional domain substitution for Twitter, Youtube and Reddit domains (e.g. Nitter, Invidious and teddit respectively)
  • option to add timestamp of the original tweet to bottom of toot
  • Optional footer line to add tags at bottom of toot
  • Allows rate-limiting posts to Mastodon instance

Usage

usage: twoot.py [-h] [-f <.toml config file>] [-t <twitter account>] [-i <mastodon instance>]
                [-m <mastodon account>] [-p <mastodon password>] [-r] [-s] [-l] [-u] [-v] [-o] [-q]
                [-a <max age (in days)>] [-d <min delay (in mins)>] [-c <max # of toots to post>]

Arguments

Assuming that the Twitter handle is @SuperDuperBot and the Mastodon account is sd@example.com on instance masto.space:

Switch Description Example Required
-f path of .toml file with configuration SuperDuper.toml No
-t twitter account name without '@' SuperDuper If no config file
-i Mastodon instance domain name masto.space If no config file
-m Mastodon username sd@example.com If no config file
-p Mastodon password my_Sup3r-S4f3*pw Once at first run
-v Upload videos to Mastodon N/A No
-o Do not add "Original tweet" line N/A No
-q Update avatar and banner on profile if changed N/A No
-r Post reply-to tweets (ignored by default) N/A No
-s Skip retweets (posted by default) N/A No
-l Remove link redirections N/A No
-u Remove trackers from URLs N/A No
-a Max. age of tweet to post (in days) 5 No
-d Min. age before posting new tweet (in minutes) 15 No
-c Max number of toots allowed to post (cap) 1 No

Notes

Password

A password must be provided for the first run only. Once twoot has connected successfully to the Mastodon host, an access token is saved in a .secret file named after the mastodon account, and a password is no longer necessary (command-line switch -p is not longer required).

Config file

A default.toml file is provided to be used as template. If -f is used to specify a config file to use, all the other command-line parameters are ignored, except -p (password) if provided.

-l (or remove_link_redirections = true in toml file) will follow every link included in the tweet and replace them with the url that the resource is directly dowmnloaded from (if applicable). e.g. bit.ly/xxyyyzz -> example.com Every link visit can take up to 5 sec (timeout) depending on the responsiveness of the source therefore this option will slow down tweet processing.

If you are interested by tracker removal (-u, remove_trackers_from_urls = true) you should also select redirection removal as trackers are often hidden behind the redirection of a short URL.

Uploading videos

When using the -v (upload_videos = true) switch consider:

  • whether the copyright of the content that you want to cross-post allows it
  • the storage / transfer limitations of the Mastodon instance that you are posting to
  • the upstream bandwidth that you may consume on your internet connection

Updating profile

If -q (update_profile = true) is specified, twoot will check if the avatar and banner pictures have changed on the twitter page. This check compares the name of files used by twitter with the names of the files that have been uploaded on Mastodon and if they differ both files are downloaded from twitter and uploaded on Mastodon. The check is very fast if there is no update.

Adding timestamp of original tweet to toot

Use tweet_time_format option in configuration file to specify the datetime format to display the date at which the tweet was published next to the "Original tweet" link. Valid format specifiers are the same as those used to format datetimes in python (https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior). e.g. tweet_time_format = "(%d %b %Y %H:%M %Z)"

An empty or missing tweet_time_format disables the display of the timestamp.

By default, dates are specified in UTC time zone. To convert the timestamp to another time zone, use the tweet_timezone option in configuration file. Valid time zone names are those of the Olson time zone database (https://en.wikipedia.org/wiki/Tz_database) e.g. tweet_timezone = "Europe/Paris"

Rate control

Default max age is 1 day. Decimal values are OK.

Default min delay is 0 minutes.

No limitation is applied to the number of toots uploaded if -c is not specified.

Installation

Make sure python3 is installed.

Twoot depends on beautifulsoup4 and Mastodon.py python modules. Additionally, if you are using a version of python < 3.11 you also need to install the tomli module.

Only If you plan to download videos with the -v switch, are the additional dependencies required:

  • Python module youtube-dl2
  • ffmpeg (installed with the package manager of your distribution)
pip install beautifulsoup4 Mastodon.py youtube-dl2

In your user folder, execute git clone https://gitlab.com/jeancf/twoot.git to clone repo with twoot.py script.

Add command line to crontab. For example, to run every 15 minutes starting at minute 1 of every hour and process the tweets posted in the last 5 days but at least 15 minutes ago:

1-59/15 * * * * /path/to/twoot.py -t SuperDuper -i masto.space -m sd@example.com -p my_Sup3r-S4f3*pw -a 5 -d 15

Twoot is known to be used for the following feeds (older first):

Background

I started twoot when tootbot stopped working. Tootbot relied on RSS feeds from https://twitrss.me that broke when Twitter refreshed their web UI in July 2019.