this repo has no description
1import logging 2 3import apsw 4import apsw.ext 5 6from . import BaseFeed 7 8# https://bsky.app/profile/nora.zone/post/3kv35hqi4a22b 9TARGET_QUOTE_URI = 'at://did:plc:4qqizocrnriintskkh6trnzv/app.bsky.feed.post/3kv35hqi4a22b' 10 11class NoraZoneInteresting(BaseFeed): 12 FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/nz-interesting' 13 14 def __init__(self): 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') 18 19 with self.db_cnx: 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); 23 """) 24 25 self.logger = logging.getLogger('feeds.nz_interesting') 26 27 def process_commit(self, commit): 28 if commit['opType'] != 'c': 29 return 30 31 if commit['collection'] != 'app.bsky.feed.post': 32 return 33 34 record = commit.get('record') 35 if record is None: 36 return 37 38 embed = record.get('embed') 39 if embed is None: 40 return 41 42 inner_record = embed.get('record') 43 if inner_record is None: 44 return 45 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'] 52 ) 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) 57 ) 58 59 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') 63 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) 68 ) 69 return [uri for (uri,) in cur] 70 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 77 )