this repo has no description
1import logging 2 3import apsw 4import apsw.ext 5 6from . import BaseFeed 7 8MLBHRS_DID = 'did:plc:pnksqegntq5t3o7pusp2idx3' 9 10TEAM_ABBR_LOOKUP = { 11 "OAK":"OaklandAthletics", 12 "PIT":"PittsburghPirates", 13 "SDN":"SanDiegoPadres", 14 "SEA":"SeattleMariners", 15 "SFN":"SanFranciscoGiants", 16 "SLN":"StLouisCardinals", 17 "TBA":"TampaBayRays", 18 "TEX":"TexasRangers", 19 "TOR":"TorontoBlueJays", 20 "MIN":"MinnesotaTwins", 21 "PHI":"PhiladelphiaPhillies", 22 "ATL":"AtlantaBraves", 23 "CHA":"ChicagoWhiteSox", 24 "MIA":"MiamiMarlins", 25 "NYA":"NewYorkYankees", 26 "MIL":"MilwaukeeBrewers", 27 "LAA":"LosAngelesAngels", 28 "ARI":"ArizonaDiamondbacks", 29 "BAL":"BaltimoreOrioles", 30 "BOS":"BostonRedSox", 31 "CHN":"ChicagoCubs", 32 "CIN":"CincinnatiReds", 33 "CLE":"ClevelandGuardians", 34 "COL":"ColoradoRockies", 35 "DET":"DetroitTigers", 36 "HOU":"HoustonAstros", 37 "KCA":"KansasCityRoyals", 38 "LAN":"LosAngelesDodgers", 39 "WAS":"WashingtonNationals", 40 "NYN":"NewYorkMets", 41} 42 43class HomeRunsTeamFeed(BaseFeed): 44 FEED_URI = 'at://did:plc:pnksqegntq5t3o7pusp2idx3/app.bsky.feed.generator/team:*' 45 46 def __init__(self): 47 self.db_cnx = apsw.Connection('db/homeruns.db') 48 self.db_cnx.pragma('journal_mode', 'WAL') 49 self.db_cnx.pragma('wal_autocheckpoint', '0') 50 51 with self.db_cnx: 52 self.db_cnx.execute(""" 53 create table if not exists posts (uri text, tag text); 54 create index if not exists tag_idx on posts(tag); 55 """) 56 57 self.logger = logging.getLogger('feeds.homeruns') 58 59 def process_commit(self, commit): 60 if commit['did'] != MLBHRS_DID: 61 return 62 63 if commit['opType'] != 'c': 64 return 65 66 if commit['collection'] != 'app.bsky.feed.post': 67 return 68 69 record = commit.get('record') 70 if record is None: 71 return 72 73 uri = 'at://{repo}/app.bsky.feed.post/{rkey}'.format( 74 repo = commit['did'], 75 rkey = commit['rkey'] 76 ) 77 tags = record.get('tags', []) 78 79 self.logger.debug(f'adding {uri!r} under {tags!r}') 80 81 with self.db_cnx: 82 for tag in tags: 83 self.db_cnx.execute( 84 "insert into posts (uri, tag) values (:uri, :tag)", 85 dict(uri=uri, tag=tag) 86 ) 87 88 def commit_changes(self): 89 self.logger.debug('committing changes') 90 self.wal_checkpoint(self.db_cnx, 'RESTART') 91 92 def serve_wildcard_feed(self, feed_uri, limit, offset, langs): 93 prefix, sep, team_abbr = feed_uri.rpartition(':') 94 team_tag = TEAM_ABBR_LOOKUP[team_abbr] 95 96 cur = self.db_cnx.execute(""" 97 select uri 98 from posts 99 where tag = :tag 100 order by uri desc 101 limit :limit offset :offset 102 """, dict(tag=team_tag, limit=limit, offset=offset)) 103 104 return [uri for (uri,) in cur] 105 106 def serve_wildcard_feed_debug(self, feed_uri, limit, offset, langs): 107 pass