···
+
class RatioFeed(BaseFeed):
+
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/ratio'
+
( unixepoch('now') - create_ts ) as age_seconds,
+
( replies + quoteposts ) / ( likes + reposts ) as ratio,
+
exp( -1 * ( ( unixepoch('now') - create_ts ) / ( 3600.0 * 16 ) ) ) as decay
+
( ratio * decay ) as score
+
where replies > 15 and ratio > 2.5
+
limit :limit offset :offset
+
DELETE_OLD_POSTS_QUERY = """
+
create_ts < unixepoch('now', '-5 days')
+
self.db_cnx = apsw.Connection('db/ratio.db')
+
self.db_cnx.pragma('journal_mode', 'WAL')
+
self.db_cnx.pragma('wal_autocheckpoint', '0')
+
self.db_cnx.execute("""
+
create table if not exists posts (
+
uri text, create_ts timestamp,
+
replies float, likes float, reposts float, quoteposts float
+
create unique index if not exists uri_idx on posts(uri);
+
self.logger = logging.getLogger('feeds.ratio')
+
def process_commit(self, commit):
+
if commit['opType'] != 'c':
+
if commit['collection'] in {'app.bsky.feed.like', 'app.bsky.feed.repost'}:
+
record = commit.get('record')
+
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
+
subject_uri = record['subject']['uri']
+
elif commit['collection'] == 'app.bsky.feed.post':
+
record = commit.get('record')
+
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
+
if record.get('reply') is not None:
+
subject_uri = record['reply']['parent']['uri']
+
# only count non-OP replies
+
if subject_uri.startswith('at://' + commit['did']):
+
elif record.get('embed') is not None:
+
t = record['embed']['$type']
+
if t == 'app.bsky.embed.record':
+
subject_uri = record['embed']['record']['uri']
+
elif t == 'app.bsky.embed.recordWithMedia':
+
subject_uri = record['embed']['record']['record']['uri']
+
if subject_uri is None:
+
'is_reply': int(is_reply),
+
'is_like': int(commit['collection'] == 'app.bsky.feed.like'),
+
'is_repost': int(commit['collection'] == 'app.bsky.feed.repost'),
+
'is_quotepost': int(is_quotepost),
+
self.transaction_begin(self.db_cnx)
+
self.db_cnx.execute("""
+
insert into posts(uri, create_ts, replies, likes, reposts, quoteposts)
+
case when :is_reply then 1 else 0 end,
+
case when :is_like then 1 else 0 end,
+
case when :is_repost then 1 else 0 end,
+
case when :is_quotepost then 1 else 0 end)
+
replies = replies + case when :is_reply then 1 else 0 end,
+
likes = likes + case when :is_like then 1 else 0 end,
+
reposts = reposts + case when :is_repost then 1 else 0 end,
+
quoteposts = quoteposts + case when :is_quotepost then 1 else 0 end
+
def delete_old_posts(self):
+
self.db_cnx.execute(self.DELETE_OLD_POSTS_QUERY)
+
def commit_changes(self):
+
self.logger.debug('committing changes')
+
self.delete_old_posts()
+
self.transaction_commit(self.db_cnx)
+
self.wal_checkpoint(self.db_cnx, 'RESTART')
+
def serve_feed(self, limit, offset, langs):
+
cur = self.db_cnx.execute(self.SERVE_FEED_QUERY, dict(limit=limit, offset=offset))
+
return [row[0] for row in cur]
+
def serve_feed_debug(self, limit, offset, langs):
+
bindings = dict(limit=limit, offset=offset)
+
return apsw.ext.format_query_table(
+
self.db_cnx, self.SERVE_FEED_QUERY, bindings,
+
string_sanitize=2, text_width=9999, use_unicode=True