-
-
Notifications
You must be signed in to change notification settings - Fork 989
/
bluesky.py
563 lines (450 loc) · 17.8 KB
/
bluesky.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# -*- coding: utf-8 -*-
# Copyright 2024 Mike Fährmann
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
"""Extractors for https://bsky.app/"""
from .common import Extractor, Message
from .. import text, util, exception
from ..cache import cache, memcache
BASE_PATTERN = (r"(?:https?://)?"
r"(?:(?:www\.)?(?:c|[fv]x)?bs[ky]y[ex]?\.app|main\.bsky\.dev)")
USER_PATTERN = BASE_PATTERN + r"/profile/([^/?#]+)"
class BlueskyExtractor(Extractor):
"""Base class for bluesky extractors"""
category = "bluesky"
directory_fmt = ("{category}", "{author[handle]}")
filename_fmt = "{createdAt[:19]}_{post_id}_{num}.{extension}"
archive_fmt = "{filename}"
root = "https://bsky.app"
def __init__(self, match):
Extractor.__init__(self, match)
self.user = match.group(1)
def _init(self):
meta = self.config("metadata") or ()
if meta:
if isinstance(meta, str):
meta = meta.replace(" ", "").split(",")
elif not isinstance(meta, (list, tuple)):
meta = ("user", "facets")
self._metadata_user = ("user" in meta)
self._metadata_facets = ("facets" in meta)
self.api = BlueskyAPI(self)
self._user = self._user_did = None
self.instance = self.root.partition("://")[2]
self.videos = self.config("videos", True)
self.quoted = self.config("quoted", False)
def items(self):
for post in self.posts():
if "post" in post:
post = post["post"]
if self._user_did and post["author"]["did"] != self._user_did:
self.log.debug("Skipping %s (repost)", self._pid(post))
continue
embed = post.get("embed")
post.update(post.pop("record"))
while True:
self._prepare(post)
files = self._extract_files(post)
yield Message.Directory, post
if files:
did = post["author"]["did"]
base = (
"{}/xrpc/com.atproto.sync.getBlob?did={}&cid=".format(
self.api.service_endpoint(did), did))
for post["num"], file in enumerate(files, 1):
post.update(file)
yield Message.Url, base + file["filename"], post
if not self.quoted or not embed or "record" not in embed:
break
quote = embed["record"]
if "record" in quote:
quote = quote["record"]
value = quote.pop("value", None)
if value is None:
break
quote["quote_id"] = self._pid(post)
quote["quote_by"] = post["author"]
embed = quote.get("embed")
quote.update(value)
post = quote
def posts(self):
return ()
def _pid(self, post):
return post["uri"].rpartition("/")[2]
@memcache(keyarg=1)
def _instance(self, handle):
return ".".join(handle.rsplit(".", 2)[-2:])
def _prepare(self, post):
author = post["author"]
author["instance"] = self._instance(author["handle"])
if self._metadata_facets:
if "facets" in post:
post["hashtags"] = tags = []
post["mentions"] = dids = []
post["uris"] = uris = []
for facet in post["facets"]:
features = facet["features"][0]
if "tag" in features:
tags.append(features["tag"])
elif "did" in features:
dids.append(features["did"])
elif "uri" in features:
uris.append(features["uri"])
else:
post["hashtags"] = post["mentions"] = post["uris"] = ()
if self._metadata_user:
post["user"] = self._user or author
post["instance"] = self.instance
post["post_id"] = self._pid(post)
post["date"] = text.parse_datetime(
post["createdAt"][:19], "%Y-%m-%dT%H:%M:%S")
def _extract_files(self, post):
if "embed" not in post:
post["count"] = 0
return ()
files = []
media = post["embed"]
if "media" in media:
media = media["media"]
if "images" in media:
for image in media["images"]:
files.append(self._extract_media(image, "image"))
if "video" in media and self.videos:
files.append(self._extract_media(media, "video"))
post["count"] = len(files)
return files
def _extract_media(self, media, key):
try:
aspect = media["aspectRatio"]
width = aspect["width"]
height = aspect["height"]
except KeyError:
width = height = 0
data = media[key]
try:
cid = data["ref"]["$link"]
except KeyError:
cid = data["cid"]
return {
"description": media.get("alt") or "",
"width" : width,
"height" : height,
"filename" : cid,
"extension" : data["mimeType"].rpartition("/")[2],
}
def _make_post(self, actor, kind):
did = self.api._did_from_actor(actor)
profile = self.api.get_profile(did)
if kind not in profile:
return ()
cid = profile[kind].rpartition("/")[2].partition("@")[0]
return ({
"post": {
"embed": {"images": [{
"alt": kind,
"image": {
"$type" : "blob",
"ref" : {"$link": cid},
"mimeType": "image/jpeg",
"size" : 0,
},
"aspectRatio": {
"width" : 1000,
"height": 1000,
},
}]},
"author" : profile,
"record" : (),
"createdAt": "",
"uri" : cid,
},
},)
class BlueskyUserExtractor(BlueskyExtractor):
subcategory = "user"
pattern = USER_PATTERN + r"$"
example = "https://bsky.app/profile/HANDLE"
def initialize(self):
pass
def items(self):
base = "{}/profile/{}/".format(self.root, self.user)
return self._dispatch_extractors((
(BlueskyInfoExtractor , base + "info"),
(BlueskyAvatarExtractor , base + "avatar"),
(BlueskyBackgroundExtractor, base + "banner"),
(BlueskyPostsExtractor , base + "posts"),
(BlueskyRepliesExtractor , base + "replies"),
(BlueskyMediaExtractor , base + "media"),
(BlueskyLikesExtractor , base + "likes"),
), ("media",))
class BlueskyPostsExtractor(BlueskyExtractor):
subcategory = "posts"
pattern = USER_PATTERN + r"/posts"
example = "https://bsky.app/profile/HANDLE/posts"
def posts(self):
return self.api.get_author_feed(self.user, "posts_and_author_threads")
class BlueskyRepliesExtractor(BlueskyExtractor):
subcategory = "replies"
pattern = USER_PATTERN + r"/replies"
example = "https://bsky.app/profile/HANDLE/replies"
def posts(self):
return self.api.get_author_feed(self.user, "posts_with_replies")
class BlueskyMediaExtractor(BlueskyExtractor):
subcategory = "media"
pattern = USER_PATTERN + r"/media"
example = "https://bsky.app/profile/HANDLE/media"
def posts(self):
return self.api.get_author_feed(self.user, "posts_with_media")
class BlueskyLikesExtractor(BlueskyExtractor):
subcategory = "likes"
pattern = USER_PATTERN + r"/likes"
example = "https://bsky.app/profile/HANDLE/likes"
def posts(self):
return self.api.get_actor_likes(self.user)
class BlueskyFeedExtractor(BlueskyExtractor):
subcategory = "feed"
pattern = USER_PATTERN + r"/feed/([^/?#]+)"
example = "https://bsky.app/profile/HANDLE/feed/NAME"
def __init__(self, match):
BlueskyExtractor.__init__(self, match)
self.feed = match.group(2)
def posts(self):
return self.api.get_feed(self.user, self.feed)
class BlueskyListExtractor(BlueskyExtractor):
subcategory = "list"
pattern = USER_PATTERN + r"/lists/([^/?#]+)"
example = "https://bsky.app/profile/HANDLE/lists/ID"
def __init__(self, match):
BlueskyExtractor.__init__(self, match)
self.list = match.group(2)
def posts(self):
return self.api.get_list_feed(self.user, self.list)
class BlueskyFollowingExtractor(BlueskyExtractor):
subcategory = "following"
pattern = USER_PATTERN + r"/follows"
example = "https://bsky.app/profile/HANDLE/follows"
def items(self):
for user in self.api.get_follows(self.user):
url = "https://bsky.app/profile/" + user["did"]
user["_extractor"] = BlueskyUserExtractor
yield Message.Queue, url, user
class BlueskyPostExtractor(BlueskyExtractor):
subcategory = "post"
pattern = USER_PATTERN + r"/post/([^/?#]+)"
example = "https://bsky.app/profile/HANDLE/post/ID"
def __init__(self, match):
BlueskyExtractor.__init__(self, match)
self.post_id = match.group(2)
def posts(self):
return self.api.get_post_thread(self.user, self.post_id)
class BlueskyInfoExtractor(BlueskyExtractor):
subcategory = "info"
pattern = USER_PATTERN + r"/info"
example = "https://bsky.app/profile/HANDLE/info"
def items(self):
self._metadata_user = True
self.api._did_from_actor(self.user)
return iter(((Message.Directory, self._user),))
class BlueskyAvatarExtractor(BlueskyExtractor):
subcategory = "avatar"
filename_fmt = "avatar_{post_id}.{extension}"
pattern = USER_PATTERN + r"/avatar"
example = "https://bsky.app/profile/HANDLE/avatar"
def posts(self):
return self._make_post(self.user, "avatar")
class BlueskyBackgroundExtractor(BlueskyExtractor):
subcategory = "background"
filename_fmt = "background_{post_id}.{extension}"
pattern = USER_PATTERN + r"/ba(?:nner|ckground)"
example = "https://bsky.app/profile/HANDLE/banner"
def posts(self):
return self._make_post(self.user, "banner")
class BlueskySearchExtractor(BlueskyExtractor):
subcategory = "search"
pattern = BASE_PATTERN + r"/search(?:/|\?q=)(.+)"
example = "https://bsky.app/search?q=QUERY"
def posts(self):
query = text.unquote(self.user.replace("+", " "))
return self.api.search_posts(query)
class BlueskyHashtagExtractor(BlueskyExtractor):
subcategory = "hashtag"
pattern = BASE_PATTERN + r"/hashtag/([^/?#]+)(?:/(top|latest))?"
example = "https://bsky.app/hashtag/NAME"
def posts(self):
return self.api.search_posts("#"+self.user, self.groups[1])
class BlueskyAPI():
"""Interface for the Bluesky API
https://www.docs.bsky.app/docs/category/http-reference
"""
def __init__(self, extractor):
self.extractor = extractor
self.log = extractor.log
self.headers = {"Accept": "application/json"}
self.username, self.password = extractor._get_auth_info()
if self.username:
self.root = "https://bsky.social"
else:
self.root = "https://api.bsky.app"
self.authenticate = util.noop
def get_actor_likes(self, actor):
endpoint = "app.bsky.feed.getActorLikes"
params = {
"actor": self._did_from_actor(actor),
"limit": "100",
}
return self._pagination(endpoint, params)
def get_author_feed(self, actor, filter="posts_and_author_threads"):
endpoint = "app.bsky.feed.getAuthorFeed"
params = {
"actor" : self._did_from_actor(actor, True),
"filter": filter,
"limit" : "100",
}
return self._pagination(endpoint, params)
def get_feed(self, actor, feed):
endpoint = "app.bsky.feed.getFeed"
params = {
"feed" : "at://{}/app.bsky.feed.generator/{}".format(
self._did_from_actor(actor), feed),
"limit": "100",
}
return self._pagination(endpoint, params)
def get_follows(self, actor):
endpoint = "app.bsky.graph.getFollows"
params = {
"actor": self._did_from_actor(actor),
"limit": "100",
}
return self._pagination(endpoint, params, "follows")
def get_list_feed(self, actor, list):
endpoint = "app.bsky.feed.getListFeed"
params = {
"list" : "at://{}/app.bsky.graph.list/{}".format(
self._did_from_actor(actor), list),
"limit": "100",
}
return self._pagination(endpoint, params)
def get_post_thread(self, actor, post_id):
endpoint = "app.bsky.feed.getPostThread"
params = {
"uri": "at://{}/app.bsky.feed.post/{}".format(
self._did_from_actor(actor), post_id),
"depth" : self.extractor.config("depth", "0"),
"parentHeight": "0",
}
thread = self._call(endpoint, params)["thread"]
if "replies" not in thread:
return (thread,)
index = 0
posts = [thread]
while index < len(posts):
post = posts[index]
if "replies" in post:
posts.extend(post["replies"])
index += 1
return posts
@memcache(keyarg=1)
def get_profile(self, did):
endpoint = "app.bsky.actor.getProfile"
params = {"actor": did}
return self._call(endpoint, params)
@memcache(keyarg=1)
def resolve_handle(self, handle):
endpoint = "com.atproto.identity.resolveHandle"
params = {"handle": handle}
return self._call(endpoint, params)["did"]
@memcache(keyarg=1)
def service_endpoint(self, did):
if did.startswith('did:web:'):
url = "https://" + did[8:] + "/.well-known/did.json"
else:
url = "https://plc.directory/" + did
try:
data = self.extractor.request(url).json()
for service in data["service"]:
if service["type"] == "AtprotoPersonalDataServer":
return service["serviceEndpoint"]
except Exception:
pass
return "https://bsky.social"
def search_posts(self, query, sort=None):
endpoint = "app.bsky.feed.searchPosts"
params = {
"q" : query,
"limit": "100",
"sort" : sort,
}
return self._pagination(endpoint, params, "posts")
def _did_from_actor(self, actor, user_did=False):
if actor.startswith("did:"):
did = actor
else:
did = self.resolve_handle(actor)
extr = self.extractor
if user_did and not extr.config("reposts", False):
extr._user_did = did
if extr._metadata_user:
extr._user = user = self.get_profile(did)
user["instance"] = extr._instance(user["handle"])
return did
def authenticate(self):
self.headers["Authorization"] = self._authenticate_impl(self.username)
@cache(maxage=3600, keyarg=1)
def _authenticate_impl(self, username):
refresh_token = _refresh_token_cache(username)
if refresh_token:
self.log.info("Refreshing access token for %s", username)
endpoint = "com.atproto.server.refreshSession"
headers = {"Authorization": "Bearer " + refresh_token}
data = None
else:
self.log.info("Logging in as %s", username)
endpoint = "com.atproto.server.createSession"
headers = None
data = {
"identifier": username,
"password" : self.password,
}
url = "{}/xrpc/{}".format(self.root, endpoint)
response = self.extractor.request(
url, method="POST", headers=headers, json=data, fatal=None)
data = response.json()
if response.status_code != 200:
self.log.debug("Server response: %s", data)
raise exception.AuthenticationError('"{}: {}"'.format(
data.get("error"), data.get("message")))
_refresh_token_cache.update(self.username, data["refreshJwt"])
return "Bearer " + data["accessJwt"]
def _call(self, endpoint, params):
url = "{}/xrpc/{}".format(self.root, endpoint)
while True:
self.authenticate()
response = self.extractor.request(
url, params=params, headers=self.headers, fatal=None)
if response.status_code < 400:
return response.json()
if response.status_code == 429:
until = response.headers.get("RateLimit-Reset")
self.extractor.wait(until=until)
continue
try:
data = response.json()
msg = "API request failed ('{}: {}')".format(
data["error"], data["message"])
except Exception:
msg = "API request failed ({} {})".format(
response.status_code, response.reason)
self.extractor.log.debug("Server response: %s", response.text)
raise exception.StopExtraction(msg)
def _pagination(self, endpoint, params, key="feed"):
while True:
data = self._call(endpoint, params)
yield from data[key]
cursor = data.get("cursor")
if not cursor:
return
params["cursor"] = cursor
@cache(maxage=84*86400, keyarg=0)
def _refresh_token_cache(username):
return None