···
3
+
from atproto import Client, models
···
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/nz-interesting'
15
-
self.db_cnx = apsw.Connection('db/nz-interesting.db')
16
-
self.db_cnx.pragma('journal_mode', 'WAL')
17
-
self.db_cnx.pragma('wal_autocheckpoint', '0')
20
-
self.db_cnx.execute("""
21
-
create table if not exists posts (uri text, create_ts timestamp);
22
-
create index if not exists create_ts_idx on posts(create_ts);
25
-
self.logger = logging.getLogger('feeds.nz_interesting')
16
+
self.client = Client('https://public.api.bsky.app')
def process_commit(self, commit):
28
-
if commit['opType'] != 'c':
31
-
if commit['collection'] != 'app.bsky.feed.post':
34
-
record = commit.get('record')
38
-
embed = record.get('embed')
42
-
inner_record = embed.get('record')
43
-
if inner_record is None:
46
-
if inner_record.get('uri') == TARGET_QUOTE_URI:
47
-
self.transaction_begin(self.db_cnx)
48
-
self.logger.debug('found quote post of target, adding to feed')
49
-
uri = 'at://{repo}/app.bsky.feed.post/{rkey}'.format(
50
-
repo = commit['did'],
51
-
rkey = commit['rkey']
53
-
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
54
-
self.db_cnx.execute(
55
-
'insert into posts (uri, create_ts) values (:uri, :ts)',
56
-
dict(uri=uri, ts=ts)
def commit_changes(self):
60
-
self.logger.debug('committing changes')
61
-
self.transaction_commit(self.db_cnx)
62
-
self.wal_checkpoint(self.db_cnx, 'RESTART')
64
-
def serve_feed(self, limit, offset, langs):
65
-
cur = self.db_cnx.execute(
66
-
'select uri from posts order by create_ts desc limit :limit offset :offset',
67
-
dict(limit=limit, offset=offset)
69
-
return [uri for (uri,) in cur]
71
-
def serve_feed_debug(self, limit, offset, langs):
72
-
query = 'select * from posts order by create_ts desc limit :limit offset :offset'
73
-
bindings = dict(limit=limit, offset=offset)
74
-
return apsw.ext.format_query_table(
75
-
self.db_cnx, query, bindings,
76
-
string_sanitize=2, text_width=9999, use_unicode=True
24
+
def serve_feed(self, limit, cursor, langs):
25
+
quotes = self.client.app.bsky.feed.get_quotes(
26
+
models.AppBskyFeedGetQuotes.Params(
27
+
uri = TARGET_QUOTE_URI,
33
+
'cursor': quotes.cursor,
34
+
'feed': [dict(post=post.uri) for post in quotes.posts],