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.logger.debug('found quote post of target, adding to feed') 48 uri = 'at://{repo}/app.bsky.feed.post/{rkey}'.format( 49 repo = commit['did'], 50 rkey = commit['rkey'] 51 ) 52 ts = self.safe_timestamp(record.get('createdAt')).timestamp() 53 self.db_cnx.execute( 54 'insert into posts (uri, create_ts) values (:uri, :ts)', 55 dict(uri=uri, ts=ts) 56 ) 57 58 def commit_changes(self): 59 self.logger.debug('committing changes') 60 self.wal_checkpoint(self.db_cnx, 'RESTART') 61 62 def serve_feed(self, limit, offset, langs): 63 cur = self.db_cnx.execute( 64 'select uri from posts order by create_ts desc limit :limit offset :offset', 65 dict(limit=limit, offset=offset) 66 ) 67 return [uri for (uri,) in cur] 68 69 def serve_feed_debug(self, limit, offset, langs): 70 query = 'select uri from posts order by create_ts desc limit :limit offset :offset' 71 bindings = dict(limit=limit, offset=offset) 72 return apsw.ext.format_query_table( 73 self.db_cnx, query, bindings, 74 string_sanitize=2, text_width=9999, use_unicode=True 75 )