From: Michael Tremer Date: Thu, 11 May 2023 10:35:26 +0000 (+0000) Subject: Remove any references to Twitter X-Git-Url: http://git.ipfire.org/?p=ipfire.org.git;a=commitdiff_plain;h=329bb75bdad2405b40f4588fbdaebd4c29ed90d6 Remove any references to Twitter Signed-off-by: Michael Tremer --- diff --git a/Makefile.am b/Makefile.am index d30de3ab..e4495302 100644 --- a/Makefile.am +++ b/Makefile.am @@ -69,7 +69,7 @@ backend_PYTHON = \ src/backend/resolver.py \ src/backend/settings.py \ src/backend/talk.py \ - src/backend/tweets.py \ + src/backend/toots.py \ src/backend/util.py \ src/backend/wiki.py \ src/backend/zeiterfassung.py diff --git a/src/backend/base.py b/src/backend/base.py index f85b5ad6..2bb8214f 100644 --- a/src/backend/base.py +++ b/src/backend/base.py @@ -23,7 +23,7 @@ from . import releases from . import resolver from . import settings from . import talk -from . import tweets +from . import toots from . import util from . import wiki from . import zeiterfassung @@ -143,7 +143,7 @@ class Backend(object): "send-message" : self.messages.send_cli, "send-all-messages" : self.messages.queue.send_all, "test-ldap" : self.accounts.test_ldap, - "tweet" : self.tweets.tweet, + "toot" : self.toots.toot, "update-blog-feeds" : self.blog.update_feeds, } @@ -191,8 +191,8 @@ class Backend(object): return resolver.Resolver(tries=2, timeout=2, domains=[]) @lazy_property - def tweets(self): - return tweets.Tweets(self) + def toots(self): + return toots.Toots(self) async def cleanup(self): # Cleanup message queue diff --git a/src/backend/toots.py b/src/backend/toots.py new file mode 100644 index 00000000..d434d6d9 --- /dev/null +++ b/src/backend/toots.py @@ -0,0 +1,75 @@ +#!/usr/bin/python3 + +import datetime +import logging +import mastodon + +from .misc import Object + +class Toots(Object): + async def toot(self): + """ + Sends a random promotional toot + """ + # Do not toot too often + if self.has_had_recent_activity(days=3): + logging.debug("Won't toot because we recently did it") + return + + # Do not toot when there was a blog post + if self.backend.blog.has_had_recent_activity(hours=24): + logging.debug("Won't toot because the blog has had activity") + return + + # Select a toot + toot = self._get_random_toot() + if not toot: + logging.warning("Could not find anything to toot") + return + + # Toot the toot! + with self.db.transaction(): + self._toot(toot) + + def has_had_recent_activity(self, **kwargs): + t = datetime.timedelta(**kwargs) + + res = self.db.get("SELECT COUNT(*) AS count FROM toots \ + WHERE last_tooted_at IS NOT NULL AND last_tooted_at >= NOW() - %s", t) + + if res and res.count > 0: + return True + + return False + + def _get_random_toot(self): + res = self.db.get( + "WITH candidate_toots AS (SELECT id, \ + (CURRENT_TIMESTAMP - COALESCE(last_tooted_at, '1970-01-01')) * RANDOM() AS age \ + FROM toots \ + WHERE (last_tooted_at IS NULL OR last_tooted_at <= CURRENT_TIMESTAMP - INTERVAL '1 month') \ + ) \ + SELECT toots.* FROM candidate_toots \ + LEFT JOIN toots ON candidate_toots.id = toots.id \ + ORDER BY age DESC LIMIT 1") + + return res + + def _toot(self, toot): + logging.debug("Posting: %s" % toot.message) + + # Update database status + self.db.execute("UPDATE toots \ + SET last_tooted_at = CURRENT_TIMESTAMP, total_toots = total_toots + 1 \ + WHERE id = %s", toot.id) + + # Connect to Mastodon + conn = mastodon.Mastodon( + client_id=self.settings.get("mastodon-client-key"), + client_secret=self.settings.get("mastodon-client-secret"), + access_token=self.settings.get("mastodon-access-token"), + api_base_url="https://social.ipfire.org", + ) + + # Toot! + conn.toot(toot.message) diff --git a/src/backend/tweets.py b/src/backend/tweets.py deleted file mode 100644 index 84c78b82..00000000 --- a/src/backend/tweets.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/usr/bin/python3 - -import datetime -import logging -import mastodon - -from .misc import Object - -class Tweets(Object): - async def tweet(self): - """ - Sends a random promotional tweet - """ - # Do not tweet too often - if self.has_had_recent_activity(days=3): - logging.debug("Won't tweet because we recently did it") - return - - # Do not tweet when there was a blog post - if self.backend.blog.has_had_recent_activity(hours=24): - logging.debug("Won't tweet because the blog has had activity") - return - - # Select a tweet - tweet = self._get_random_tweet() - if not tweet: - logging.warning("Could not find anything to tweet") - return - - # Tweet the tweet - with self.db.transaction(): - self._tweet(tweet) - - def has_had_recent_activity(self, **kwargs): - t = datetime.timedelta(**kwargs) - - res = self.db.get("SELECT COUNT(*) AS count FROM tweets \ - WHERE last_tweeted_at IS NOT NULL AND last_tweeted_at >= NOW() - %s", t) - - if res and res.count > 0: - return True - - return False - - def _get_random_tweet(self): - res = self.db.get( - "WITH candidate_tweets AS (SELECT id, \ - (CURRENT_TIMESTAMP - COALESCE(last_tweeted_at, '1970-01-01')) * RANDOM() AS age \ - FROM tweets \ - WHERE (last_tweeted_at IS NULL OR last_tweeted_at <= CURRENT_TIMESTAMP - INTERVAL '1 month') \ - ) \ - SELECT tweets.* FROM candidate_tweets \ - LEFT JOIN tweets ON candidate_tweets.id = tweets.id \ - ORDER BY age DESC LIMIT 1") - - return res - - def _tweet(self, tweet): - logging.debug("Posting: %s" % tweet.message) - - # Update database status - self.db.execute("UPDATE tweets \ - SET last_tweeted_at = CURRENT_TIMESTAMP, total_tweets = total_tweets + 1 \ - WHERE id = %s", tweet.id) - - # Connect to Mastodon - conn = mastodon.Mastodon( - client_id=self.settings.get("mastodon-client-key"), - client_secret=self.settings.get("mastodon-client-secret"), - access_token=self.settings.get("mastodon-access-token"), - api_base_url="https://social.ipfire.org", - ) - - # Toot! - conn.toot(tweet.message) diff --git a/src/crontab/ipfire b/src/crontab/ipfire index 8dfe1aea..a5cb7c29 100644 --- a/src/crontab/ipfire +++ b/src/crontab/ipfire @@ -21,5 +21,5 @@ SHELL=/bin/bash # Check mirrors once every 30 min */30 * * * * nobody ipfire.org --logging=error check-mirrors -# Tweet once a week -0 8 * * * nobody sleep ${RANDOM} && ipfire.org tweet +# Toot once a day +0 8 * * * nobody sleep ${RANDOM} && ipfire.org toot