2019-04-02 17:34:57 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2023-09-11 16:30:55 +02:00
|
|
|
# Copyright 2019-2023 Mike Fährmann
|
2019-04-02 17:34:57 +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.
|
|
|
|
|
|
|
|
"""Extractors for https://www.wikiart.org/"""
|
|
|
|
|
|
|
|
from .common import Extractor, Message
|
|
|
|
from .. import text
|
|
|
|
|
2019-04-05 12:15:28 +02:00
|
|
|
BASE_PATTERN = r"(?:https?://)?(?:www\.)?wikiart\.org/([a-z]+)"
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
|
|
|
|
class WikiartExtractor(Extractor):
|
|
|
|
"""Base class for wikiart extractors"""
|
|
|
|
category = "wikiart"
|
|
|
|
filename_fmt = "{id}_{title}.{extension}"
|
|
|
|
archive_fmt = "{id}"
|
|
|
|
root = "https://www.wikiart.org"
|
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
Extractor.__init__(self, match)
|
|
|
|
self.lang = match.group(1)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
data = self.metadata()
|
|
|
|
yield Message.Directory, data
|
|
|
|
for painting in self.paintings():
|
|
|
|
url = painting["image"]
|
|
|
|
painting.update(data)
|
|
|
|
yield Message.Url, url, text.nameext_from_url(url, painting)
|
|
|
|
|
|
|
|
def metadata(self):
|
2019-04-03 07:28:10 +02:00
|
|
|
"""Return a dict with general metadata"""
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def paintings(self):
|
2019-04-03 07:28:10 +02:00
|
|
|
"""Return an iterable containing all relevant 'painting' objects"""
|
2019-04-02 17:34:57 +02:00
|
|
|
|
2021-01-08 23:15:20 +01:00
|
|
|
def _pagination(self, url, extra_params=None, key="Paintings", stop=False):
|
2019-04-02 17:34:57 +02:00
|
|
|
headers = {
|
|
|
|
"X-Requested-With": "XMLHttpRequest",
|
|
|
|
"Referer": url,
|
|
|
|
}
|
|
|
|
params = {
|
|
|
|
"json": "2",
|
|
|
|
"layout": "new",
|
|
|
|
"page": 1,
|
|
|
|
"resultType": "masonry",
|
|
|
|
}
|
|
|
|
if extra_params:
|
|
|
|
params.update(extra_params)
|
|
|
|
|
|
|
|
while True:
|
|
|
|
data = self.request(url, headers=headers, params=params).json()
|
|
|
|
items = data.get(key)
|
|
|
|
if not items:
|
|
|
|
return
|
|
|
|
yield from items
|
2021-01-08 23:15:20 +01:00
|
|
|
if stop:
|
|
|
|
return
|
2019-04-02 17:34:57 +02:00
|
|
|
params["page"] += 1
|
|
|
|
|
|
|
|
|
|
|
|
class WikiartArtistExtractor(WikiartExtractor):
|
|
|
|
"""Extractor for an artist's paintings on wikiart.org"""
|
|
|
|
subcategory = "artist"
|
|
|
|
directory_fmt = ("{category}", "{artist[artistName]}")
|
2021-01-08 23:15:20 +01:00
|
|
|
pattern = BASE_PATTERN + r"/(?!\w+-by-)([\w-]+)/?$"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.wikiart.org/en/ARTIST"
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WikiartExtractor.__init__(self, match)
|
2021-01-08 23:15:20 +01:00
|
|
|
self.artist_name = match.group(2)
|
|
|
|
self.artist = None
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def metadata(self):
|
2021-01-08 23:15:20 +01:00
|
|
|
url = "{}/{}/{}?json=2".format(self.root, self.lang, self.artist_name)
|
|
|
|
self.artist = self.request(url).json()
|
|
|
|
return {"artist": self.artist}
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def paintings(self):
|
|
|
|
url = "{}/{}/{}/mode/all-paintings".format(
|
2021-01-08 23:15:20 +01:00
|
|
|
self.root, self.lang, self.artist_name)
|
2019-04-02 17:34:57 +02:00
|
|
|
return self._pagination(url)
|
|
|
|
|
|
|
|
|
2021-01-08 23:15:20 +01:00
|
|
|
class WikiartImageExtractor(WikiartArtistExtractor):
|
|
|
|
"""Extractor for individual paintings on wikiart.org"""
|
|
|
|
subcategory = "image"
|
|
|
|
pattern = BASE_PATTERN + r"/(?!(?:paintings|artists)-by-)([\w-]+)/([\w-]+)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.wikiart.org/en/ARTIST/TITLE"
|
2021-01-08 23:15:20 +01:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WikiartArtistExtractor.__init__(self, match)
|
|
|
|
self.title = match.group(3)
|
|
|
|
|
|
|
|
def paintings(self):
|
|
|
|
title, sep, year = self.title.rpartition("-")
|
|
|
|
if not sep or not year.isdecimal():
|
|
|
|
title = self.title
|
|
|
|
url = "{}/{}/Search/{} {}".format(
|
|
|
|
self.root, self.lang,
|
|
|
|
self.artist.get("artistName") or self.artist_name, title,
|
|
|
|
)
|
|
|
|
return self._pagination(url, stop=True)
|
|
|
|
|
|
|
|
|
2019-04-02 17:34:57 +02:00
|
|
|
class WikiartArtworksExtractor(WikiartExtractor):
|
|
|
|
"""Extractor for artwork collections on wikiart.org"""
|
|
|
|
subcategory = "artworks"
|
|
|
|
directory_fmt = ("{category}", "Artworks by {group!c}", "{type}")
|
|
|
|
pattern = BASE_PATTERN + r"/paintings-by-([\w-]+)/([\w-]+)"
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.wikiart.org/en/paintings-by-GROUP/TYPE"
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WikiartExtractor.__init__(self, match)
|
|
|
|
self.group = match.group(2)
|
|
|
|
self.type = match.group(3)
|
|
|
|
|
|
|
|
def metadata(self):
|
|
|
|
return {"group": self.group, "type": self.type}
|
|
|
|
|
|
|
|
def paintings(self):
|
|
|
|
url = "{}/{}/paintings-by-{}/{}".format(
|
|
|
|
self.root, self.lang, self.group, self.type)
|
|
|
|
return self._pagination(url)
|
|
|
|
|
|
|
|
|
|
|
|
class WikiartArtistsExtractor(WikiartExtractor):
|
|
|
|
"""Extractor for artist collections on wikiart.org"""
|
|
|
|
subcategory = "artists"
|
|
|
|
pattern = (BASE_PATTERN + r"/artists-by-([\w-]+)/([\w-]+)")
|
2023-09-11 16:30:55 +02:00
|
|
|
example = "https://www.wikiart.org/en/artists-by-GROUP/TYPE"
|
2019-04-02 17:34:57 +02:00
|
|
|
|
|
|
|
def __init__(self, match):
|
|
|
|
WikiartExtractor.__init__(self, match)
|
|
|
|
self.group = match.group(2)
|
|
|
|
self.type = match.group(3)
|
|
|
|
|
|
|
|
def items(self):
|
|
|
|
url = "{}/{}/App/Search/Artists-by-{}".format(
|
|
|
|
self.root, self.lang, self.group)
|
|
|
|
params = {"json": "3", "searchterm": self.type}
|
|
|
|
|
2019-04-03 07:28:10 +02:00
|
|
|
for artist in self._pagination(url, params, "Artists"):
|
2019-04-02 17:34:57 +02:00
|
|
|
artist["_extractor"] = WikiartArtistExtractor
|
|
|
|
yield Message.Queue, self.root + artist["artistUrl"], artist
|