this repo has no description
1import logging 2import re 3 4import apsw 5import apsw.ext 6 7from . import BaseFeed 8 9SDW_REGEX = re.compile(r'^(shit|piss|fuck|cunt|cocksucker|motherfucker|tits)\W*$', re.I|re.A) 10 11class SevenDirtyWordsFeed(BaseFeed): 12 FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/sdw' 13 14 def __init__(self): 15 self.db_cnx = apsw.Connection('db/sdw.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 unique index if not exists create_ts_idx on posts(create_ts); 23 """) 24 25 self.logger = logging.getLogger('feeds.sdw') 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 if record.get('reply') is not None: 39 return 40 41 # https://en.wikipedia.org/wiki/Seven_dirty_words 42 if SDW_REGEX.search(record['text']) is not None: 43 repo = commit['did'] 44 rkey = commit['rkey'] 45 post_uri = f'at://{repo}/app.bsky.feed.post/{rkey}' 46 ts = self.safe_timestamp(record.get('createdAt')).timestamp() 47 self.transaction_begin(self.db_cnx) 48 self.db_cnx.execute( 49 'insert into posts (uri, create_ts) values (:uri, :ts)', 50 dict(uri=post_uri, ts=ts) 51 ) 52 53 def commit_changes(self): 54 self.logger.debug('committing changes') 55 self.transaction_commit(self.db_cnx) 56 self.wal_checkpoint(self.db_cnx, 'RESTART') 57 58 def serve_feed(self, limit, offset, langs): 59 cur = self.db_cnx.execute(""" 60 select uri 61 from posts 62 order by create_ts desc 63 limit :limit 64 offset :offset 65 """, dict(limit=limit, offset=offset)) 66 return [uri for (uri,) in cur] 67 68 def serve_feed_debug(self, limit, offset, langs): 69 query = "select * from posts order by create_ts desc limit :limit offset :offset" 70 bindings = dict(limit=limit, offset=offset) 71 return apsw.ext.format_query_table( 72 self.db_cnx, query, bindings, 73 string_sanitize=2, text_width=9999, use_unicode=True 74 )