this repo has no description
1import logging 2import os 3 4import apsw 5 6from . import BaseFeed 7 8class PopularFeed(BaseFeed): 9 FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/popular' 10 11 def __init__(self): 12 db_fname = '' 13 if os.path.isdir('/dev/shm/'): 14 os.makedirs('/dev/shm/feedgens/', exist_ok=True) 15 db_fname = '/dev/shm/feedgens/popular.db' 16 else: 17 db_fname = 'db/popular.db' 18 19 self.db_cnx = apsw.Connection(db_fname) 20 self.db_cnx.pragma('journal_mode', 'WAL') 21 self.db_cnx.pragma('synchronous', 'OFF') 22 self.db_cnx.pragma('wal_autocheckpoint', '0') 23 24 with self.db_cnx: 25 self.db_cnx.execute(""" 26 create table if not exists posts (uri text, create_ts timestamp, update_ts timestamp, temperature int); 27 create unique index if not exists uri_idx on posts(uri); 28 """) 29 30 self.logger = logging.getLogger('feeds.popular') 31 32 def process_commit(self, commit): 33 op = commit['op'] 34 if op['action'] != 'create': 35 return 36 37 collection, _ = op['path'].split('/') 38 if collection != 'app.bsky.feed.like': 39 return 40 41 ts = commit['time'] 42 like_subject_uri = op['record']['subject']['uri'] 43 44 with self.db_cnx: 45 self.db_cnx.execute(( 46 "insert into posts (uri, create_ts, update_ts, temperature) " 47 "values (:uri, :ts, :ts, 1) " 48 "on conflict (uri) do update set temperature = temperature + 1, update_ts = :ts" 49 ), dict(uri=like_subject_uri, ts=ts)) 50 51 def run_tasks_minute(self): 52 self.logger.debug('running minute tasks') 53 54 with self.db_cnx: 55 self.db_cnx.execute( 56 "delete from posts where temperature * exp( -1 * ( ( strftime( '%s', 'now' ) - strftime( '%s', create_ts ) ) / 1800.0 ) ) < 1.0 and strftime( '%s', create_ts ) < strftime( '%s', 'now', '-15 minutes' )" 57 ) 58 59 self.db_cnx.pragma('wal_checkpoint(TRUNCATE)') 60 61 def serve_feed(self, limit, offset, langs): 62 cur = self.db_cnx.execute(( 63 "select uri from posts " 64 "order by temperature * exp( " 65 "-1 * ( ( strftime( '%s', 'now' ) - strftime( '%s', create_ts ) ) / 1800.0 ) " 66 ") desc limit :limit offset :offset" 67 ), dict(limit=limit, offset=offset)) 68 return [uri for (uri,) in cur]