-
Notifications
You must be signed in to change notification settings - Fork 202
/
image.py
215 lines (189 loc) · 7.89 KB
/
image.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
207
208
209
210
211
212
213
214
215
import logging
from collections import namedtuple
from common.licenses import LicenseInfo
from common.storage.media import MediaStore
from common.storage.tsv_columns import CURRENT_IMAGE_TSV_COLUMNS
logger = logging.getLogger(__name__)
Image = namedtuple("Image", [c.name for c in CURRENT_IMAGE_TSV_COLUMNS])
class ImageStore(MediaStore):
"""
A class that stores image information from a given provider.
Optional init arguments:
provider: String marking the provider in the `image` table of the DB.
tsv_suffix: Optional string to append to the tsv filename.
output_file: String giving a temporary .tsv filename (*not* the
full path) where the image info should be stored.
output_dir: String giving a path where `output_file` should be placed.
buffer_length: Integer giving the maximum number of image information rows
to store in memory before writing them to disk.
"""
def __init__(
self,
provider=None,
tsv_suffix=None,
output_file=None,
output_dir=None,
buffer_length=100,
media_type="image",
tsv_columns=None,
strip_url_trailing_slashes: bool = True,
):
super().__init__(
provider, tsv_suffix, buffer_length, media_type, strip_url_trailing_slashes
)
self.columns = CURRENT_IMAGE_TSV_COLUMNS if tsv_columns is None else tsv_columns
def add_item(
self,
foreign_landing_url: str,
url: str,
license_info: LicenseInfo,
foreign_identifier: str,
thumbnail_url: str | None = None,
filesize: int | None = None,
filetype: str | None = None,
width: int | None = None,
height: int | None = None,
creator: str | None = None,
creator_url: str | None = None,
title: str | None = None,
meta_data: dict | str | None = None,
raw_tags=None,
category: str | None = None,
watermarked: str | None = "f",
source: str | None = None,
ingestion_type: str | None = None,
**kwargs,
):
"""
Add information for a single image to the ImageStore. Image data
without the required parameters will be discarded.
Required Arguments:
foreign_landing_url: URL of page where the image lives on the
source website.
url: Direct link to the image file
license_info: LicenseInfo object that has
- the URL of the license for the image,
- string representation of the license,
- version of the license,
- raw license URL that was by provider,
if different from canonical URL
For valid options of license names, see
`common.license.constants.get_license_path_map()`.
To get the LicenseInfo object, use `get_license_info` with either
(license_ and license_version) or (license_url) named parameters.
In the case of the `publicdomain` license, which has no version,
one should pass `common.license.constants.NO_VERSION` here.
foreign_identifier: Unique identifier for the image on the
source site.
Optional Arguments:
thumbnail_url: Direct link to a thumbnail-sized version of
the image.
filesize: Size of the image file in bytes.
filetype: eg. 'jpg', 'svg'.
width: in pixels.
height: in pixels.
creator: The creator of the image.
creator_url: The user page, or home page of the creator.
title: Title of the image.
meta_data: Dictionary of meta_data about the image.
Currently, a key that we prefer to have is
`description`. If 'license_url' is included
in this dictionary, and `license_url` is
given as an argument, the argument will
replace the one given in the dictionary.
raw_tags: List of tags associated with the image.
category: The image category, defaults to the default
category for the provider from
common/loader/provider_details.py.
watermarked: A boolean, or 't' or 'f' string; whether
the image has a noticeable watermark.
source: If different from the provider. This might
be the case when we get information from
some aggregation of images. In this case,
the `source` argument gives the aggregator,
and the `provider` argument in the
ImageStore init function is the specific
provider of the image.
ingestion_type: Set programmatically
"""
image_data = {
"foreign_landing_url": foreign_landing_url,
"url": url,
"thumbnail_url": thumbnail_url,
"filesize": filesize,
"filetype": filetype,
"license_info": license_info,
"foreign_identifier": foreign_identifier,
"width": width,
"height": height,
"creator": creator,
"creator_url": creator_url,
"title": title,
"meta_data": meta_data,
"raw_tags": raw_tags,
"category": category,
"watermarked": watermarked,
"source": source,
"ingestion_type": ingestion_type,
}
image = self._get_image(**image_data)
if image is not None:
self.save_item(image)
return self.total_items
def _get_image(self, **kwargs) -> Image | None:
"""Validate image information and return Image namedtuple."""
image_metadata = self.clean_media_metadata(**kwargs)
if image_metadata is None:
return None
return Image(**image_metadata)
class MockImageStore(ImageStore):
"""
A class that mocks the role of the ImageStore class.
This class replaces all functionality of ImageStore that calls the internet.
It also allows for easy introspection into the images added to the
media_buffer by making it a public attribute, and not converting the
images to TSV.
For information about all arguments other than license_info refer to
ImageStore class.
Required init arguments:
license_info: A named tuple consisting of valid license info from
the test script in which MockImageStore is being used.
"""
NULLABLE_FIELDS = [
"thumbnail_url",
"filesize",
"filetype",
"foreign_identifier",
"width",
"height",
"creator",
"creator_url",
"title",
"meta_data",
"raw_tags",
"category",
"watermarked",
"source",
"ingestion_type",
]
def __init__(
self,
provider=None,
output_file=None,
output_dir=None,
buffer_length=100,
license_info=None,
):
logger.info(f"Initialized with provider {provider}")
super().__init__(provider=provider)
self.license_info = license_info
self.media_buffer = []
def add_item(self, **kwargs):
image_data = kwargs
for field in MockImageStore.NULLABLE_FIELDS:
if field not in image_data:
image_data[field] = None
image = self._get_image(**image_data)
if image is not None:
self.media_buffer.append(image)
return len(self.media_buffer)