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
|
# -*- coding: utf-8 -*-
# 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 Misskey instances"""
from .common import BaseExtractor, Message, Dispatch
from .. import text, dt, exception
from ..cache import memcache
class MisskeyExtractor(BaseExtractor):
"""Base class for Misskey extractors"""
basecategory = "misskey"
directory_fmt = ("misskey", "{instance}", "{user[username]}")
filename_fmt = "{category}_{id}_{file[id]}.{extension}"
archive_fmt = "{id}_{file[id]}"
def _init(self):
self.api = MisskeyAPI(self)
self.instance = self.root.rpartition("://")[2]
self.renotes = True if self.config("renotes", False) else False
self.replies = True if self.config("replies", True) else False
def items(self):
for note in self.notes():
if "note" in note:
note = note["note"]
files = note.pop("files") or []
if renote := note.get("renote"):
if not self.renotes:
self.log.debug("Skipping %s (renote)", note["id"])
continue
files.extend(renote.get("files") or ())
if reply := note.get("reply"):
if not self.replies:
self.log.debug("Skipping %s (reply)", note["id"])
continue
files.extend(reply.get("files") or ())
note["instance"] = self.instance
note["instance_remote"] = note["user"]["host"]
note["count"] = len(files)
note["date"] = self.parse_datetime_iso(note["createdAt"])
yield Message.Directory, "", note
for note["num"], file in enumerate(files, 1):
file["date"] = self.parse_datetime_iso(file["createdAt"])
note["file"] = file
url = file["url"]
yield Message.Url, url, text.nameext_from_url(url, note)
def notes(self):
"""Return an iterable containing all relevant Note objects"""
return ()
def _make_note(self, type, user, url):
# extract real URL from potential proxy
path, sep, query = url.partition("?")
if sep:
url = text.parse_query(query).get("url") or path
return {
"id" : type,
"user" : user,
"files": ({
"id" : url.rpartition("/")[2].partition(".")[0], # ID from URL
"url": url,
"createdAt": "",
},),
"createdAt": "",
}
BASE_PATTERN = MisskeyExtractor.update({
"misskey.io": {
"root": "https://misskey.io",
"pattern": r"misskey\.io",
},
"misskey.design": {
"root": "https://misskey.design",
"pattern": r"misskey\.design",
},
"misskey.art": {
"root": "https://misskey.art",
"pattern": r"misskey\.art",
},
"lesbian.energy": {
"root": "https://lesbian.energy",
"pattern": r"lesbian\.energy",
},
"sushi.ski": {
"root": "https://sushi.ski",
"pattern": r"sushi\.ski",
},
})
class MisskeyUserExtractor(Dispatch, MisskeyExtractor):
"""Extractor for all images of a Misskey user"""
subcategory = "user"
pattern = BASE_PATTERN + r"/@([^/?#]+)/?$"
example = "https://misskey.io/@USER"
def items(self):
base = f"{self.root}/@{self.groups[-1]}/"
return self._dispatch_extractors((
(MisskeyInfoExtractor , base + "info"),
(MisskeyAvatarExtractor , base + "avatar"),
(MisskeyBackgroundExtractor, base + "banner"),
(MisskeyNotesExtractor , base + "notes"),
), ("notes",))
class MisskeyNotesExtractor(MisskeyExtractor):
"""Extractor for a Misskey user's notes"""
subcategory = "notes"
pattern = BASE_PATTERN + r"/@([^/?#]+)/notes"
example = "https://misskey.io/@USER/notes"
def notes(self):
return self.api.users_notes(self.api.user_id_by_username(
self.groups[-1]))
class MisskeyInfoExtractor(MisskeyExtractor):
"""Extractor for a Misskey user's profile data"""
subcategory = "info"
pattern = BASE_PATTERN + r"/@([^/?#]+)/info"
example = "https://misskey.io/@USER/info"
def items(self):
user = self.api.users_show(self.groups[-1])
return iter(((Message.Directory, "", user),))
class MisskeyAvatarExtractor(MisskeyExtractor):
"""Extractor for a Misskey user's avatar"""
subcategory = "avatar"
pattern = BASE_PATTERN + r"/@([^/?#]+)/avatar"
example = "https://misskey.io/@USER/avatar"
def notes(self):
user = self.api.users_show(self.groups[-1])
url = user.get("avatarUrl")
return (self._make_note("avatar", user, url),) if url else ()
class MisskeyBackgroundExtractor(MisskeyExtractor):
"""Extractor for a Misskey user's banner image"""
subcategory = "background"
pattern = BASE_PATTERN + r"/@([^/?#]+)/ba(?:nner|ckground)"
example = "https://misskey.io/@USER/banner"
def notes(self):
user = self.api.users_show(self.groups[-1])
url = user.get("bannerUrl")
return (self._make_note("background", user, url),) if url else ()
class MisskeyFollowingExtractor(MisskeyExtractor):
"""Extractor for followed Misskey users"""
subcategory = "following"
pattern = BASE_PATTERN + r"/@([^/?#]+)/following"
example = "https://misskey.io/@USER/following"
def items(self):
user_id = self.api.user_id_by_username(self.groups[-1])
for user in self.api.users_following(user_id):
user = user["followee"]
url = f"{self.root}/@{user['username']}"
if (host := user["host"]) is not None:
url = f"{url}@{host}"
user["_extractor"] = MisskeyUserExtractor
yield Message.Queue, url, user
class MisskeyNoteExtractor(MisskeyExtractor):
"""Extractor for images from a Note"""
subcategory = "note"
pattern = BASE_PATTERN + r"/notes/(\w+)"
example = "https://misskey.io/notes/98765"
def notes(self):
return (self.api.notes_show(self.groups[-1]),)
class MisskeyFavoriteExtractor(MisskeyExtractor):
"""Extractor for favorited notes"""
subcategory = "favorite"
pattern = BASE_PATTERN + r"/(?:my|api/i)/favorites"
example = "https://misskey.io/my/favorites"
def notes(self):
return self.api.i_favorites()
class MisskeyAPI():
"""Interface for Misskey API
https://github.com/misskey-dev/misskey
https://misskey-hub.net/en/docs/api/
https://misskey-hub.net/docs/api/endpoints.html
"""
def __init__(self, extractor):
self.root = extractor.root
self.extractor = extractor
self.access_token = extractor.config("access-token")
def user_id_by_username(self, username):
return self.users_show(username)["id"]
def users_following(self, user_id):
endpoint = "/users/following"
data = {"userId": user_id}
return self._pagination(endpoint, data)
def users_notes(self, user_id):
endpoint = "/users/notes"
data = {"userId": user_id}
return self._pagination(endpoint, data)
@memcache(keyarg=1)
def users_show(self, username):
endpoint = "/users/show"
username, _, host = username.partition("@")
data = {"username": username, "host": host or None}
return self._call(endpoint, data)
def notes_show(self, note_id):
endpoint = "/notes/show"
data = {"noteId": note_id}
return self._call(endpoint, data)
def i_favorites(self):
endpoint = "/i/favorites"
if not self.access_token:
raise exception.AuthenticationError()
data = {"i": self.access_token}
return self._pagination(endpoint, data)
def _call(self, endpoint, data):
url = f"{self.root}/api{endpoint}"
return self.extractor.request_json(url, method="POST", json=data)
def _pagination(self, endpoint, data):
extr = self.extractor
data["limit"] = 100
data["withRenotes"] = extr.renotes
data["withFiles"] = False if extr.config("text-posts") else True
date_min, date_max = extr._get_date_min_max()
if (order := extr.config("order-posts")) and \
order[0] in ("a", "r"):
key = "sinceId"
data["sinceDate"] = 1 if date_min is None else date_min * 1000
date_stop = None if date_max is None else date_max
else:
key = "untilId"
date_stop = None
if date_min is not None:
data["sinceDate"] = date_min * 1000
if date_max is None:
# ensure notes are returned in descending order
data["untilDate"] = (int(dt.time.time()) + 1000) * 1000
if date_max is not None:
data["untilDate"] = date_max * 1000
while True:
notes = self._call(endpoint, data)
if not notes:
return
elif date_stop is not None and dt.to_ts(dt.parse_iso(
notes[-1]["createdAt"])) > date_stop:
for idx, note in enumerate(notes):
if dt.to_ts(dt.parse_iso(note["createdAt"])) > date_stop:
yield from notes[:idx]
return
else:
yield from notes
data[key] = notes[-1]["id"]
|