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['repo'] != MLBHRS_DID:
61 return
62
63 op = commit['op']
64 if op['action'] != 'create':
65 return
66
67 collection, _ = op['path'].split('/')
68 if collection != 'app.bsky.feed.post':
69 return
70
71 record = op.get('record')
72 if record is None:
73 return
74
75 uri = 'at://{repo}/{path}'.format(
76 repo = commit['repo'],
77 path = op['path']
78 )
79 tags = record.get('tags', [])
80
81 self.logger.debug(f'adding {uri!r} under {tags!r}')
82
83 with self.db_cnx:
84 for tag in tags:
85 self.db_cnx.execute(
86 "insert into posts (uri, tag) values (:uri, :tag)",
87 dict(uri=uri, tag=tag)
88 )
89
90 def commit_changes(self):
91 self.logger.debug('committing changes')
92 self.wal_checkpoint(self.db_cnx, 'RESTART')
93
94 def serve_wildcard_feed(self, feed_uri, limit, offset, langs):
95 prefix, sep, team_abbr = feed_uri.rpartition(':')
96 team_tag = TEAM_ABBR_LOOKUP[team_abbr]
97
98 cur = self.db_cnx.execute("""
99 select uri
100 from posts
101 where tag = :tag
102 order by uri desc
103 limit :limit offset :offset
104 """, dict(tag=team_tag, limit=limit, offset=offset))
105
106 return [uri for (uri,) in cur]
107
108 def serve_wildcard_feed_debug(self, feed_uri, limit, offset, langs):
109 pass