2018-09-17 21:19:25 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
# Copyright 2018-2021 Mike Fährmann
|
2018-09-17 21:19:25 +02: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-02-23 16:48:30 +01:00
|
|
|
"""Extractors for https://wallhaven.cc/"""
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
from .common import Extractor, Message
|
2019-05-30 23:11:36 +02:00
|
|
|
from .. import text
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WallhavenExtractor(Extractor):
|
|
|
|
"""Base class for wallhaven extractors"""
|
|
|
|
category = "wallhaven"
|
2019-05-30 23:11:36 +02:00
|
|
|
filename_fmt = "{category}_{id}_{resolution}.{extension}"
|
2021-03-01 17:03:25 +01:00
|
|
|
archive_fmt = "{id}"
|
2019-05-30 23:11:36 +02:00
|
|
|
root = "https://wallhaven.cc"
|
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
def items(self):
|
|
|
|
metadata = self.metadata()
|
|
|
|
for wp in self.wallpapers():
|
|
|
|
self._transform(wp)
|
|
|
|
wp.update(metadata)
|
|
|
|
url = wp["url"]
|
|
|
|
yield Message.Directory, wp
|
|
|
|
yield Message.Url, url, text.nameext_from_url(url, wp)
|
|
|
|
|
|
|
|
def wallpapers(self):
|
|
|
|
"""Return relevant 'wallpaper' objects"""
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
"""Return general metadata"""
|
|
|
|
return ()
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _transform(wp):
|
|
|
|
wp["url"] = wp.pop("path")
|
|
|
|
if "tags" in wp:
|
|
|
|
wp["tags"] = [t["name"] for t in wp["tags"]]
|
|
|
|
wp["date"] = text.parse_datetime(
|
|
|
|
wp.pop("created_at"), "%Y-%m-%d %H:%M:%S")
|
|
|
|
wp["width"] = wp.pop("dimension_x")
|
|
|
|
wp["height"] = wp.pop("dimension_y")
|
|
|
|
wp["wh_category"] = wp["category"]
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WallhavenSearchExtractor(WallhavenExtractor):
|
|
|
|
"""Extractor for search results on wallhaven.cc"""
|
|
|
|
subcategory = "search"
|
2019-02-08 13:45:40 +01:00
|
|
|
directory_fmt = ("{category}", "{search[q]}")
|
2018-09-17 21:19:25 +02:00
|
|
|
archive_fmt = "s_{search[q]}_{id}"
|
2019-05-30 23:11:36 +02:00
|
|
|
pattern = r"(?:https?://)?wallhaven\.cc/search(?:/?\?([^/?#]+))?"
|
2019-02-08 13:45:40 +01:00
|
|
|
test = (
|
2019-05-30 23:11:36 +02:00
|
|
|
("https://wallhaven.cc/search?q=touhou"),
|
|
|
|
(("https://wallhaven.cc/search?q=id%3A87"
|
2019-02-02 15:37:54 +01:00
|
|
|
"&categories=111&purity=100&sorting=date_added&order=asc&page=3"), {
|
2019-11-27 19:39:56 +01:00
|
|
|
"pattern": r"https://w.wallhaven.cc/full/\w\w/wallhaven-\w+\.\w+",
|
2020-07-13 22:57:48 +02:00
|
|
|
"count": "<= 20",
|
2018-09-17 21:19:25 +02:00
|
|
|
}),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
WallhavenExtractor.__init__(self, match)
|
2018-09-17 21:19:25 +02:00
|
|
|
self.params = text.parse_query(match.group(1))
|
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
def wallpapers(self):
|
|
|
|
return WallhavenAPI(self).search(self.params.copy())
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"search": self.params}
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
|
2021-03-01 17:12:11 +01:00
|
|
|
class WallhavenCollectionExtractor(WallhavenExtractor):
|
|
|
|
"""Extractor for a collection on wallhaven.cc"""
|
|
|
|
subcategory = "collection"
|
|
|
|
directory_fmt = ("{category}", "{username}", "{collection_id}")
|
|
|
|
pattern = r"(?:https?://)?wallhaven\.cc/user/([^/?#]+)/favorites/(\d+)"
|
|
|
|
test = ("https://wallhaven.cc/user/AksumkA/favorites/74", {
|
|
|
|
"count": ">= 50",
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WallhavenExtractor.__init__(self, match)
|
|
|
|
self.username, self.collection_id = match.groups()
|
|
|
|
|
|
|
|
def wallpapers(self):
|
|
|
|
return WallhavenAPI(self).collection(self.username, self.collection_id)
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"username": self.username, "collection_id": self.collection_id}
|
|
|
|
|
|
|
|
|
2021-03-02 01:32:26 +01:00
|
|
|
class WallhavenCollectionsExtractor(WallhavenExtractor):
|
|
|
|
"""Extractor for all collections of a wallhaven user"""
|
|
|
|
subcategory = "collections"
|
|
|
|
pattern = r"(?:https?://)?wallhaven\.cc/user/([^/?#]+)/favorites/?$"
|
|
|
|
test = ("https://wallhaven.cc/user/AksumkA/favorites", {
|
|
|
|
"pattern": WallhavenCollectionExtractor.pattern,
|
|
|
|
"count": 4,
|
|
|
|
})
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WallhavenExtractor.__init__(self, match)
|
|
|
|
self.username = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
for collection in WallhavenAPI(self).collections(self.username):
|
|
|
|
collection["_extractor"] = WallhavenCollectionExtractor
|
|
|
|
url = "https://wallhaven.cc/user/{}/favorites/{}".format(
|
|
|
|
self.username, collection["id"])
|
|
|
|
yield Message.Queue, url, collection
|
|
|
|
|
|
|
|
|
2018-09-17 21:19:25 +02:00
|
|
|
class WallhavenImageExtractor(WallhavenExtractor):
|
|
|
|
"""Extractor for individual wallpaper on wallhaven.cc"""
|
|
|
|
subcategory = "image"
|
2019-05-30 23:11:36 +02:00
|
|
|
pattern = (r"(?:https?://)?(?:wallhaven\.cc/w/|whvn\.cc/"
|
|
|
|
r"|w\.wallhaven\.cc/[a-z]+/\w\w/wallhaven-)(\w+)")
|
2019-02-08 13:45:40 +01:00
|
|
|
test = (
|
2019-05-30 23:11:36 +02:00
|
|
|
("https://wallhaven.cc/w/01w334", {
|
|
|
|
"pattern": "https://[^.]+.wallhaven.cc/full/01/[^-]+-01w334.jpg",
|
2018-09-17 21:19:25 +02:00
|
|
|
"content": "497212679383a465da1e35bd75873240435085a2",
|
|
|
|
"keyword": {
|
2019-05-30 23:11:36 +02:00
|
|
|
"id" : "01w334",
|
|
|
|
"width" : 1920,
|
|
|
|
"height" : 1200,
|
|
|
|
"resolution" : "1920x1200",
|
2021-03-01 17:03:25 +01:00
|
|
|
"ratio" : "1.6",
|
2019-05-30 23:11:36 +02:00
|
|
|
"colors" : list,
|
|
|
|
"tags" : list,
|
|
|
|
"file_size" : 278799,
|
|
|
|
"file_type" : "image/jpeg",
|
|
|
|
"purity" : "sfw",
|
|
|
|
"short_url" : "https://whvn.cc/01w334",
|
|
|
|
"source" : str,
|
|
|
|
"uploader" : {
|
|
|
|
"group" : "Owner/Developer",
|
|
|
|
"username" : "AksumkA",
|
|
|
|
},
|
2020-02-23 16:48:30 +01:00
|
|
|
"date" : "dt:2014-08-31 06:17:19",
|
2019-05-30 23:11:36 +02:00
|
|
|
"wh_category": "anime",
|
|
|
|
"views" : int,
|
|
|
|
"favorites" : int,
|
2018-09-17 21:19:25 +02:00
|
|
|
},
|
|
|
|
}),
|
2018-09-19 15:15:14 +02:00
|
|
|
# NSFW
|
2019-05-30 23:11:36 +02:00
|
|
|
("https://wallhaven.cc/w/dge6v3", {
|
|
|
|
"url": "e4b802e70483f659d790ad5d0bd316245badf2ec",
|
2018-09-19 15:15:14 +02:00
|
|
|
}),
|
2019-05-30 23:11:36 +02:00
|
|
|
("https://whvn.cc/01w334"),
|
|
|
|
("https://w.wallhaven.cc/full/01/wallhaven-01w334.jpg"),
|
2019-02-08 13:45:40 +01:00
|
|
|
)
|
2018-09-17 21:19:25 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
2019-02-11 13:31:10 +01:00
|
|
|
WallhavenExtractor.__init__(self, match)
|
2018-09-17 21:19:25 +02:00
|
|
|
self.wallpaper_id = match.group(1)
|
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
def wallpapers(self):
|
|
|
|
return (WallhavenAPI(self).info(self.wallpaper_id),)
|
2019-05-30 23:11:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WallhavenAPI():
|
2021-03-01 17:03:25 +01:00
|
|
|
"""Interface for wallhaven's API
|
|
|
|
|
|
|
|
Ref: https://wallhaven.cc/help/api
|
|
|
|
"""
|
2019-05-30 23:11:36 +02:00
|
|
|
|
|
|
|
def __init__(self, extractor):
|
|
|
|
self.extractor = extractor
|
|
|
|
|
|
|
|
key = extractor.config("api-key")
|
|
|
|
if key is None:
|
|
|
|
key = "25HYZenXTICjzBZXzFSg98uJtcQVrDs2"
|
|
|
|
extractor.log.debug("Using default API Key")
|
|
|
|
else:
|
|
|
|
extractor.log.debug("Using custom API Key")
|
|
|
|
self.headers = {"X-API-Key": key}
|
|
|
|
|
|
|
|
def info(self, wallpaper_id):
|
2021-03-01 17:03:25 +01:00
|
|
|
endpoint = "/v1/w/" + wallpaper_id
|
|
|
|
return self._call(endpoint)["data"]
|
2019-05-30 23:11:36 +02:00
|
|
|
|
2021-03-01 17:12:11 +01:00
|
|
|
def collection(self, username, collection_id):
|
|
|
|
endpoint = "/v1/collections/{}/{}".format(username, collection_id)
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
|
|
|
def collections(self, username):
|
|
|
|
endpoint = "/v1/collections/" + username
|
|
|
|
return self._pagination(endpoint)
|
|
|
|
|
2019-05-30 23:11:36 +02:00
|
|
|
def search(self, params):
|
2021-03-01 17:03:25 +01:00
|
|
|
endpoint = "/v1/search"
|
|
|
|
return self._pagination(endpoint, params)
|
2019-05-30 23:11:36 +02:00
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
def _call(self, endpoint, params=None):
|
|
|
|
url = "https://wallhaven.cc/api" + endpoint
|
2019-05-30 23:11:36 +02:00
|
|
|
return self.extractor.request(
|
|
|
|
url, headers=self.headers, params=params).json()
|
|
|
|
|
2021-03-01 17:03:25 +01:00
|
|
|
def _pagination(self, endpoint, params=None):
|
|
|
|
if params is None:
|
|
|
|
params = {}
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = self._call(endpoint, params)
|
|
|
|
yield from data["data"]
|
|
|
|
|
2021-03-02 01:32:26 +01:00
|
|
|
meta = data.get("meta")
|
|
|
|
if not meta or meta["current_page"] >= meta["last_page"]:
|
2021-03-01 17:03:25 +01:00
|
|
|
return
|
|
|
|
params["page"] = meta["current_page"] + 1
|