this repo has no description
1import logging 2import re 3 4import apsw 5import apsw.ext 6 7from . import BaseFeed 8 9# https://en.wikipedia.org/wiki/Seven_dirty_words 10SDW_REGEX = re.compile(r'^(shit|piss|fuck|cunt|cocksucker|motherfucker|tits)[!,./;?~ ]*$', re.I|re.A) 11 12class SevenDirtyWordsFeed(BaseFeed): 13 FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/sdw' 14 15 def __init__(self): 16 self.db_cnx = apsw.Connection('db/sdw.db') 17 self.db_cnx.pragma('journal_mode', 'WAL') 18 self.db_cnx.pragma('wal_autocheckpoint', '0') 19 20 with self.db_cnx: 21 self.db_cnx.execute(""" 22 create table if not exists posts (uri text, create_ts timestamp); 23 create unique index if not exists create_ts_idx on posts(create_ts); 24 """) 25 26 self.logger = logging.getLogger('feeds.sdw') 27 28 def process_commit(self, commit): 29 if commit['opType'] != 'c': 30 return 31 32 if commit['collection'] != 'app.bsky.feed.post': 33 return 34 35 record = commit.get('record') 36 if record is None: 37 return 38 39 conds = [ 40 record.get('reply') is None, 41 record.get('embed') is None, 42 record.get('facets') is None, 43 SDW_REGEX.search(record.get('text', '')) is not None, 44 ] 45 46 if not all(conds): 47 return 48 49 repo = commit['did'] 50 rkey = commit['rkey'] 51 post_uri = f'at://{repo}/app.bsky.feed.post/{rkey}' 52 ts = self.safe_timestamp(record.get('createdAt')).timestamp() 53 self.transaction_begin(self.db_cnx) 54 self.db_cnx.execute( 55 'insert into posts (uri, create_ts) values (:uri, :ts)', 56 dict(uri=post_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 67 from posts 68 order by create_ts desc 69 limit :limit 70 offset :offset 71 """, dict(limit=limit, offset=offset)) 72 return [uri for (uri,) in cur] 73 74 def serve_feed_debug(self, limit, offset, langs): 75 query = "select * from posts order by create_ts desc limit :limit offset :offset" 76 bindings = dict(limit=limit, offset=offset) 77 return apsw.ext.format_query_table( 78 self.db_cnx, query, bindings, 79 string_sanitize=2, text_width=9999, use_unicode=True 80 )