| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://unsplash.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text, util |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?unsplash\.com" |
| 15 |
|
| 16 |
|
| 17 |
class UnsplashExtractor(Extractor): |
| 18 |
"""Base class for unsplash extractors""" |
| 19 |
category = "unsplash" |
| 20 |
directory_fmt = ("{category}", "{user[username]}") |
| 21 |
filename_fmt = "{id}.{extension}" |
| 22 |
archive_fmt = "{id}" |
| 23 |
root = "https://unsplash.com" |
| 24 |
page_start = 1 |
| 25 |
per_page = 20 |
| 26 |
|
| 27 |
def __init__(self, match): |
| 28 |
Extractor.__init__(self, match) |
| 29 |
self.item = match[1] |
| 30 |
|
| 31 |
def items(self): |
| 32 |
fmt = self.config("format") or "raw" |
| 33 |
metadata = self.metadata() |
| 34 |
|
| 35 |
for photo in self.photos(): |
| 36 |
util.delete_items( |
| 37 |
photo, ("current_user_collections", "related_collections")) |
| 38 |
url = photo["urls"][fmt] |
| 39 |
text.nameext_from_url(url, photo) |
| 40 |
|
| 41 |
if metadata: |
| 42 |
photo.update(metadata) |
| 43 |
photo["extension"] = "jpg" |
| 44 |
photo["date"] = self.parse_datetime_iso(photo["created_at"]) |
| 45 |
if "tags" in photo: |
| 46 |
photo["tags"] = [t["title"] for t in photo["tags"]] |
| 47 |
|
| 48 |
yield Message.Directory, "", photo |
| 49 |
yield Message.Url, url, photo |
| 50 |
|
| 51 |
def metadata(self): |
| 52 |
return None |
| 53 |
|
| 54 |
def skip_files(self, num): |
| 55 |
pages = num // self.per_page |
| 56 |
self.page_start += pages |
| 57 |
return pages * self.per_page |
| 58 |
|
| 59 |
def _pagination(self, url, params, results=False): |
| 60 |
params["per_page"] = self.per_page |
| 61 |
params["page"] = self.page_start |
| 62 |
|
| 63 |
while True: |
| 64 |
photos = self.request_json(url, params=params) |
| 65 |
if results: |
| 66 |
photos = photos["results"] |
| 67 |
yield from photos |
| 68 |
|
| 69 |
if len(photos) < self.per_page: |
| 70 |
return |
| 71 |
params["page"] += 1 |
| 72 |
|
| 73 |
|
| 74 |
class UnsplashImageExtractor(UnsplashExtractor): |
| 75 |
"""Extractor for a single unsplash photo""" |
| 76 |
subcategory = "image" |
| 77 |
pattern = BASE_PATTERN + r"/photos/([^/?#]+)" |
| 78 |
example = "https://unsplash.com/photos/ID" |
| 79 |
|
| 80 |
def photos(self): |
| 81 |
url = f"{self.root}/napi/photos/{self.item}" |
| 82 |
return (self.request_json(url),) |
| 83 |
|
| 84 |
|
| 85 |
class UnsplashUserExtractor(UnsplashExtractor): |
| 86 |
"""Extractor for all photos of an unsplash user""" |
| 87 |
subcategory = "user" |
| 88 |
pattern = BASE_PATTERN + r"/@(\w+)/?$" |
| 89 |
example = "https://unsplash.com/@USER" |
| 90 |
|
| 91 |
def photos(self): |
| 92 |
url = f"{self.root}/napi/users/{self.item}/photos" |
| 93 |
params = {"order_by": "latest"} |
| 94 |
return self._pagination(url, params) |
| 95 |
|
| 96 |
|
| 97 |
class UnsplashFavoriteExtractor(UnsplashExtractor): |
| 98 |
"""Extractor for all likes of an unsplash user""" |
| 99 |
subcategory = "favorite" |
| 100 |
pattern = BASE_PATTERN + r"/@(\w+)/likes" |
| 101 |
example = "https://unsplash.com/@USER/likes" |
| 102 |
|
| 103 |
def photos(self): |
| 104 |
url = f"{self.root}/napi/users/{self.item}/likes" |
| 105 |
params = {"order_by": "latest"} |
| 106 |
return self._pagination(url, params) |
| 107 |
|
| 108 |
|
| 109 |
class UnsplashCollectionExtractor(UnsplashExtractor): |
| 110 |
"""Extractor for an unsplash collection""" |
| 111 |
subcategory = "collection" |
| 112 |
pattern = BASE_PATTERN + r"/collections/([^/?#]+)(?:/([^/?#]+))?" |
| 113 |
example = "https://unsplash.com/collections/12345/TITLE" |
| 114 |
|
| 115 |
def __init__(self, match): |
| 116 |
UnsplashExtractor.__init__(self, match) |
| 117 |
self.title = match[2] or "" |
| 118 |
|
| 119 |
def metadata(self): |
| 120 |
return {"collection_id": self.item, "collection_title": self.title} |
| 121 |
|
| 122 |
def photos(self): |
| 123 |
url = f"{self.root}/napi/collections/{self.item}/photos" |
| 124 |
params = {"order_by": "latest"} |
| 125 |
return self._pagination(url, params) |
| 126 |
|
| 127 |
|
| 128 |
class UnsplashSearchExtractor(UnsplashExtractor): |
| 129 |
"""Extractor for unsplash search results""" |
| 130 |
subcategory = "search" |
| 131 |
pattern = BASE_PATTERN + r"/s/photos/([^/?#]+)(?:\?([^#]+))?" |
| 132 |
example = "https://unsplash.com/s/photos/QUERY" |
| 133 |
|
| 134 |
def __init__(self, match): |
| 135 |
UnsplashExtractor.__init__(self, match) |
| 136 |
self.query = match[2] |
| 137 |
|
| 138 |
def photos(self): |
| 139 |
url = self.root + "/napi/search/photos" |
| 140 |
params = {"query": text.unquote(self.item.replace('-', ' '))} |
| 141 |
if self.query: |
| 142 |
params.update(text.parse_query(self.query)) |
| 143 |
return self._pagination(url, params, True) |