···
6
+
from . import BaseFeed
8
+
class PopularQuotePostsFeed(BaseFeed):
9
+
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/popqp'
10
+
SERVE_FEED_QUERY = """
11
+
select uri, create_ts, update_ts, quote_count, exp( -1 * ( ( unixepoch('now') - create_ts ) / 10800.0 ) ) as decay,
12
+
quote_count * exp( -1 * ( ( unixepoch('now') - create_ts ) / 10800.0 ) ) as score
14
+
order by quote_count * exp( -1 * ( ( unixepoch('now') - create_ts ) / 10800.0 ) ) desc
15
+
limit :limit offset :offset
17
+
DELETE_OLD_POSTS_QUERY = """
18
+
delete from posts where
19
+
quote_count * exp( -1 * ( ( unixepoch('now') - create_ts ) / 10800.0 ) ) < 1.0
20
+
and create_ts < unixepoch('now', '-24 hours')
24
+
self.db_cnx = apsw.Connection('db/popqp.db')
25
+
self.db_cnx.pragma('journal_mode', 'WAL')
26
+
self.db_cnx.pragma('wal_autocheckpoint', '0')
29
+
self.db_cnx.execute("""
30
+
create table if not exists posts (
31
+
uri text, create_ts timestamp, update_ts timestamp, quote_count int
33
+
create unique index if not exists uri_idx on posts(uri);
36
+
self.logger = logging.getLogger('feeds.popqp')
38
+
def process_commit(self, commit):
39
+
if commit['opType'] != 'c':
42
+
if commit['collection'] != 'app.bsky.feed.post':
45
+
record = commit.get('record')
49
+
embed = record.get('embed')
53
+
embed_type = embed.get('$type')
54
+
if embed_type == 'app.bsky.embed.record':
55
+
quote_post_uri = embed['record']['uri']
56
+
elif embed_type == 'app.bsky.embed.recordWithMedia':
57
+
quote_post_uri = embed['record']['record']['uri']
61
+
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
62
+
self.transaction_begin(self.db_cnx)
64
+
self.db_cnx.execute("""
65
+
insert into posts (uri, create_ts, update_ts, quote_count)
66
+
values (:uri, :ts, :ts, 1)
67
+
on conflict (uri) do
68
+
update set quote_count = quote_count + 1, update_ts = :ts
69
+
""", dict(uri=quote_post_uri, ts=ts))
71
+
def delete_old_posts(self):
72
+
self.db_cnx.execute(self.DELETE_OLD_POSTS_QUERY)
73
+
self.logger.debug('deleted {} old posts'.format(self.db_cnx.changes()))
75
+
def commit_changes(self):
76
+
self.delete_old_posts()
77
+
self.logger.debug('committing changes')
78
+
self.transaction_commit(self.db_cnx)
79
+
self.wal_checkpoint(self.db_cnx, 'RESTART')
81
+
def serve_feed(self, limit, offset, langs):
82
+
cur = self.db_cnx.execute(self.SERVE_FEED_QUERY, dict(limit=limit, offset=offset))
83
+
return [row[0] for row in cur]
85
+
def serve_feed_debug(self, limit, offset, langs):
86
+
bindings = dict(limit=limit, offset=offset)
87
+
return apsw.ext.format_query_table(
88
+
self.db_cnx, self.SERVE_FEED_QUERY, bindings,
89
+
string_sanitize=2, text_width=9999, use_unicode=True