···
7
+
from . import BaseFeed
9
+
class SevenDirtyWordsFeed(BaseFeed):
10
+
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/sdw'
13
+
self.db_cnx = apsw.Connection('db/sdw.db')
14
+
self.db_cnx.pragma('journal_mode', 'WAL')
15
+
self.db_cnx.pragma('wal_autocheckpoint', '0')
18
+
self.db_cnx.execute("""
19
+
create table if not exists posts (uri text, create_ts timestamp);
20
+
create unique index if not exists create_ts_idx on posts(create_ts);
23
+
self.logger = logging.getLogger('feeds.sdw')
25
+
def process_commit(self, commit):
26
+
if commit['opType'] != 'c':
29
+
if commit['collection'] != 'app.bsky.feed.post':
32
+
record = commit.get('record')
36
+
# https://en.wikipedia.org/wiki/Seven_dirty_words
37
+
if re.search(r'\b(shit|piss|fuck|cunt|cocksucker|motherfucker|tits)\b', record['text'], re.I) is not None:
38
+
repo = commit['did']
39
+
rkey = commit['rkey']
40
+
post_uri = f'at://{repo}/app.bsky.feed.post/{rkey}'
41
+
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
42
+
self.transaction_begin(self.db_cnx)
43
+
self.db_cnx.execute(
44
+
'insert into posts (uri, create_ts) values (:uri, :ts)',
45
+
dict(uri=post_uri, ts=ts)
48
+
def delete_old_posts(self):
49
+
self.db_cnx.execute(
50
+
"delete from posts where create_ts < unixepoch('-24 hours')"
53
+
def commit_changes(self):
54
+
self.logger.debug('committing changes')
55
+
self.delete_old_posts()
56
+
self.transaction_commit(self.db_cnx)
57
+
self.wal_checkpoint(self.db_cnx, 'RESTART')
59
+
def serve_feed(self, limit, offset, langs):
60
+
cur = self.db_cnx.execute("""
63
+
order by create_ts desc
66
+
""", dict(limit=limit, offset=offset))
67
+
return [uri for (uri,) in cur]
69
+
def serve_feed_debug(self, limit, offset, langs):
70
+
query = "select * 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