| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://www.wikiart.org/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?wikiart\.org/([a-z]+)" |
| 15 |
|
| 16 |
|
| 17 |
class WikiartExtractor(Extractor): |
| 18 |
"""Base class for wikiart extractors""" |
| 19 |
category = "wikiart" |
| 20 |
filename_fmt = "{id}_{title}.{extension}" |
| 21 |
archive_fmt = "{id}" |
| 22 |
root = "https://www.wikiart.org" |
| 23 |
|
| 24 |
def __init__(self, match): |
| 25 |
Extractor.__init__(self, match) |
| 26 |
self.lang = match[1] |
| 27 |
|
| 28 |
def items(self): |
| 29 |
data = self.metadata() |
| 30 |
yield Message.Directory, "", data |
| 31 |
for painting in self.paintings(): |
| 32 |
url = painting["image"] |
| 33 |
painting.update(data) |
| 34 |
yield Message.Url, url, text.nameext_from_url(url, painting) |
| 35 |
|
| 36 |
def metadata(self): |
| 37 |
"""Return a dict with general metadata""" |
| 38 |
|
| 39 |
def paintings(self): |
| 40 |
"""Return an iterable containing all relevant 'painting' objects""" |
| 41 |
|
| 42 |
def _pagination(self, url, extra_params=None, key="Paintings", stop=False): |
| 43 |
headers = { |
| 44 |
"X-Requested-With": "XMLHttpRequest", |
| 45 |
"Referer": url, |
| 46 |
} |
| 47 |
params = { |
| 48 |
"json": "2", |
| 49 |
"layout": "new", |
| 50 |
"page": 1, |
| 51 |
"resultType": "masonry", |
| 52 |
} |
| 53 |
if extra_params: |
| 54 |
params.update(extra_params) |
| 55 |
|
| 56 |
while True: |
| 57 |
data = self.request_json(url, headers=headers, params=params) |
| 58 |
items = data.get(key) |
| 59 |
if not items: |
| 60 |
return |
| 61 |
yield from items |
| 62 |
if stop: |
| 63 |
return |
| 64 |
params["page"] += 1 |
| 65 |
|
| 66 |
|
| 67 |
class WikiartArtistExtractor(WikiartExtractor): |
| 68 |
"""Extractor for an artist's paintings on wikiart.org""" |
| 69 |
subcategory = "artist" |
| 70 |
directory_fmt = ("{category}", "{artist[artistName]}") |
| 71 |
pattern = BASE_PATTERN + r"/(?!\w+-by-)([\w-]+)/?$" |
| 72 |
example = "https://www.wikiart.org/en/ARTIST" |
| 73 |
|
| 74 |
def __init__(self, match): |
| 75 |
WikiartExtractor.__init__(self, match) |
| 76 |
self.artist_name = match[2] |
| 77 |
self.artist = None |
| 78 |
|
| 79 |
def metadata(self): |
| 80 |
url = f"{self.root}/{self.lang}/{self.artist_name}?json=2" |
| 81 |
self.artist = self.request_json(url) |
| 82 |
return {"artist": self.artist} |
| 83 |
|
| 84 |
def paintings(self): |
| 85 |
url = f"{self.root}/{self.lang}/{self.artist_name}/mode/all-paintings" |
| 86 |
return self._pagination(url) |
| 87 |
|
| 88 |
|
| 89 |
class WikiartImageExtractor(WikiartArtistExtractor): |
| 90 |
"""Extractor for individual paintings on wikiart.org""" |
| 91 |
subcategory = "image" |
| 92 |
pattern = BASE_PATTERN + r"/(?!(?:paintings|artists)-by-)([\w-]+)/([\w-]+)" |
| 93 |
example = "https://www.wikiart.org/en/ARTIST/TITLE" |
| 94 |
|
| 95 |
def __init__(self, match): |
| 96 |
WikiartArtistExtractor.__init__(self, match) |
| 97 |
self.title = match[3] |
| 98 |
|
| 99 |
def paintings(self): |
| 100 |
title, sep, year = self.title.rpartition("-") |
| 101 |
if not sep or not year.isdecimal(): |
| 102 |
title = self.title |
| 103 |
url = (f"{self.root}/{self.lang}/Search/" |
| 104 |
f"{self.artist.get('artistName') or self.artist_name} {title}") |
| 105 |
return self._pagination(url, stop=True) |
| 106 |
|
| 107 |
|
| 108 |
class WikiartArtworksExtractor(WikiartExtractor): |
| 109 |
"""Extractor for artwork collections on wikiart.org""" |
| 110 |
subcategory = "artworks" |
| 111 |
directory_fmt = ("{category}", "Artworks by {group!c}", "{type}") |
| 112 |
pattern = BASE_PATTERN + r"/paintings-by-([\w-]+)/([\w-]+)" |
| 113 |
example = "https://www.wikiart.org/en/paintings-by-GROUP/TYPE" |
| 114 |
|
| 115 |
def __init__(self, match): |
| 116 |
WikiartExtractor.__init__(self, match) |
| 117 |
self.group = match[2] |
| 118 |
self.type = match[3] |
| 119 |
|
| 120 |
def metadata(self): |
| 121 |
return {"group": self.group, "type": self.type} |
| 122 |
|
| 123 |
def paintings(self): |
| 124 |
url = f"{self.root}/{self.lang}/paintings-by-{self.group}/{self.type}" |
| 125 |
return self._pagination(url) |
| 126 |
|
| 127 |
|
| 128 |
class WikiartArtistsExtractor(WikiartExtractor): |
| 129 |
"""Extractor for artist collections on wikiart.org""" |
| 130 |
subcategory = "artists" |
| 131 |
pattern = (BASE_PATTERN + r"/artists-by-([\w-]+)/([\w-]+)") |
| 132 |
example = "https://www.wikiart.org/en/artists-by-GROUP/TYPE" |
| 133 |
|
| 134 |
def __init__(self, match): |
| 135 |
WikiartExtractor.__init__(self, match) |
| 136 |
self.group = match[2] |
| 137 |
self.type = match[3] |
| 138 |
|
| 139 |
def items(self): |
| 140 |
url = f"{self.root}/{self.lang}/App/Search/Artists-by-{self.group}" |
| 141 |
params = {"json": "3", "searchterm": self.type} |
| 142 |
|
| 143 |
for artist in self._pagination(url, params, "Artists"): |
| 144 |
artist["_extractor"] = WikiartArtistExtractor |
| 145 |
yield Message.Queue, self.root + artist["artistUrl"], artist |