| 1 |
|
| 2 |
|
| 3 |
n |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
"""Extractors for https://pexels.com/""" |
| 10 |
|
| 11 |
from .common import Extractor, Message |
| 12 |
from .. import text |
| 13 |
|
| 14 |
BASE_PATTERN = r"(?:https?://)?(?:www\.)?pexels\.com" |
| 15 |
|
| 16 |
|
| 17 |
class PexelsExtractor(Extractor): |
| 18 |
"""Base class for pexels extractors""" |
| 19 |
category = "pexels" |
| 20 |
root = "https://www.pexels.com" |
| 21 |
archive_fmt = "{id}" |
| 22 |
request_interval = (1.0, 2.0) |
| 23 |
request_interval_min = 0.5 |
| 24 |
|
| 25 |
def _init(self): |
| 26 |
self.api = PexelsAPI(self) |
| 27 |
|
| 28 |
def items(self): |
| 29 |
metadata = self.metadata() |
| 30 |
|
| 31 |
for post in self.posts(): |
| 32 |
if "attributes" in post: |
| 33 |
attr = post |
| 34 |
post = post["attributes"] |
| 35 |
post["type"] = attr["type"] |
| 36 |
|
| 37 |
post.update(metadata) |
| 38 |
post["date"] = self.parse_datetime_iso(post["created_at"][:-5]) |
| 39 |
|
| 40 |
if "image" in post: |
| 41 |
url, _, query = post["image"]["download_link"].partition("?") |
| 42 |
name = text.extr(query, "&dl=", "&") |
| 43 |
elif "video" in post: |
| 44 |
video = post["video"] |
| 45 |
name = video["src"] |
| 46 |
url = video["download_link"] |
| 47 |
else: |
| 48 |
self.log.warning("%s: Unsupported post type", post.get("id")) |
| 49 |
continue |
| 50 |
|
| 51 |
yield Message.Directory, "", post |
| 52 |
yield Message.Url, url, text.nameext_from_url(name, post) |
| 53 |
|
| 54 |
def posts(self): |
| 55 |
return () |
| 56 |
|
| 57 |
def metadata(self): |
| 58 |
return {} |
| 59 |
|
| 60 |
|
| 61 |
class PexelsCollectionExtractor(PexelsExtractor): |
| 62 |
"""Extractor for a pexels.com collection""" |
| 63 |
subcategory = "collection" |
| 64 |
directory_fmt = ("{category}", "Collections", "{collection}") |
| 65 |
pattern = BASE_PATTERN + r"/collections/((?:[^/?#]*-)?(\w+))" |
| 66 |
example = "https://www.pexels.com/collections/SLUG-a1b2c3/" |
| 67 |
|
| 68 |
def metadata(self): |
| 69 |
cname, cid = self.groups |
| 70 |
return {"collection": cname, "collection_id": cid} |
| 71 |
|
| 72 |
def posts(self): |
| 73 |
return self.api.collections_media(self.groups[1]) |
| 74 |
|
| 75 |
|
| 76 |
class PexelsSearchExtractor(PexelsExtractor): |
| 77 |
"""Extractor for pexels.com search results""" |
| 78 |
subcategory = "search" |
| 79 |
directory_fmt = ("{category}", "Searches", "{search_tags}") |
| 80 |
pattern = BASE_PATTERN + r"/search/([^/?#]+)" |
| 81 |
example = "https://www.pexels.com/search/QUERY/" |
| 82 |
|
| 83 |
def metadata(self): |
| 84 |
return {"search_tags": self.groups[0]} |
| 85 |
|
| 86 |
def posts(self): |
| 87 |
return self.api.search_photos(self.groups[0]) |
| 88 |
|
| 89 |
|
| 90 |
class PexelsUserExtractor(PexelsExtractor): |
| 91 |
"""Extractor for pexels.com user galleries""" |
| 92 |
subcategory = "user" |
| 93 |
directory_fmt = ("{category}", "@{user[slug]}") |
| 94 |
pattern = BASE_PATTERN + r"/(@(?:(?:[^/?#]*-)?(\d+)|[^/?#]+))" |
| 95 |
example = "https://www.pexels.com/@USER-12345/" |
| 96 |
|
| 97 |
def posts(self): |
| 98 |
return self.api.users_media_recent(self.groups[1] or self.groups[0]) |
| 99 |
|
| 100 |
|
| 101 |
class PexelsImageExtractor(PexelsExtractor): |
| 102 |
subcategory = "image" |
| 103 |
pattern = BASE_PATTERN + r"/photo/((?:[^/?#]*-)?\d+)" |
| 104 |
example = "https://www.pexels.com/photo/SLUG-12345/" |
| 105 |
|
| 106 |
def posts(self): |
| 107 |
url = f"{self.root}/photo/{self.groups[0]}/" |
| 108 |
page = self.request(url).text |
| 109 |
return (self._extract_nextdata(page)["props"]["pageProps"]["medium"],) |
| 110 |
|
| 111 |
|
| 112 |
class PexelsAPI(): |
| 113 |
"""Interface for the Pexels Web API""" |
| 114 |
|
| 115 |
def __init__(self, extractor): |
| 116 |
self.extractor = extractor |
| 117 |
self.root = "https://www.pexels.com/en-us/api" |
| 118 |
self.headers = { |
| 119 |
"Accept" : "*/*", |
| 120 |
"Content-Type" : "application/json", |
| 121 |
"secret-key" : "H2jk9uKnhRmL6WPwh89zBezWvr", |
| 122 |
"Authorization" : "", |
| 123 |
"X-Forwarded-CF-Connecting-IP" : "", |
| 124 |
"X-Forwarded-HTTP_CF_IPCOUNTRY": "", |
| 125 |
"X-Forwarded-CF-IPRegionCode" : "", |
| 126 |
"X-Client-Type" : "react", |
| 127 |
"Sec-Fetch-Dest": "empty", |
| 128 |
"Sec-Fetch-Mode": "cors", |
| 129 |
"Sec-Fetch-Site": "same-origin", |
| 130 |
"Priority" : "u=4", |
| 131 |
} |
| 132 |
|
| 133 |
def collections_media(self, collection_id): |
| 134 |
endpoint = f"/v3/collections/{collection_id}/media" |
| 135 |
params = { |
| 136 |
"page" : "1", |
| 137 |
"per_page": "24", |
| 138 |
} |
| 139 |
return self._pagination(endpoint, params) |
| 140 |
|
| 141 |
def search_photos(self, query): |
| 142 |
endpoint = "/v3/search/photos" |
| 143 |
params = { |
| 144 |
"query" : query, |
| 145 |
"page" : "1", |
| 146 |
"per_page" : "24", |
| 147 |
"orientation": "all", |
| 148 |
"size" : "all", |
| 149 |
"color" : "all", |
| 150 |
"sort" : "popular", |
| 151 |
} |
| 152 |
return self._pagination(endpoint, params) |
| 153 |
|
| 154 |
def users_media_recent(self, user_id): |
| 155 |
endpoint = f"/v3/users/{user_id}/media/recent" |
| 156 |
params = { |
| 157 |
"page" : "1", |
| 158 |
"per_page": "24", |
| 159 |
} |
| 160 |
return self._pagination(endpoint, params) |
| 161 |
|
| 162 |
def _call(self, endpoint, params): |
| 163 |
url = self.root + endpoint |
| 164 |
|
| 165 |
while True: |
| 166 |
response = self.extractor.request( |
| 167 |
url, params=params, headers=self.headers, fatal=None) |
| 168 |
|
| 169 |
if response.status_code < 300: |
| 170 |
return response.json() |
| 171 |
|
| 172 |
elif response.status_code == 429: |
| 173 |
self.extractor.wait(seconds=600) |
| 174 |
|
| 175 |
else: |
| 176 |
self.extractor.log.debug(response.text) |
| 177 |
raise self.extractor.exc.AbortExtraction("API request failed") |
| 178 |
|
| 179 |
def _pagination(self, endpoint, params): |
| 180 |
while True: |
| 181 |
data = self._call(endpoint, params) |
| 182 |
|
| 183 |
yield from data["data"] |
| 184 |
|
| 185 |
pagination = data["pagination"] |
| 186 |
if pagination["current_page"] >= pagination["total_pages"]: |
| 187 |
return |
| 188 |
params["page"] = pagination["current_page"] + 1 |