2015-11-09 02:29:33 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2020-11-17 00:34:07 +01:00
|
|
|
# Copyright 2014-2020 Mike Fährmann
|
2015-11-09 02:29:33 +01:00
|
|
|
#
|
|
|
|
# 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.
|
|
|
|
|
2020-12-22 01:57:53 +01:00
|
|
|
"""Extractors for https://sankaku.app/"""
|
2015-11-09 02:29:33 +01:00
|
|
|
|
2020-12-08 21:05:47 +01:00
|
|
|
from .booru import BooruExtractor
|
2020-12-05 22:08:58 +01:00
|
|
|
from .. import text, exception
|
2020-12-17 16:12:59 +01:00
|
|
|
from ..cache import cache
|
2018-07-13 16:20:14 +02:00
|
|
|
import collections
|
2020-12-05 22:08:58 +01:00
|
|
|
|
2020-12-22 01:57:53 +01:00
|
|
|
BASE_PATTERN = r"(?:https?://)?" \
|
|
|
|
r"(?:sankaku\.app|(?:beta|chan)\.sankakucomplex\.com)"
|
2015-11-09 02:29:33 +01:00
|
|
|
|
2017-02-01 00:53:19 +01:00
|
|
|
|
2020-12-08 21:05:47 +01:00
|
|
|
class SankakuExtractor(BooruExtractor):
|
2020-12-05 22:08:58 +01:00
|
|
|
"""Base class for sankaku channel extractors"""
|
2017-12-11 21:44:27 +01:00
|
|
|
basecategory = "booru"
|
2015-11-21 04:26:30 +01:00
|
|
|
category = "sankaku"
|
|
|
|
filename_fmt = "{category}_{id}_{md5}.{extension}"
|
2020-12-17 16:12:59 +01:00
|
|
|
cookiedomain = None
|
|
|
|
_warning = True
|
2020-12-05 22:08:58 +01:00
|
|
|
|
|
|
|
TAG_TYPES = {
|
|
|
|
0: "general",
|
|
|
|
1: "artist",
|
|
|
|
2: "studio",
|
|
|
|
3: "copyright",
|
|
|
|
4: "character",
|
|
|
|
5: "genre",
|
|
|
|
6: "",
|
|
|
|
7: "",
|
|
|
|
8: "medium",
|
|
|
|
9: "meta",
|
|
|
|
}
|
2014-10-12 21:56:44 +02:00
|
|
|
|
2020-12-22 02:27:19 +01:00
|
|
|
def skip(self, num):
|
|
|
|
return 0
|
|
|
|
|
2020-12-24 01:04:44 +01:00
|
|
|
def _file_url(self, post):
|
2020-12-05 22:08:58 +01:00
|
|
|
url = post["file_url"]
|
2020-12-17 16:12:59 +01:00
|
|
|
if not url and self._warning:
|
|
|
|
self.log.warning(
|
|
|
|
"Login required to download 'contentious_content' posts")
|
|
|
|
SankakuExtractor._warning = False
|
2020-12-24 01:04:44 +01:00
|
|
|
return url
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _prepare(post):
|
2020-12-21 02:04:02 +01:00
|
|
|
post["created_at"] = post["created_at"]["s"]
|
|
|
|
post["date"] = text.parse_timestamp(post["created_at"])
|
2020-12-05 22:08:58 +01:00
|
|
|
post["tags"] = [tag["name"] for tag in post["tags"]]
|
|
|
|
|
2020-12-24 01:04:44 +01:00
|
|
|
def _extended_tags(self, post):
|
2020-12-05 22:08:58 +01:00
|
|
|
tags = collections.defaultdict(list)
|
|
|
|
types = self.TAG_TYPES
|
|
|
|
for tag in post["tags"]:
|
|
|
|
tags[types[tag["type"]]].append(tag["name"])
|
|
|
|
for key, value in tags.items():
|
|
|
|
post["tags_" + key] = value
|
|
|
|
|
2017-12-11 21:44:27 +01:00
|
|
|
|
|
|
|
class SankakuTagExtractor(SankakuExtractor):
|
2020-12-22 01:57:53 +01:00
|
|
|
"""Extractor for images from sankaku.app by search-tags"""
|
2017-12-11 21:44:27 +01:00
|
|
|
subcategory = "tag"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{search_tags}")
|
2018-03-01 17:40:31 +01:00
|
|
|
archive_fmt = "t_{search_tags}_{id}"
|
2020-12-05 22:08:58 +01:00
|
|
|
pattern = BASE_PATTERN + r"/\?([^#]*)"
|
2019-02-08 13:45:40 +01:00
|
|
|
test = (
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://sankaku.app/?tags=bonocho", {
|
2017-12-11 21:44:27 +01:00
|
|
|
"count": 5,
|
2020-10-11 18:12:40 +02:00
|
|
|
"pattern": r"https://c?s\.sankakucomplex\.com/data/[^/]{2}/[^/]{2}"
|
2019-02-08 13:45:40 +01:00
|
|
|
r"/[^/]{32}\.\w+\?e=\d+&m=[^&#]+",
|
2017-12-11 21:44:27 +01:00
|
|
|
}),
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://beta.sankakucomplex.com/?tags=bonocho"),
|
|
|
|
("https://chan.sankakucomplex.com/?tags=bonocho"),
|
2018-02-27 16:36:19 +01:00
|
|
|
# error on five or more tags
|
2017-12-11 21:44:27 +01:00
|
|
|
("https://chan.sankakucomplex.com/?tags=bonocho+a+b+c+d", {
|
|
|
|
"options": (("username", None),),
|
|
|
|
"exception": exception.StopExtraction,
|
2018-02-26 11:13:49 +01:00
|
|
|
}),
|
2018-02-27 16:36:19 +01:00
|
|
|
# match arbitrary query parameters
|
2019-02-08 13:45:40 +01:00
|
|
|
("https://chan.sankakucomplex.com"
|
|
|
|
"/?tags=marie_rose&page=98&next=3874906&commit=Search"),
|
|
|
|
)
|
2017-12-11 21:44:27 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
SankakuExtractor.__init__(self, match)
|
2018-02-26 11:13:49 +01:00
|
|
|
query = text.parse_query(match.group(1))
|
2018-02-27 16:36:19 +01:00
|
|
|
self.tags = text.unquote(query.get("tags", "").replace("+", " "))
|
2017-12-11 21:44:27 +01:00
|
|
|
|
2020-12-05 22:08:58 +01:00
|
|
|
def metadata(self):
|
|
|
|
return {"search_tags": self.tags}
|
2017-12-11 21:44:27 +01:00
|
|
|
|
2020-12-05 22:08:58 +01:00
|
|
|
def posts(self):
|
2020-12-22 02:27:19 +01:00
|
|
|
params = {"tags": self.tags}
|
|
|
|
return SankakuAPI(self).posts_keyset(params)
|
2017-12-12 18:20:15 +01:00
|
|
|
|
|
|
|
|
2017-12-12 19:18:43 +01:00
|
|
|
class SankakuPoolExtractor(SankakuExtractor):
|
2020-12-22 01:57:53 +01:00
|
|
|
"""Extractor for image pools or books from sankaku.app"""
|
2017-12-12 19:18:43 +01:00
|
|
|
subcategory = "pool"
|
2020-12-05 22:08:58 +01:00
|
|
|
directory_fmt = ("{category}", "pool", "{pool[id]} {pool[name_en]}")
|
2018-03-01 17:40:31 +01:00
|
|
|
archive_fmt = "p_{pool}_{id}"
|
2020-12-05 22:08:58 +01:00
|
|
|
pattern = BASE_PATTERN + r"/(?:books|pool/show)/(\d+)"
|
|
|
|
test = (
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://sankaku.app/books/90", {
|
2020-12-05 22:08:58 +01:00
|
|
|
"count": 5,
|
|
|
|
}),
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://beta.sankakucomplex.com/books/90"),
|
2020-12-05 22:08:58 +01:00
|
|
|
("https://chan.sankakucomplex.com/pool/show/90"),
|
|
|
|
)
|
2017-12-12 19:18:43 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
SankakuExtractor.__init__(self, match)
|
2017-12-12 19:18:43 +01:00
|
|
|
self.pool_id = match.group(1)
|
|
|
|
|
2020-12-05 22:08:58 +01:00
|
|
|
def metadata(self):
|
2020-12-17 16:12:59 +01:00
|
|
|
pool = SankakuAPI(self).pools(self.pool_id)
|
2020-12-05 22:08:58 +01:00
|
|
|
self._posts = pool.pop("posts")
|
|
|
|
return {"pool": pool}
|
2017-12-12 19:18:43 +01:00
|
|
|
|
2020-12-05 22:08:58 +01:00
|
|
|
def posts(self):
|
|
|
|
return self._posts
|
2017-12-12 19:18:43 +01:00
|
|
|
|
|
|
|
|
2017-12-12 18:20:15 +01:00
|
|
|
class SankakuPostExtractor(SankakuExtractor):
|
2020-12-22 01:57:53 +01:00
|
|
|
"""Extractor for single posts from sankaku.app"""
|
2017-12-12 18:20:15 +01:00
|
|
|
subcategory = "post"
|
2018-03-01 17:40:31 +01:00
|
|
|
archive_fmt = "{id}"
|
2020-12-05 22:08:58 +01:00
|
|
|
pattern = BASE_PATTERN + r"/post/show/(\d+)"
|
|
|
|
test = (
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://sankaku.app/post/show/360451", {
|
2020-12-05 22:08:58 +01:00
|
|
|
"content": "5e255713cbf0a8e0801dc423563c34d896bb9229",
|
|
|
|
"options": (("tags", True),),
|
|
|
|
"keyword": {
|
|
|
|
"tags_artist": ["bonocho"],
|
|
|
|
"tags_studio": ["dc_comics"],
|
|
|
|
"tags_medium": ["sketch", "copyright_name"],
|
|
|
|
"tags_copyright": list,
|
|
|
|
"tags_character": list,
|
|
|
|
"tags_general" : list,
|
|
|
|
},
|
|
|
|
}),
|
2020-12-17 16:12:59 +01:00
|
|
|
# 'contentious_content'
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://sankaku.app/post/show/21418978", {
|
2020-12-17 16:12:59 +01:00
|
|
|
"pattern": r"https://s\.sankakucomplex\.com"
|
|
|
|
r"/data/13/3c/133cda3bfde249c504284493903fb985\.jpg",
|
|
|
|
}),
|
2020-12-22 01:57:53 +01:00
|
|
|
("https://beta.sankakucomplex.com/post/show/360451"),
|
2020-12-05 22:08:58 +01:00
|
|
|
("https://chan.sankakucomplex.com/post/show/360451"),
|
|
|
|
)
|
2017-12-12 18:20:15 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
SankakuExtractor.__init__(self, match)
|
2017-12-12 18:20:15 +01:00
|
|
|
self.post_id = match.group(1)
|
|
|
|
|
2020-12-05 22:08:58 +01:00
|
|
|
def posts(self):
|
2020-12-22 02:27:19 +01:00
|
|
|
return SankakuAPI(self).posts(self.post_id)
|
2020-12-17 16:12:59 +01:00
|
|
|
|
|
|
|
|
|
|
|
class SankakuAPI():
|
2020-12-22 01:57:53 +01:00
|
|
|
"""Interface for the sankaku.app API"""
|
2020-12-17 16:12:59 +01:00
|
|
|
|
|
|
|
def __init__(self, extractor):
|
|
|
|
self.extractor = extractor
|
|
|
|
self.headers = {"Accept": "application/vnd.sankaku.api+json;v=2"}
|
|
|
|
|
|
|
|
self.username, self.password = self.extractor._get_auth_info()
|
|
|
|
if not self.username:
|
|
|
|
self.authenticate = lambda: None
|
|
|
|
|
|
|
|
def pools(self, pool_id):
|
2020-12-22 02:27:19 +01:00
|
|
|
params = {"lang": "en"}
|
|
|
|
return self._call("/pools/" + pool_id, params)
|
|
|
|
|
|
|
|
def posts(self, post_id):
|
|
|
|
params = {
|
|
|
|
"lang" : "en",
|
|
|
|
"page" : "1",
|
|
|
|
"limit": "1",
|
|
|
|
"tags" : "id_range:" + post_id,
|
|
|
|
}
|
|
|
|
return self._call("/posts", params, False)
|
2020-12-17 16:12:59 +01:00
|
|
|
|
|
|
|
def posts_keyset(self, params):
|
|
|
|
return self._pagination("/posts/keyset", params)
|
|
|
|
|
|
|
|
def authenticate(self):
|
|
|
|
self.headers["Authorization"] = \
|
|
|
|
_authenticate_impl(self.extractor, self.username, self.password)
|
|
|
|
|
2020-12-22 02:27:19 +01:00
|
|
|
def _call(self, endpoint, params=None, check=True):
|
2020-12-17 16:12:59 +01:00
|
|
|
url = "https://capi-v2.sankakucomplex.com" + endpoint
|
|
|
|
for _ in range(5):
|
|
|
|
self.authenticate()
|
|
|
|
response = self.extractor.request(
|
|
|
|
url, params=params, headers=self.headers, fatal=False)
|
|
|
|
|
|
|
|
if response.status_code == 429:
|
|
|
|
self.extractor.wait(
|
|
|
|
until=response.headers.get("X-RateLimit-Reset"))
|
|
|
|
continue
|
|
|
|
|
|
|
|
data = response.json()
|
2020-12-22 02:27:19 +01:00
|
|
|
if check and not data.get("success", True):
|
2020-12-17 16:12:59 +01:00
|
|
|
code = data.get("code")
|
|
|
|
if code == "invalid_token":
|
|
|
|
_authenticate_impl.invalidate(self.username)
|
|
|
|
continue
|
|
|
|
raise exception.StopExtraction(code)
|
|
|
|
return data
|
|
|
|
|
|
|
|
def _pagination(self, endpoint, params):
|
|
|
|
params["lang"] = "en"
|
|
|
|
params["limit"] = str(self.extractor.per_page)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = self._call(endpoint, params)
|
|
|
|
yield from data["data"]
|
|
|
|
|
|
|
|
params["next"] = data["meta"]["next"]
|
|
|
|
if not params["next"]:
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
@cache(maxage=365*24*3600, keyarg=2)
|
|
|
|
def _authenticate_impl(extr, username, password):
|
|
|
|
extr.log.info("Logging in as %s", username)
|
|
|
|
headers = {"Accept": "application/vnd.sankaku.api+json;v=2"}
|
|
|
|
|
|
|
|
# get initial access_token
|
|
|
|
url = "https://login.sankakucomplex.com/auth/token"
|
|
|
|
data = {"login": username, "password": password}
|
|
|
|
response = extr.request(
|
|
|
|
url, method="POST", headers=headers, json=data, fatal=False)
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
if response.status_code >= 400 or not data.get("success"):
|
|
|
|
raise exception.AuthenticationError(data.get("error"))
|
|
|
|
access_token = data["access_token"]
|
|
|
|
|
|
|
|
# start openid auth
|
|
|
|
url = "https://login.sankakucomplex.com/oidc/auth"
|
|
|
|
params = {
|
|
|
|
"response_type": "code",
|
|
|
|
"scope" : "openid",
|
|
|
|
"client_id" : "sankaku-web-app",
|
2020-12-22 01:57:53 +01:00
|
|
|
"redirect_uri" : "https://sankaku.app/sso/callback",
|
|
|
|
"state" : "return_uri=https://sankaku.app/",
|
2020-12-17 16:12:59 +01:00
|
|
|
"theme" : "black",
|
|
|
|
"lang" : "undefined",
|
|
|
|
}
|
|
|
|
page = extr.request(url, params=params).text
|
|
|
|
submit_url = text.extract(page, 'submitUrl = "', '"')[0]
|
|
|
|
|
|
|
|
# get code from initial access_token
|
|
|
|
url = "https://login.sankakucomplex.com" + submit_url
|
|
|
|
data = {
|
|
|
|
"accessToken": access_token,
|
|
|
|
"nonce" : "undefined",
|
|
|
|
}
|
|
|
|
response = extr.request(url, method="POST", data=data)
|
|
|
|
query = text.parse_query(response.request.url.partition("?")[2])
|
|
|
|
|
|
|
|
# get final access_token from code
|
|
|
|
url = "https://capi-v2.sankakucomplex.com/sso/finalize?lang=en"
|
|
|
|
data = {
|
|
|
|
"code" : query["code"],
|
|
|
|
"client_id" : "sankaku-web-app",
|
2020-12-22 01:57:53 +01:00
|
|
|
"redirect_uri": "https://sankaku.app/sso/callback",
|
2020-12-17 16:12:59 +01:00
|
|
|
}
|
|
|
|
response = extr.request(
|
|
|
|
url, method="POST", headers=headers, json=data, fatal=False)
|
|
|
|
data = response.json()
|
|
|
|
|
|
|
|
if response.status_code >= 400 or not data.get("success"):
|
|
|
|
raise exception.AuthenticationError(data.get("error"))
|
|
|
|
return "Bearer " + data["access_token"]
|