this repo has no description
1import os 2import sys 3import math 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 def process_commit(self, commit): 31 op = commit['op'] 32 if op['action'] != 'create': 33 return 34 35 collection, _ = op['path'].split('/') 36 if collection != 'app.bsky.feed.like': 37 return 38 39 ts = commit['time'] 40 like_subject_uri = op['record']['subject']['uri'] 41 42 with self.db_cnx: 43 self.db_cnx.execute(( 44 "insert into posts (uri, create_ts, update_ts, temperature) " 45 "values (:uri, :ts, :ts, 1) " 46 "on conflict (uri) do update set temperature = temperature + 1, update_ts = :ts" 47 ), dict(uri=like_subject_uri, ts=ts)) 48 49 def run_tasks_minute(self): 50 sys.stdout.write('popular: running minute tasks\n') 51 sys.stdout.flush() 52 53 with self.db_cnx: 54 self.db_cnx.execute( 55 "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' )" 56 ) 57 58 self.db_cnx.pragma('wal_checkpoint(TRUNCATE)') 59 60 def serve_feed(self, limit, offset, langs): 61 cur = self.db_cnx.execute(( 62 "select uri from posts " 63 "order by temperature * exp( " 64 "-1 * ( ( strftime( '%s', 'now' ) - strftime( '%s', create_ts ) ) / 1800.0 ) " 65 ") desc limit :limit offset :offset" 66 ), dict(limit=limit, offset=offset)) 67 return [uri for (uri,) in cur]