| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# This program is free software; you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License version 2 as |
| 5 |
# published by the Free Software Foundation. |
| 6 |
|
| 7 |
"""Extractors for Misskey instances""" |
| 8 |
|
| 9 |
from .common import BaseExtractor, Message, Dispatch |
| 10 |
from .. import text, dt |
| 11 |
|
| 12 |
|
| 13 |
class MisskeyExtractor(BaseExtractor): |
| 14 |
"""Base class for Misskey extractors""" |
| 15 |
basecategory = "misskey" |
| 16 |
directory_fmt = ("misskey", "{instance}", "{user[username]}") |
| 17 |
filename_fmt = "{category}_{id}_{file[id]}.{extension}" |
| 18 |
archive_fmt = "{id}_{file[id]}" |
| 19 |
|
| 20 |
def _init(self): |
| 21 |
self.api = MisskeyAPI(self) |
| 22 |
self.instance = self.root.rpartition("://")[2] |
| 23 |
self.renotes = True if self.config("renotes", False) else False |
| 24 |
self.replies = True if self.config("replies", True) else False |
| 25 |
|
| 26 |
def items(self): |
| 27 |
for note in self.notes(): |
| 28 |
if "note" in note: |
| 29 |
note = note["note"] |
| 30 |
files = note.pop("files") or [] |
| 31 |
if renote := note.get("renote"): |
| 32 |
if not self.renotes: |
| 33 |
self.log.debug("Skipping %s (renote)", note["id"]) |
| 34 |
continue |
| 35 |
files.extend(renote.get("files") or ()) |
| 36 |
|
| 37 |
if reply := note.get("reply"): |
| 38 |
if not self.replies: |
| 39 |
self.log.debug("Skipping %s (reply)", note["id"]) |
| 40 |
continue |
| 41 |
files.extend(reply.get("files") or ()) |
| 42 |
|
| 43 |
note["instance"] = self.instance |
| 44 |
note["instance_remote"] = note["user"]["host"] |
| 45 |
note["count"] = len(files) |
| 46 |
note["date"] = self.parse_datetime_iso(note["createdAt"]) |
| 47 |
|
| 48 |
yield Message.Directory, "", note |
| 49 |
for note["num"], file in enumerate(files, 1): |
| 50 |
file["date"] = self.parse_datetime_iso(file["createdAt"]) |
| 51 |
note["file"] = file |
| 52 |
url = file["url"] |
| 53 |
yield Message.Url, url, text.nameext_from_url(url, note) |
| 54 |
|
| 55 |
def notes(self): |
| 56 |
"""Return an iterable containing all relevant Note objects""" |
| 57 |
return () |
| 58 |
|
| 59 |
def _make_note(self, type, user, url): |
| 60 |
# extract real URL from potential proxy |
| 61 |
path, sep, query = url.partition("?") |
| 62 |
if sep: |
| 63 |
url = text.parse_query(query).get("url") or path |
| 64 |
|
| 65 |
return { |
| 66 |
"id" : type, |
| 67 |
"user" : user, |
| 68 |
"files": ({ |
| 69 |
"id" : url.rpartition("/")[2].partition(".")[0], # ID from URL |
| 70 |
"url": url, |
| 71 |
"createdAt": "", |
| 72 |
},), |
| 73 |
"createdAt": "", |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
BASE_PATTERN = MisskeyExtractor.update({ |
| 78 |
"misskey.io": { |
| 79 |
"root": "https://misskey.io", |
| 80 |
"pattern": r"misskey\.io", |
| 81 |
}, |
| 82 |
"misskey.design": { |
| 83 |
"root": "https://misskey.design", |
| 84 |
"pattern": r"misskey\.design", |
| 85 |
}, |
| 86 |
"misskey.art": { |
| 87 |
"root": "https://misskey.art", |
| 88 |
"pattern": r"misskey\.art", |
| 89 |
}, |
| 90 |
"sushi.ski": { |
| 91 |
"root": "https://sushi.ski", |
| 92 |
"pattern": r"sushi\.ski", |
| 93 |
}, |
| 94 |
}) |
| 95 |
|
| 96 |
|
| 97 |
class MisskeyUserExtractor(Dispatch, MisskeyExtractor): |
| 98 |
"""Extractor for all images of a Misskey user""" |
| 99 |
subcategory = "user" |
| 100 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/?$" |
| 101 |
example = "https://misskey.io/@USER" |
| 102 |
|
| 103 |
def items(self): |
| 104 |
base = f"{self.root}/@{self.groups[-1]}/" |
| 105 |
return self._dispatch_extractors(( |
| 106 |
(MisskeyInfoExtractor , base + "info"), |
| 107 |
(MisskeyAvatarExtractor , base + "avatar"), |
| 108 |
(MisskeyBackgroundExtractor, base + "banner"), |
| 109 |
(MisskeyNotesExtractor , base + "notes"), |
| 110 |
), ("notes",)) |
| 111 |
|
| 112 |
|
| 113 |
class MisskeyNotesExtractor(MisskeyExtractor): |
| 114 |
"""Extractor for a Misskey user's notes""" |
| 115 |
subcategory = "notes" |
| 116 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/notes" |
| 117 |
example = "https://misskey.io/@USER/notes" |
| 118 |
|
| 119 |
def notes(self): |
| 120 |
return self.api.users_notes(self.api.user_id_by_username( |
| 121 |
self.groups[-1])) |
| 122 |
|
| 123 |
|
| 124 |
class MisskeyInfoExtractor(MisskeyExtractor): |
| 125 |
"""Extractor for a Misskey user's profile data""" |
| 126 |
subcategory = "info" |
| 127 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/info" |
| 128 |
example = "https://misskey.io/@USER/info" |
| 129 |
|
| 130 |
def items(self): |
| 131 |
user = self.api.users_show(self.groups[-1]) |
| 132 |
return iter(((Message.Directory, "", user.copy()),)) |
| 133 |
|
| 134 |
|
| 135 |
class MisskeyAvatarExtractor(MisskeyExtractor): |
| 136 |
"""Extractor for a Misskey user's avatar""" |
| 137 |
subcategory = "avatar" |
| 138 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/avatar" |
| 139 |
example = "https://misskey.io/@USER/avatar" |
| 140 |
|
| 141 |
def notes(self): |
| 142 |
user = self.api.users_show(self.groups[-1]) |
| 143 |
url = user.get("avatarUrl") |
| 144 |
return (self._make_note("avatar", user, url),) if url else () |
| 145 |
|
| 146 |
|
| 147 |
class MisskeyBackgroundExtractor(MisskeyExtractor): |
| 148 |
"""Extractor for a Misskey user's banner image""" |
| 149 |
subcategory = "background" |
| 150 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/ba(?:nner|ckground)" |
| 151 |
example = "https://misskey.io/@USER/banner" |
| 152 |
|
| 153 |
def notes(self): |
| 154 |
user = self.api.users_show(self.groups[-1]) |
| 155 |
url = user.get("bannerUrl") |
| 156 |
return (self._make_note("background", user, url),) if url else () |
| 157 |
|
| 158 |
|
| 159 |
class MisskeyFollowingExtractor(MisskeyExtractor): |
| 160 |
"""Extractor for followed Misskey users""" |
| 161 |
subcategory = "following" |
| 162 |
pattern = BASE_PATTERN + r"/@([^/?#]+)/following" |
| 163 |
example = "https://misskey.io/@USER/following" |
| 164 |
|
| 165 |
def items(self): |
| 166 |
user_id = self.api.user_id_by_username(self.groups[-1]) |
| 167 |
for user in self.api.users_following(user_id): |
| 168 |
user = user["followee"] |
| 169 |
url = f"{self.root}/@{user['username']}" |
| 170 |
if (host := user["host"]) is not None: |
| 171 |
url = f"{url}@{host}" |
| 172 |
user["_extractor"] = MisskeyUserExtractor |
| 173 |
yield Message.Queue, url, user |
| 174 |
|
| 175 |
|
| 176 |
class MisskeyNoteExtractor(MisskeyExtractor): |
| 177 |
"""Extractor for images from a Note""" |
| 178 |
subcategory = "note" |
| 179 |
pattern = BASE_PATTERN + r"/notes/(\w+)" |
| 180 |
example = "https://misskey.io/notes/98765" |
| 181 |
|
| 182 |
def notes(self): |
| 183 |
return (self.api.notes_show(self.groups[-1]),) |
| 184 |
|
| 185 |
|
| 186 |
class MisskeyFavoriteExtractor(MisskeyExtractor): |
| 187 |
"""Extractor for favorited notes""" |
| 188 |
subcategory = "favorite" |
| 189 |
pattern = BASE_PATTERN + r"/(?:my|api/i)/favorites" |
| 190 |
example = "https://misskey.io/my/favorites" |
| 191 |
|
| 192 |
def notes(self): |
| 193 |
return self.api.i_favorites() |
| 194 |
|
| 195 |
|
| 196 |
class MisskeyEmojiExtractor(MisskeyExtractor): |
| 197 |
"""Extractor for instance emojis""" |
| 198 |
subcategory = "emoji" |
| 199 |
directory_fmt = ("misskey", "{instance}", "Emoji", "{emoji_category|''}") |
| 200 |
filename_fmt = "{name|filename}.{extension}" |
| 201 |
archive_fmt = "E/{filename}" |
| 202 |
pattern = BASE_PATTERN + r"/about" |
| 203 |
example = "https://misskey.io/about" |
| 204 |
|
| 205 |
def items(self): |
| 206 |
emojis = self.api.emojis() |
| 207 |
data = { |
| 208 |
"instance": self.instance, |
| 209 |
"count" : len(emojis), |
| 210 |
} |
| 211 |
|
| 212 |
for data["num"], emoji in enumerate(emojis, 1): |
| 213 |
url = emoji["url"] |
| 214 |
emoji["emoji_category"] = emoji.pop("category", None) |
| 215 |
emoji.update(data) |
| 216 |
yield Message.Directory, "", emoji |
| 217 |
yield Message.Url, url, text.nameext_from_url(url, emoji) |
| 218 |
|
| 219 |
|
| 220 |
class MisskeyAPI(): |
| 221 |
"""Interface for Misskey API |
| 222 |
|
| 223 |
https://github.com/misskey-dev/misskey |
| 224 |
https://misskey-hub.net/en/docs/api/ |
| 225 |
https://misskey-hub.net/docs/api/endpoints.html |
| 226 |
""" |
| 227 |
|
| 228 |
def __init__(self, extractor): |
| 229 |
self.root = extractor.root |
| 230 |
self.extractor = extractor |
| 231 |
self.access_token = extractor.config("access-token") |
| 232 |
|
| 233 |
def user_id_by_username(self, username): |
| 234 |
return self.users_show(username)["id"] |
| 235 |
|
| 236 |
def users_following(self, user_id): |
| 237 |
endpoint = "/users/following" |
| 238 |
data = {"userId": user_id} |
| 239 |
return self._pagination(endpoint, data) |
| 240 |
|
| 241 |
def users_notes(self, user_id): |
| 242 |
endpoint = "/users/notes" |
| 243 |
data = {"userId": user_id} |
| 244 |
return self._pagination(endpoint, data) |
| 245 |
|
| 246 |
def users_show(self, username): |
| 247 |
return self.extractor.cache(self._users_show_impl, username) |
| 248 |
|
| 249 |
def _users_show_impl(self, username): |
| 250 |
endpoint = "/users/show" |
| 251 |
username, _, host = username.partition("@") |
| 252 |
data = {"username": username, "host": host or None} |
| 253 |
return self._call(endpoint, data) |
| 254 |
|
| 255 |
def emojis(self): |
| 256 |
endpoint = "/emojis" |
| 257 |
return self._call(endpoint, None).get("emojis") |
| 258 |
|
| 259 |
def notes_show(self, note_id): |
| 260 |
endpoint = "/notes/show" |
| 261 |
data = {"noteId": note_id} |
| 262 |
return self._call(endpoint, data) |
| 263 |
|
| 264 |
def i_favorites(self): |
| 265 |
endpoint = "/i/favorites" |
| 266 |
if not self.access_token: |
| 267 |
raise self.extractor.exc.AuthenticationError() |
| 268 |
data = {"i": self.access_token} |
| 269 |
return self._pagination(endpoint, data) |
| 270 |
|
| 271 |
def _call(self, endpoint, data=None): |
| 272 |
return self.extractor.request_json( |
| 273 |
f"{self.root}/api{endpoint}", |
| 274 |
method=("GET" if data is None else "POST"), json=data) |
| 275 |
|
| 276 |
def _pagination(self, endpoint, data): |
| 277 |
extr = self.extractor |
| 278 |
data["limit"] = 100 |
| 279 |
data["withRenotes"] = extr.renotes |
| 280 |
data["withFiles"] = False if extr.config("text-posts") else True |
| 281 |
|
| 282 |
date_min, date_max = extr._get_date_min_max() |
| 283 |
if (order := extr.config("order-posts")) and \ |
| 284 |
order[0] in {"a", "r"}: |
| 285 |
key = "sinceId" |
| 286 |
data["sinceDate"] = 1 if date_min is None else date_min * 1000 |
| 287 |
date_stop = None if date_max is None else date_max |
| 288 |
else: |
| 289 |
key = "untilId" |
| 290 |
date_stop = None |
| 291 |
if date_min is not None: |
| 292 |
data["sinceDate"] = date_min * 1000 |
| 293 |
if date_max is None: |
| 294 |
# ensure notes are returned in descending order |
| 295 |
data["untilDate"] = (int(dt.time.time()) + 1000) * 1000 |
| 296 |
if date_max is not None: |
| 297 |
data["untilDate"] = date_max * 1000 |
| 298 |
|
| 299 |
while True: |
| 300 |
notes = self._call(endpoint, data) |
| 301 |
if not notes: |
| 302 |
return |
| 303 |
elif date_stop is not None and dt.to_ts(dt.parse_iso( |
| 304 |
notes[-1]["createdAt"])) > date_stop: |
| 305 |
for idx, note in enumerate(notes): |
| 306 |
if dt.to_ts(dt.parse_iso(note["createdAt"])) > date_stop: |
| 307 |
yield from notes[:idx] |
| 308 |
return |
| 309 |
else: |
| 310 |
yield from notes |
| 311 |
|
| 312 |
data[key] = notes[-1]["id"] |