]>
git.ipfire.org Git - ipfire.org.git/blob - src/backend/tweets.py
8 from .misc
import Object
11 @tornado.gen
.coroutine
14 Sends a random promotional tweet
16 # Do not tweet too often
17 if self
.has_had_recent_activity():
18 logging
.debug("Won't tweet because we recently did it")
21 # Do not tweet when there was a blog post
22 if self
.backend
.blog
.has_had_recent_activity():
23 logging
.debug("Won't tweet because the blog has had activity")
27 tweet
= self
._get
_random
_tweet
()
29 logging
.warning("Could not find anything to tweet")
33 with self
.db
.transaction():
36 def has_had_recent_activity(self
, t
=None):
38 t
= datetime
.timedelta(days
=3)
40 res
= self
.db
.get("SELECT COUNT(*) AS count FROM tweets \
41 WHERE last_tweeted_at IS NOT NULL AND last_tweeted_at >= NOW() - %s", t
)
43 if res
and res
.count
> 0:
48 def _get_random_tweet(self
):
50 "WITH candidate_tweets AS (SELECT id, \
51 (CURRENT_TIMESTAMP - COALESCE(last_tweeted_at, '1970-01-01')) * RANDOM() AS age \
53 WHERE (last_tweeted_at IS NULL OR last_tweeted_at <= CURRENT_TIMESTAMP - INTERVAL '1 month') \
55 SELECT tweets.* FROM candidate_tweets \
56 LEFT JOIN tweets ON candidate_tweets.id = tweets.id \
57 ORDER BY age DESC LIMIT 1")
61 def _tweet(self
, tweet
):
62 logging
.info("Tweeting: %s" % tweet
.message
)
64 # Update database status
65 self
.db
.execute("UPDATE tweets \
66 SET last_tweeted_at = CURRENT_TIMESTAMP, total_tweets = total_tweets + 1 \
67 WHERE id = %s", tweet
.id)
70 twitter
= twython
.Twython(
71 self
.settings
.get("twitter_consumer_key"),
72 self
.settings
.get("twitter_consumer_secret"),
73 self
.settings
.get("twitter_%s_access_token" % tweet
.account
),
74 self
.settings
.get("twitter_%s_access_token_secret" % tweet
.account
),
78 twitter
.update_status(status
=tweet
.message
)