forked from WordPress/openverse-catalog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstocksnap.py
206 lines (171 loc) · 5.78 KB
/
stocksnap.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
"""
Content Provider: StockSnap
ETL Process: Use the API to identify all CC-licensed images.
Output: TSV file containing the image, the respective
meta-data.
Notes: https://stocksnap.io/api/
No rate limit specified. No authentication required.
All images are licensed under CC0.
"""
import json
import logging
from common.licenses import get_license_info
from common.loader import provider_details as prov
from common.requester import DelayedRequester
from common.storage.image import ImageStore
logging.basicConfig(
format="%(asctime)s - %(name)s - %(levelname)s: %(message)s",
level=logging.INFO,
)
logger = logging.getLogger(__name__)
DELAY = 1 # in seconds
RETRIES = 3
HOST = "stocksnap.io"
ENDPOINT = f"https://{HOST}/api/load-photos/date/desc"
IMAGE_CDN = "https://cdn.stocksnap.io/img-thumbs/960w"
THUMBNAIL_CDN = "https://cdn.stocksnap.io/img-thumbs/280h"
PROVIDER = prov.STOCKSNAP_DEFAULT_PROVIDER
HEADERS = {
"Accept": "application/json",
}
delayed_requester = DelayedRequester(DELAY)
image_store = ImageStore(provider=PROVIDER)
license_url = "https://creativecommons.org/publicdomain/zero/1.0/"
license_info = get_license_info(license_url=license_url)
def main():
"""
This script pulls all the data from the StockSnap and writes it into a
.TSV file to be eventually read into our DB.
"""
logger.info("Begin: StockSnap script")
image_count = _get_items()
image_store.commit()
logger.info(f"Total images pulled: {image_count}")
logger.info("Terminated!")
def _get_items():
item_count = 0
page_number = 1
should_continue = True
while should_continue:
page_endpoint = f"{ENDPOINT}/{page_number}"
batch_data = _get_batch_json(endpoint=page_endpoint)
if isinstance(batch_data, list) and len(batch_data) > 0:
item_count = _process_item_batch(batch_data)
page_number += 1
else:
should_continue = False
return item_count
def _get_batch_json(
endpoint=ENDPOINT, headers=None, retries=RETRIES, query_params=None
):
if headers is None:
headers = HEADERS.copy()
response_json = delayed_requester.get_response_json(
endpoint, retries, query_params, headers=headers
)
if response_json is None:
return None
else:
data = response_json.get("results")
return data
def _process_item_batch(items_batch):
for item in items_batch:
item_meta_data = _extract_item_data(item)
if item_meta_data is None:
continue
image_store.add_item(**item_meta_data)
return image_store.total_items
def _extract_item_data(media_data):
"""
Extract data for individual image.
"""
try:
foreign_id = media_data["img_id"]
except (TypeError, KeyError, AttributeError):
return None
foreign_landing_url = f"https://{HOST}/photo/{foreign_id}"
image_url, thumbnail_url, width, height = _get_image_info(media_data)
if image_url is None:
logger.info("Found no image url.")
logger.info(f"{json.dumps(media_data, indent=2)}")
return None
title = _get_title(media_data)
if title is None:
logger.info("Found no image title.")
logger.info(f"{json.dumps(media_data, indent=2)}")
return None
creator, creator_url = _get_creator_data(media_data)
metadata = _get_metadata(media_data)
tags = _get_tags(media_data)
return {
"title": title,
"creator": creator,
"creator_url": creator_url,
"foreign_identifier": foreign_id,
"foreign_landing_url": foreign_landing_url,
"image_url": image_url,
"filesize": _get_filesize(image_url),
"filetype": "jpg",
"height": height,
"width": width,
"thumbnail_url": thumbnail_url,
"license_info": license_info,
"meta_data": metadata,
"raw_tags": tags,
"category": prov.DEFAULT_IMAGE_CATEGORY[PROVIDER],
}
def _get_image_info(item):
width = item.get("img_width")
height = item.get("img_height")
img_id = item.get("img_id")
image_url = f"{IMAGE_CDN}/{img_id}.jpg"
thumbnail_url = f"{THUMBNAIL_CDN}/{img_id}.jpg"
return image_url, thumbnail_url, width, height
def _get_creator_data(item):
"""
Get the author's name and website preferring their custom link over the
StockSnap profile. The latter is used if the first is not found.
"""
creator_name = item.get("author_name")
if creator_name is None:
return None, None
creator_url = item.get("author_website")
if creator_url is None or creator_url in [
"https://stocksnap.io/",
"https://stocksnap.io/author/undefined/",
]:
creator_url = item.get("author_profile")
return creator_name, creator_url
def _get_title(item):
"""
Get the first two photo's tags/keywords to make the title and transform it
to title case, as shown on its page.
"""
tags = item.get("keywords", [])[:2]
if len(tags) > 0:
tags.append("Photo")
img_title = " ".join(tags)
return img_title.title()
def _get_filesize(image_url):
"""
Get the size of the image in bytes.
"""
resp = delayed_requester.get(image_url)
if resp:
filesize = int(resp.headers.get("Content-Length", 0))
return filesize if filesize != 0 else None
def _get_metadata(item):
"""
Include popularity statistics.
"""
extras = ["downloads_raw", "page_views_raw", "favorites_raw"]
metadata = {}
for key in extras:
value = item.get(key)
if value is not None:
metadata[key] = value
return metadata
def _get_tags(item):
return item.get("keywords")
if __name__ == "__main__":
main()