this repo has no description
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

feeds/popular.py: update for new framework

+20 -23
+20 -23
feeds/popular.py
··· 1 1 import os 2 2 import sys 3 3 import math 4 - import sqlite3 4 + import apsw 5 5 6 6 from . import BaseFeed 7 7 ··· 9 9 FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/popular' 10 10 11 11 def __init__(self): 12 + db_fname = '' 12 13 if os.path.isdir('/dev/shm/'): 13 14 os.makedirs('/dev/shm/feedgens/', exist_ok=True) 14 - self.db_cnx = sqlite3.connect('/dev/shm/feedgens/popular.db') 15 + db_fname = '/dev/shm/feedgens/popular.db' 15 16 else: 16 - self.db_cnx = sqlite3.connect('db/popular.db') 17 + db_fname = 'db/popular.db' 17 18 18 - self.db_cnx.create_function('exp', 1, math.exp) 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 + 19 24 with self.db_cnx: 20 - self.db_cnx.executescript( 21 - "pragma journal_mode = WAL;" 22 - "pragma synchronous = OFF;" 23 - "pragma wal_autocheckpoint = 0;" 24 - "create table if not exists posts (uri text, create_ts timestamp, update_ts timestamp, temperature int);" 25 - "create unique index if not exists uri_idx on posts(uri);" 26 - ) 27 - 28 - self.cleanup_checkpoint = 0 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 29 30 30 def process_commit(self, commit): 31 31 op = commit['op'] ··· 46 46 "on conflict (uri) do update set temperature = temperature + 1, update_ts = :ts" 47 47 ), dict(uri=like_subject_uri, ts=ts)) 48 48 49 - self.cleanup_checkpoint += 1 50 - if self.cleanup_checkpoint % 1000 == 0: 51 - sys.stdout.write('popular: running cleanup checkpoint\n') 52 - sys.stdout.flush() 49 + def run_tasks_minute(self): 50 + sys.stdout.write('popular: running minute tasks\n') 51 + sys.stdout.flush() 53 52 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 - 53 + with self.db_cnx: 59 54 self.db_cnx.execute( 60 - "pragma wal_checkpoint(TRUNCATE)" 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' )" 61 56 ) 57 + 58 + self.db_cnx.pragma('wal_checkpoint(TRUNCATE)') 62 59 63 60 def serve_feed(self, limit, offset, langs): 64 61 cur = self.db_cnx.execute((