]> git.ipfire.org Git - ipfire.org.git/commitdiff
Remove any references to Twitter
authorMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 May 2023 10:35:26 +0000 (10:35 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Thu, 11 May 2023 10:35:26 +0000 (10:35 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
Makefile.am
src/backend/base.py
src/backend/toots.py [new file with mode: 0644]
src/backend/tweets.py [deleted file]
src/crontab/ipfire

index d30de3ab4c0d8a83921a81a67029f10038cdbaa2..e4495302247ba43455d374ac18652188fd389e0f 100644 (file)
@@ -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
index f85b5ad619208218aad4319da37fa4bcaa890e69..2bb8214f03e949d333c5e0b3af4a632b8fae6fbe 100644 (file)
@@ -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 (file)
index 0000000..d434d6d
--- /dev/null
@@ -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 (file)
index 84c78b8..0000000
+++ /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)
index 8dfe1aea000c608f3c1cb3fc7594fc322202b06f..a5cb7c2944fdb73a33b9e005b391afb26337d6e3 100644 (file)
@@ -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