+15
-57
feeds/norazone_interesting.py
+15
-57
feeds/norazone_interesting.py
···
1
1
import logging
2
2
3
+
from atproto import Client, models
3
4
import apsw
4
5
import apsw.ext
5
6
···
12
13
FEED_URI = 'at://did:plc:4nsduwlpivpuur4mqkbfvm6a/app.bsky.feed.generator/nz-interesting'
13
14
14
15
def __init__(self):
15
-
self.db_cnx = apsw.Connection('db/nz-interesting.db')
16
-
self.db_cnx.pragma('journal_mode', 'WAL')
17
-
self.db_cnx.pragma('wal_autocheckpoint', '0')
18
-
19
-
with self.db_cnx:
20
-
self.db_cnx.execute("""
21
-
create table if not exists posts (uri text, create_ts timestamp);
22
-
create index if not exists create_ts_idx on posts(create_ts);
23
-
""")
24
-
25
-
self.logger = logging.getLogger('feeds.nz_interesting')
16
+
self.client = Client('https://public.api.bsky.app')
26
17
27
18
def process_commit(self, commit):
28
-
if commit['opType'] != 'c':
29
-
return
30
-
31
-
if commit['collection'] != 'app.bsky.feed.post':
32
-
return
33
-
34
-
record = commit.get('record')
35
-
if record is None:
36
-
return
37
-
38
-
embed = record.get('embed')
39
-
if embed is None:
40
-
return
41
-
42
-
inner_record = embed.get('record')
43
-
if inner_record is None:
44
-
return
45
-
46
-
if inner_record.get('uri') == TARGET_QUOTE_URI:
47
-
self.transaction_begin(self.db_cnx)
48
-
self.logger.debug('found quote post of target, adding to feed')
49
-
uri = 'at://{repo}/app.bsky.feed.post/{rkey}'.format(
50
-
repo = commit['did'],
51
-
rkey = commit['rkey']
52
-
)
53
-
ts = self.safe_timestamp(record.get('createdAt')).timestamp()
54
-
self.db_cnx.execute(
55
-
'insert into posts (uri, create_ts) values (:uri, :ts)',
56
-
dict(uri=uri, ts=ts)
57
-
)
19
+
pass
58
20
59
21
def commit_changes(self):
60
-
self.logger.debug('committing changes')
61
-
self.transaction_commit(self.db_cnx)
62
-
self.wal_checkpoint(self.db_cnx, 'RESTART')
63
-
64
-
def serve_feed(self, limit, offset, langs):
65
-
cur = self.db_cnx.execute(
66
-
'select uri from posts order by create_ts desc limit :limit offset :offset',
67
-
dict(limit=limit, offset=offset)
68
-
)
69
-
return [uri for (uri,) in cur]
22
+
pass
70
23
71
-
def serve_feed_debug(self, limit, offset, langs):
72
-
query = 'select * from posts order by create_ts desc limit :limit offset :offset'
73
-
bindings = dict(limit=limit, offset=offset)
74
-
return apsw.ext.format_query_table(
75
-
self.db_cnx, query, bindings,
76
-
string_sanitize=2, text_width=9999, use_unicode=True
24
+
def serve_feed(self, limit, cursor, langs):
25
+
quotes = self.client.app.bsky.feed.get_quotes(
26
+
models.AppBskyFeedGetQuotes.Params(
27
+
uri = TARGET_QUOTE_URI,
28
+
limit = limit,
29
+
cursor = cursor,
30
+
)
77
31
)
32
+
return {
33
+
'cursor': quotes.cursor,
34
+
'feed': [dict(post=post.uri) for post in quotes.posts],
35
+
}
+9
-4
feedweb.py
+9
-4
feedweb.py
···
18
18
except ValueError:
19
19
limit = 50
20
20
21
-
try:
22
-
offset = int(request.args.get('cursor', 0))
23
-
except ValueError:
24
-
offset = 0
21
+
if 'nz-interesting' in request.args['feed']:
22
+
offset = request.args.get('cursor')
23
+
else:
24
+
try:
25
+
offset = int(request.args.get('cursor', 0))
26
+
except ValueError:
27
+
offset = 0
25
28
26
29
feed_uri = request.args['feed']
27
30
if feed_uri.endswith('-dev'):
···
42
45
return debug, headers
43
46
44
47
posts = feed_manager.serve_feed(feed_uri, limit, offset, langs, debug=False)
48
+
if isinstance(posts, dict):
49
+
return posts
45
50
46
51
if len(posts) < limit:
47
52
return dict(feed=[dict(post=uri) for uri in posts])