Posting text and images to Mastodon
Private Post
Notice that, if using cam
with the -m
argument (line 59), you have a block of code (lines 60 to 64) that uses methods from the cv2
module to grab a picture from the default webcam and send that to Mastodon.
Remembering that you pulled in the OpenCV module (cv2
) on line 4, you then create a VideoCapture
object from the default camera on line 60, read what the camera is seeing on line 61, write it out to a file on line 62, and free up the camera again when done (line 63). All that's left to do is to dump the location of the image into the mmedia
variable for posting (line 64).
Posting an image from a webcam may be useful for, say, monitoring your home, but it does raise serious privacy concerns if the posts are public. This is where a toot's visibility options come into play. On line 51 of Listing 4, you can see that a user can set a toot's visibility. The options available are:
- Public means the toot will be visible to everyone. A public toot can also be found when running a search across the Mastodon federated network.
- Unlisted means the toot is visible, but that it cannot be found via a search. Your followers can see it, but a non-follower would need a direct link to the toot to be able to read it. An unlisted toot can be boosted ("retooted"), but the boosted toot remains unlisted.
- Private is a bit confusing, because it may not mean what you think. Your followers can see private toots, but non-followers can't. So, in fact, it's not very private at all. However, you cannot boost another user's private toot.
Figure 3 shows three toots with different levels of privacy. Notice the padlock and the absence of a boosting icon in the private toot.
There is a fourth level of privacy which, it turns out, is exactly what you need to keep the pictures from your webcam truly private:
- Direct makes the toot a direct message and these are truly private, since they land in your inbox and only you and the owners of the accounts mentioned in the message can see them.
So if you send a direct message with a snap attached but no username (Figure 4, bottom toot) or your own username in the text of the message (Figure 4, top toot), they will land discretely in your Mastodon inbox and won't be seen by anyone else:
./posttoots.py -u youremail@here.com -p "Your awesome password" -a post -t "Direct test toot with pic" -m cam -v direct
That said, you will have to click on a link to see the image. But, if you have two separate Mastodon accounts and send a direct message from one to the other, the toot will show up in the recipient's feed with a preview of the image, like you can see in Figure 5.
./posttoots.py -u youremail@here.com -p "Your awesome password" -a post -t "Direct test toot to @your_other@account" -m cam -v direct
The darker background indicates that it is a direct message and not a regular toot.
Post-posting
Warning: Scheduling is finicky and seems to be supported on only a few Mastodon instances. But, if one day scheduling on Mastodon becomes universally available, this is how you would do it:
Set the -a
option to schedule
and set the -w
argument. The -w
argument takes a string that contains a date. The format for the date is "mmm dd YYYY HH:MM Z", where mmm is a three letter abbreviation of the month in lower case (jan, feb, mar, etc.), dd is the day of the month (01 to 31), YYYY is the year (2019, 2020, etc.), HH is the hour (00 to 23), MM are the minutes (00 to 60), and Z is the time zone (UTC, CET, PDT, etc.).
To send a toot at noon on the 31st of January 2020, for example, you'd run:
./posttoots.py -w "jan 31 2020 12:00 CET" -a schedule -u youremail@here.com -p "Your awesome password" -t "Scheduled from command line at 12:00"
In Listing 4, lines 71 to 73 are the ones that deal with the schedule
option. First, the scheduled date is processed on lines 34 to 38 using Python's datetime
module if it is a viable date (lines 35 and 36). However, the string will be discarded if datetime
cannot identify it as a date (lines 37 and 38). Then the toot is sent as usual (Figure 6).
You can see the list of scheduled toots by using the list
option with the -a
argument:
./posttoots.py -a list -u youremail@here.com -p "Your awesome password"
Scheduling is cool, but as mentioned above, fickle. As noted elsewhere, if you rely on your .secrets
file to supply the address of the Mastodon instance you are using, for some reason, nothing related to scheduling will work, and your app will bomb out complaining about the version of the instance. That's the reason for the ugly hack on lines 10 through 12 in Listing 4.
And then there's the fact that not all Mastodon instances support scheduling, so don't be surprised if the above does not work for you. If you try to schedule a post from your command line and then it doesn't show up in your feed at the designated time, it is a pretty sure indication that the server does not support this feature. If scheduling toots is important for you, it will be time to search for another server.
You can try https://mastodon.social/. This is the server maintained by Eugen Rochko, one of the creators of Mastodon and the person who implemented scheduling. Scheduling works a treat on this instance.
Conclusion
This is the end of the series dedicated to Mastodon. As you may have gathered, Mastodon is pretty great, and it is easy to see why it has become the most popular service in the Fediverse. It is more flexible than Twitter and much easier to program for. At this point, you should have enough knowledge to do all sorts of things, from creating clients to building bots and becoming a nuisance.
But that does not mean we are done. We still have to explore all the other services, the increasingly popular PeerTube, a contender for YouTube; Pixelfed, which wants to become the Instagram of the federated social media; and so many more. We'll be tackling these and exploring how to best leverage them in upcoming issues.
See you in the Fediverse!
Infos
- "Build your own Mastodon client," by Paul Brown, Linux Magazine, issue 230, January 2020, pp. 92-94
- Mastodon.py documentation: https://mastodonpy.readthedocs.io/en/stable/index.html
- OpenCV: https://opencv.org/
« Previous 1 2
Buy this article as PDF
(incl. VAT)
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you’ve found an article to be beneficial.
News
-
Gnome 47.2 Now Available
Gnome 47.2 is now available for general use but don't expect much in the way of newness, as this is all about improvements and bug fixes.
-
Latest Cinnamon Desktop Releases with a Bold New Look
Just in time for the holidays, the developer of the Cinnamon desktop has shipped a new release to help spice up your eggnog with new features and a new look.
-
Armbian 24.11 Released with Expanded Hardware Support
If you've been waiting for Armbian to support OrangePi 5 Max and Radxa ROCK 5B+, the wait is over.
-
SUSE Renames Several Products for Better Name Recognition
SUSE has been a very powerful player in the European market, but it knows it must branch out to gain serious traction. Will a name change do the trick?
-
ESET Discovers New Linux Malware
WolfsBane is an all-in-one malware that has hit the Linux operating system and includes a dropper, a launcher, and a backdoor.
-
New Linux Kernel Patch Allows Forcing a CPU Mitigation
Even when CPU mitigations can consume precious CPU cycles, it might not be a bad idea to allow users to enable them, even if your machine isn't vulnerable.
-
Red Hat Enterprise Linux 9.5 Released
Notify your friends, loved ones, and colleagues that the latest version of RHEL is available with plenty of enhancements.
-
Linux Sees Massive Performance Increase from a Single Line of Code
With one line of code, Intel was able to increase the performance of the Linux kernel by 4,000 percent.
-
Fedora KDE Approved as an Official Spin
If you prefer the Plasma desktop environment and the Fedora distribution, you're in luck because there's now an official spin that is listed on the same level as the Fedora Workstation edition.
-
New Steam Client Ups the Ante for Linux
The latest release from Steam has some pretty cool tricks up its sleeve.