[misskey] add 'avatar' and 'background' extractors (#5347)
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
|
||||
from .common import BaseExtractor, Message
|
||||
from .. import text, exception
|
||||
from ..cache import memcache
|
||||
|
||||
|
||||
class MisskeyExtractor(BaseExtractor):
|
||||
@@ -64,6 +65,23 @@ class MisskeyExtractor(BaseExtractor):
|
||||
"""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": {
|
||||
@@ -95,6 +113,30 @@ class MisskeyUserExtractor(MisskeyExtractor):
|
||||
return self.api.users_notes(self.api.user_id_by_username(self.item))
|
||||
|
||||
|
||||
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.item)
|
||||
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.item)
|
||||
url = user.get("bannerUrl")
|
||||
return (self._make_note("background", user, url),) if url else ()
|
||||
|
||||
|
||||
class MisskeyFollowingExtractor(MisskeyExtractor):
|
||||
"""Extractor for followed Misskey users"""
|
||||
subcategory = "following"
|
||||
@@ -144,15 +186,10 @@ class MisskeyAPI():
|
||||
def __init__(self, extractor):
|
||||
self.root = extractor.root
|
||||
self.extractor = extractor
|
||||
self.headers = {"Content-Type": "application/json"}
|
||||
self.access_token = extractor.config("access-token")
|
||||
|
||||
def user_id_by_username(self, username):
|
||||
endpoint = "/users/show"
|
||||
data = {"username": username}
|
||||
if "@" in username:
|
||||
data["username"], _, data["host"] = username.partition("@")
|
||||
return self._call(endpoint, data)["id"]
|
||||
return self.users_show(username)["id"]
|
||||
|
||||
def users_following(self, user_id):
|
||||
endpoint = "/users/following"
|
||||
@@ -164,6 +201,13 @@ class MisskeyAPI():
|
||||
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}
|
||||
@@ -178,8 +222,7 @@ class MisskeyAPI():
|
||||
|
||||
def _call(self, endpoint, data):
|
||||
url = self.root + "/api" + endpoint
|
||||
return self.extractor.request(
|
||||
url, method="POST", headers=self.headers, json=data).json()
|
||||
return self.extractor.request_json(url, method="POST", json=data)
|
||||
|
||||
def _pagination(self, endpoint, data):
|
||||
data["limit"] = 100
|
||||
|
||||
Reference in New Issue
Block a user