Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Simplify WP Photo Directory script and get missing authors (#515)
Krystle Salazar40a0d8a26db1
diff --git a/openverse_catalog/dags/providers/provider_api_scripts/wordpress.py b/openverse_catalog/dags/providers/provider_api_scripts/wordpress.pyindex e6e553f71..0f659f619 100644--- a/openverse_catalog/dags/providers/provider_api_scripts/wordpress.py+++ b/openverse_catalog/dags/providers/provider_api_scripts/wordpress.py@@ -37,6 +37,7 @@ DEFAULT_QUERY_PARAMS = {"format": "json","page": 1,"per_page": LIMIT,+ "_embed": "true",}delayed_requester = DelayedRequester(DELAY)@@ -45,14 +46,6 @@ image_store = ImageStore(provider=PROVIDER)license_url = "https://creativecommons.org/publicdomain/zero/1.0/"license_info = get_license_info(license_url=license_url)-IMAGE_RELATED_RESOURCES = {- "users": {}, # authors- "photo-categories": {},- "photo-colors": {},- "photo-orientations": {},- "photo-tags": {},-}-def main():"""@@ -61,55 +54,12 @@ def main():"""logger.info("Begin: WordPress Photo Directory script")- _prefetch_image_related_data()image_count = _get_images()image_store.commit()logger.info(f"Total images pulled: {image_count}")logger.info("Terminated!")-def _prefetch_image_related_data():- for resource in IMAGE_RELATED_RESOURCES.keys():- collection = _get_resources(resource)- IMAGE_RELATED_RESOURCES[resource] = collection- logger.info("Prefetch of image-related data completed.")---def _get_resources(resource_type):- total_pages = page = 1- endpoint = f"{ENDPOINT}/{resource_type}"- collection = {}- while total_pages >= page:- query_params = _get_query_params(page=page)- batch_data, total_pages = _get_item_page(endpoint, query_params=query_params)- if isinstance(batch_data, list) and len(batch_data) > 0:- collection_page = _process_resource_batch(resource_type, batch_data)- collection = collection | collection_page- page += 1- return collection---def _process_resource_batch(resource_type, batch_data):- collected_page = {}- for item in batch_data:- item_id, name, url = None, None, None- try:- item_id = item["id"]- name = item["name"]- if resource_type == "users":- # 'url' is the website of the author and 'link' would be their wp.org- # profile, so at least the last must always be present- url = item["url"] or item["link"]- except Exception as e:- logger.error(f"Couldn't save resource({resource_type}) info due to {e}")- continue- if resource_type == "users":- collected_page[item_id] = {"name": name, "url": url}- else:- collected_page[item_id] = name- return collected_page--def _get_query_params(page=1, default_query_params=None):if default_query_params is None:default_query_params = DEFAULT_QUERY_PARAMS@@ -168,15 +118,6 @@ def _process_image_batch(image_batch):return image_store.total_items-def _get_image_details(media_data):- try:- url = media_data.get("_links").get("wp:featuredmedia")[0].get("href")- response_json = _get_response_json(url, RETRIES, allow_redirects=False)- return response_json- except (KeyError, AttributeError):- return None--def _get_response_json(endpoint, retries=0, query_params=None, **kwargs):"""Function copied from common.requester.DelayedRequester class to allow responses@@ -217,79 +158,105 @@ def _extract_image_data(media_data):"""Extract data for individual item."""- try:- foreign_identifier = media_data["slug"]- foreign_landing_url = media_data["link"]- except (TypeError, KeyError, AttributeError):+ foreign_identifier = media_data.get("slug")+ if foreign_identifier is None:return None- title = _get_title(media_data)- image_details = _get_image_details(media_data)- if image_details is None:+ foreign_landing_url = media_data.get("link")++ try:+ media_details = (+ media_data.get("_embedded", {})+ .get("wp:featuredmedia", {})[0]+ .get("media_details", {})+ )+ except (KeyError, IndexError):return None- image_url, height, width, filetype = _get_file_info(image_details)++ image_url, height, width, filetype, filesize = _get_file_info(media_details)if image_url is None:return None- thumbnail = _get_thumbnail_url(image_details)- metadata = _get_metadata(media_data, image_details)- creator, creator_url = _get_creator_data(media_data)- tags = _get_related_data("tags", media_data)++ title = _get_title(media_data)+ author, author_url = _get_author_data(media_data)+ metadata, tags = _get_metadata(media_data, media_details)return {"title": title,- "creator": creator,- "creator_url": creator_url,+ "creator": author,+ "creator_url": author_url,"foreign_identifier": foreign_identifier,"foreign_landing_url": foreign_landing_url,"image_url": image_url,"height": height,"width": width,- "thumbnail_url": thumbnail,"filetype": filetype,+ "filesize": filesize,"license_info": license_info,"meta_data": metadata,"raw_tags": tags,}-def _get_file_info(image_details):- file_details = (- image_details.get("media_details", {}).get("sizes", {}).get("full", {})- )- image_url = file_details.get("source_url")- height = file_details.get("height")- width = file_details.get("width")- filetype = None- if filename := file_details.get("file"):- filetype = Path(filename).suffix.replace(".", "")- return image_url, height, width, filetype+def _get_file_info(media_details):+ preferred_sizes = ["2048x2048", "1536x1536", "medium_large", "large", "full"]+ for size in preferred_sizes:+ file_details = media_details.get("sizes", {}).get(size, {})+ image_url = file_details.get("source_url")+ if not image_url or image_url == "":+ continue+ height = file_details.get("height")+ width = file_details.get("width")+ filetype = None+ if filename := file_details.get("file"):+ filetype = Path(filename).suffix.replace(".", "")-def _get_thumbnail_url(image_details):- return (- image_details.get("media_details", {})- .get("sizes", {})- .get("thumbnail", {})- .get("source_url")- )+ filesize = (+ media_details.get("filesize", 0)+ if size == "full"+ else file_details.get("filesize", 0)+ )+ if not filesize or int(filesize) == 0:+ filesize = _get_filesize(image_url)+ return image_url, height, width, filetype, filesize+ return None, None, None, None, None-def _get_creator_data(image):- creator, creator_url = None, None- if author_id := image.get("author"):- creator = IMAGE_RELATED_RESOURCES.get("users").get(author_id, {}).get("name")- creator_url = IMAGE_RELATED_RESOURCES.get("users").get(author_id, {}).get("url")- return creator, creator_url++def _get_filesize(image_url):+ 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_author_data(image):+ try:+ raw_author = image.get("_embedded", {}).get("author", [])[0]+ except IndexError:+ return None, None+ author = raw_author.get("name")+ if author is None or author == "":+ author = raw_author.get("slug")+ author_url = raw_author.get("url")+ if author_url == "":+ author_url = raw_author.get("link")+ return author, author_urldef _get_title(image):if title := image.get("content", {}).get("rendered"):- title = html.fromstring(title).text_content()+ try:+ title = html.fromstring(title).text_content()+ except UnicodeDecodeError as e:+ logger.warning(f"Can't save the image's title ('{title}') due to {e}")+ return Nonereturn title-def _get_metadata(image, image_details):- raw_metadata = image_details.get("media_details", {}).get("image_meta", {})- metadata = {}+def _get_metadata(media_data, media_details):+ raw_metadata = media_details.get("image_meta", {})+ metadata, tags = {}, []extras = ["aperture","camera",@@ -303,25 +270,26 @@ def _get_metadata(image, image_details):if value not in [None, ""]:metadata[key] = value- if published_date := image_details.get("date"):- metadata["published_date"] = published_date-- # these fields require looking up additional queried data- resource_extras = ["categories", "colors", "orientations"]- for resource in resource_extras:- metadata[resource] = sorted(_get_related_data(resource, image))- return metadata---def _get_related_data(resource_type, image):- resource_type = f"photo-{resource_type}"- ids = image.get(resource_type)- resource_names = set()- resources_ids = IMAGE_RELATED_RESOURCES[resource_type].keys()- for rid in ids:- if rid in resources_ids:- resource_names.add(IMAGE_RELATED_RESOURCES[resource_type][rid])- return list(resource_names)+ raw_related_resources = media_data.get("_embedded", {}).get("wp:term", [])+ resource_mapping = {+ "photo_category": "categories",+ "photo_color": "colors",+ "photo_orientation": "orientation",+ "photo_tag": "tags",+ }+ for resource_arr in raw_related_resources:+ for resource in resource_arr:+ if (txy := resource.get("taxonomy")) in resource_mapping.keys():+ resource_key = resource_mapping[txy]+ resource_val = resource.get("name")+ if txy == "photo_tag":+ tags.append(resource_val)+ elif txy == "photo_orientation":+ metadata["orientation"] = resource_val+ else:+ metadata.setdefault(resource_key, [])+ metadata[resource_key].append(resource_val)+ return metadata, tagsif __name__ == "__main__":diff --git a/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item.json b/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item.jsonindex 4dacfacf9..1a12932b6 100644--- a/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item.json+++ b/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item.json@@ -1,4 +1,388 @@{+ "_embedded": {+ "author": [+ {+ "_links": {+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wporg/v1/users?slug=st810amaze"+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wporg/v1/users/st810amaze"+ }+ ]+ },+ "avatar_urls": {+ "24": "https://secure.gravatar.com/avatar/5ea209f8c1303acd3863e6d4ba8a64c7?s=24&d=mm&r=g",+ "48": "https://secure.gravatar.com/avatar/5ea209f8c1303acd3863e6d4ba8a64c7?s=48&d=mm&r=g",+ "96": "https://secure.gravatar.com/avatar/5ea209f8c1303acd3863e6d4ba8a64c7?s=96&d=mm&r=g"+ },+ "description": "Communication Designer based in Tokyo, Japan.\r\nCI (Corporate Identity), VI (Visual Identity), Marketing, Branding are some of my passion.\r\nLove taking photos and videos.\r\nFormer web designer and developer.",+ "id": 14875848,+ "link": "https://wordpress.org/photos/author/st810amaze/",+ "name": "Shusei Toda",+ "slug": "st810amaze",+ "url": "https://shuseitoda.com"+ }+ ],+ "wp:featuredmedia": [+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/types/attachment"+ }+ ],+ "author": [+ {+ "embeddable": true,+ "href": "https://wordpress.org/photos/wp-json/wporg/v1/users/st810amaze"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/media"+ }+ ],+ "replies": [+ {+ "embeddable": true,+ "href": "https://wordpress.org/photos/wp-json/wp/v2/comments?post=9919"+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/media/9919"+ }+ ]+ },+ "alt_text": "",+ "author": 14875848,+ "caption": {+ "rendered": "<p>Coffee Bean with bags</p>\n"+ },+ "date": "2022-05-14T04:37:15",+ "id": 9919,+ "link": "https://pd.w.org/2022/05/203627f31f8770f03.61535278.jpg",+ "media_details": {+ "file": "2022/05/203627f31f8770f03.61535278.jpg",+ "filesize": 633576,+ "gcs_bucket": "pd.w.org",+ "gcs_name": "2022/05/203627f31f8770f03.61535278.jpg",+ "gcs_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278.jpg",+ "height": 2667,+ "image_meta": {+ "aperture": "4",+ "camera": "ILCE-7M4",+ "caption": "",+ "copyright": "Shusei Toda",+ "created_timestamp": "1652338105",+ "credit": "Shusei Toda",+ "focal_length": "55",+ "iso": "6400",+ "keywords": [],+ "orientation": "0",+ "shutter_speed": "0.008",+ "title": ""+ },+ "sizes": {+ "1536x1536": {+ "file": "203627f31f8770f03.61535278-1536x1024.jpg",+ "filesize": 336961,+ "height": 1024,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-1536x1024.jpg",+ "width": 1536+ },+ "2048x2048": {+ "file": "203627f31f8770f03.61535278-2048x1366.jpg",+ "filesize": 544284,+ "height": 1366,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-2048x1366.jpg",+ "width": 2048+ },+ "full": {+ "file": "203627f31f8770f03.61535278.jpg",+ "height": 2667,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278.jpg",+ "width": 4000+ },+ "large": {+ "file": "203627f31f8770f03.61535278-1024x683.jpg",+ "filesize": 169526,+ "height": 683,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-1024x683.jpg",+ "width": 1024+ },+ "medium": {+ "file": "203627f31f8770f03.61535278-300x200.jpg",+ "filesize": 19688,+ "height": 200,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-300x200.jpg",+ "width": 300+ },+ "medium_large": {+ "file": "203627f31f8770f03.61535278-768x512.jpg",+ "filesize": 101952,+ "height": 512,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-768x512.jpg",+ "width": 768+ },+ "thumbnail": {+ "file": "203627f31f8770f03.61535278-150x150.jpg",+ "filesize": 9058,+ "height": 150,+ "mime_type": "image/jpeg",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278-150x150.jpg",+ "width": 150+ }+ },+ "width": 4000+ },+ "media_type": "image",+ "mime_type": "image/jpeg",+ "slug": "203627f31f-photo",+ "source_url": "https://pd.w.org/2022/05/203627f31f8770f03.61535278.jpg",+ "title": {+ "rendered": "203627f31f-photo"+ },+ "type": "attachment"+ }+ ],+ "wp:term": [+ [+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_category"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-categories"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-categories/7"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-categories=7"+ }+ ]+ },+ "id": 7,+ "link": "https://wordpress.org/photos/c/food-drink/",+ "name": "food-drink",+ "slug": "food-drink",+ "taxonomy": "photo_category"+ }+ ],+ [+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_color"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors/18"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-colors=18"+ }+ ]+ },+ "id": 18,+ "link": "https://wordpress.org/photos/color/brown/",+ "name": "brown",+ "slug": "brown",+ "taxonomy": "photo_color"+ },+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_color"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors/13"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-colors=13"+ }+ ]+ },+ "id": 13,+ "link": "https://wordpress.org/photos/color/orange/",+ "name": "orange",+ "slug": "orange",+ "taxonomy": "photo_color"+ }+ ],+ [+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_orientation"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations/23"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-orientations=23"+ }+ ]+ },+ "id": 23,+ "link": "https://wordpress.org/photos/orientation/landscape/",+ "name": "landscape",+ "slug": "landscape",+ "taxonomy": "photo_orientation"+ }+ ],+ [+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_tag"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags/1099"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-tags=1099"+ }+ ]+ },+ "id": 1099,+ "link": "https://wordpress.org/photos/t/bean/",+ "name": "bean",+ "slug": "bean",+ "taxonomy": "photo_tag"+ },+ {+ "_links": {+ "about": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_tag"+ }+ ],+ "collection": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags"+ }+ ],+ "curies": [+ {+ "href": "https://api.w.org/{rel}",+ "name": "wp",+ "templated": true+ }+ ],+ "self": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags/337"+ }+ ],+ "wp:post_type": [+ {+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-tags=337"+ }+ ]+ },+ "id": 337,+ "link": "https://wordpress.org/photos/t/coffee/",+ "name": "coffee",+ "slug": "coffee",+ "taxonomy": "photo_tag"+ }+ ]+ ]+ },"_links": {"about": [{@@ -8,7 +392,7 @@"author": [{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/users/3606"+ "href": "https://wordpress.org/photos/wp-json/wporg/v1/users/st810amaze"}],"collection": [@@ -25,92 +409,92 @@],"predecessor-version": [{- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/43/revisions/45",- "id": 45+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/9917/revisions/9921",+ "id": 9921}],"self": [{- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/43"+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/9917"}],"version-history": [{"count": 1,- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/43/revisions"+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photos/9917/revisions"}],"wp:attachment": [{- "href": "https://wordpress.org/photos/wp-json/wp/v2/media?parent=43"+ "href": "https://wordpress.org/photos/wp-json/wp/v2/media?parent=9917"}],"wp:featuredmedia": [{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/media/44"+ "href": "https://wordpress.org/photos/wp-json/wp/v2/media/9919"}],"wp:term": [{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-categories?post=43",+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-categories?post=9917","taxonomy": "photo_category"},{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors?post=43",+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-colors?post=9917","taxonomy": "photo_color"},{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations?post=43",+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations?post=9917","taxonomy": "photo_orientation"},{"embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags?post=43",+ "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-tags?post=9917","taxonomy": "photo_tag"}]},- "author": 3606,+ "author": 14875848,"content": {"protected": false,- "rendered": "<p>Lupinus polyphyllus (aka Washington lupine)</p>\n"+ "rendered": "<p>Coffee Bean with bags</p>\n"},- "date": "2021-06-08T07:37:45",- "date_gmt": "2021-06-08T07:37:45",- "featured_media": 44,+ "date": "2022-05-16T18:50:55",+ "date_gmt": "2022-05-16T18:50:55",+ "featured_media": 9919,"guid": {- "rendered": "https://wordpress.org/photos/?post_type=photo&p=43"+ "rendered": "https://wordpress.org/photos/?post_type=photo&p=9917"},- "id": 43,- "link": "https://wordpress.org/photos/photo/56560bf1d6/",+ "id": 9917,+ "link": "https://wordpress.org/photos/photo/203627f31f/","meta": {"spay_email": ""},- "modified": "2021-06-08T07:37:45",- "modified_gmt": "2021-06-08T07:37:45",+ "modified": "2022-05-16T18:50:55",+ "modified_gmt": "2022-05-16T18:50:55","photo-categories": [- 8+ 7],"photo-colors": [- 15,- 36+ 18,+ 13],"photo-orientations": [- 24+ 23],"photo-tags": [- 35,- 28+ 1099,+ 337],- "slug": "56560bf1d6",+ "slug": "203627f31f","status": "publish","template": "","title": {- "rendered": "56560bf1d6"+ "rendered": "203627f31f"},"type": "photo"}diff --git a/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item_details.json b/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item_details.jsondeleted file mode 100644index ecf9c104c..000000000--- a/tests/dags/providers/provider_api_scripts/resources/wordpress/full_item_details.json+++ /dev/null@@ -1,137 +0,0 @@-{- "_links": {- "about": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/types/attachment"- }- ],- "author": [- {- "embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/users/3606"- }- ],- "collection": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/media"- }- ],- "replies": [- {- "embeddable": true,- "href": "https://wordpress.org/photos/wp-json/wp/v2/comments?post=44"- }- ],- "self": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/media/44"- }- ]- },- "alt_text": "",- "author": 3606,- "caption": {- "rendered": "<p>Lupinus polyphyllus (aka Washington lupine)</p>\n"- },- "comment_status": "open",- "date": "2021-06-08T07:34:17",- "date_gmt": "2021-06-08T07:34:17",- "description": {- "rendered": "<p class=\"attachment\"><a href='https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg'><img width=\"225\" height=\"300\" src=\"https://pd.w.org/2021/06/56560bf1d69971f38.94814132-225x300.jpg\" class=\"attachment-medium size-medium\" alt=\"\" loading=\"lazy\" srcset=\"https://storage.googleapis.com/pd.w.org/2021/06/56560bf1d69971f38.94814132-225x300.jpg 225w, https://storage.googleapis.com/pd.w.org/2021/06/56560bf1d69971f38.94814132-768x1024.jpg 768w, https://storage.googleapis.com/pd.w.org/2021/06/56560bf1d69971f38.94814132-1152x1536.jpg 1152w, https://storage.googleapis.com/pd.w.org/2021/06/56560bf1d69971f38.94814132-1536x2048.jpg 1536w\" sizes=\"(max-width: 225px) 100vw, 225px\" /></a></p>\n<p>Lupinus polyphyllus (aka Washington lupine)</p>\n"- },- "guid": {- "rendered": "https://storage.googleapis.com/pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg"- },- "id": 44,- "link": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg",- "media_details": {- "file": "2021/06/56560bf1d69971f38.94814132.jpg",- "gcs_bucket": "pd.w.org",- "gcs_name": "2021/06/56560bf1d69971f38.94814132.jpg",- "gcs_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg",- "height": 4032,- "image_meta": {- "aperture": "1.8",- "camera": "Pixel 2 XL",- "caption": "",- "copyright": "",- "created_timestamp": "1591460495",- "credit": "Scott Reilly",- "focal_length": "4.459",- "iso": "45",- "keywords": [],- "orientation": "1",- "shutter_speed": "0.00171",- "title": ""- },- "sizes": {- "1536x1536": {- "file": "56560bf1d69971f38.94814132-1152x1536.jpg",- "height": 1536,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-1152x1536.jpg",- "width": 1152- },- "2048x2048": {- "file": "56560bf1d69971f38.94814132-1536x2048.jpg",- "height": 2048,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-1536x2048.jpg",- "width": 1536- },- "full": {- "file": "56560bf1d69971f38.94814132.jpg",- "height": 4032,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg",- "width": 3024- },- "large": {- "file": "56560bf1d69971f38.94814132-768x1024.jpg",- "height": 1024,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-768x1024.jpg",- "width": 768- },- "medium": {- "file": "56560bf1d69971f38.94814132-225x300.jpg",- "height": 300,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-225x300.jpg",- "width": 225- },- "medium_large": {- "file": "56560bf1d69971f38.94814132-768x1024.jpg",- "height": 1024,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-768x1024.jpg",- "width": 768- },- "thumbnail": {- "file": "56560bf1d69971f38.94814132-150x150.jpg",- "height": 150,- "mime_type": "image/jpeg",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132-150x150.jpg",- "width": 150- }- },- "width": 3024- },- "media_type": "image",- "meta": {- "spay_email": ""- },- "mime_type": "image/jpeg",- "modified": "2021-09-23T21:56:53",- "modified_gmt": "2021-09-23T21:56:53",- "ping_status": "closed",- "post": 43,- "slug": "washington-lupine",- "source_url": "https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg",- "status": "inherit",- "template": "",- "title": {- "rendered": "washington-lupine"- },- "type": "attachment"-}diff --git a/tests/dags/providers/provider_api_scripts/resources/wordpress/orientations_batch.json b/tests/dags/providers/provider_api_scripts/resources/wordpress/orientations_batch.jsondeleted file mode 100644index 5cf4efdb3..000000000--- a/tests/dags/providers/provider_api_scripts/resources/wordpress/orientations_batch.json+++ /dev/null@@ -1,119 +0,0 @@-[- {- "_links": {- "about": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_orientation"- }- ],- "collection": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations"- }- ],- "curies": [- {- "href": "https://api.w.org/{rel}",- "name": "wp",- "templated": true- }- ],- "self": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations/23"- }- ],- "wp:post_type": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-orientations=23"- }- ]- },- "count": 1,- "description": "",- "id": 23,- "link": "https://wordpress.org/photos/orientation/landscape/",- "meta": [],- "name": "landscape",- "slug": "landscape",- "taxonomy": "photo_orientation"- },- {- "_links": {- "about": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_orientation"- }- ],- "collection": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations"- }- ],- "curies": [- {- "href": "https://api.w.org/{rel}",- "name": "wp",- "templated": true- }- ],- "self": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations/24"- }- ],- "wp:post_type": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-orientations=24"- }- ]- },- "count": 1,- "description": "",- "id": 24,- "link": "https://wordpress.org/photos/orientation/portrait/",- "meta": [],- "name": "portrait",- "slug": "portrait",- "taxonomy": "photo_orientation"- },- {- "_links": {- "about": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/taxonomies/photo_orientation"- }- ],- "collection": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations"- }- ],- "curies": [- {- "href": "https://api.w.org/{rel}",- "name": "wp",- "templated": true- }- ],- "self": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photo-orientations/25"- }- ],- "wp:post_type": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/photos?photo-orientations=25"- }- ]- },- "count": 0,- "description": "",- "id": 25,- "link": "https://wordpress.org/photos/orientation/square/",- "meta": [],- "name": "square",- "slug": "square",- "taxonomy": "photo_orientation"- }-]diff --git a/tests/dags/providers/provider_api_scripts/resources/wordpress/users_batch.json b/tests/dags/providers/provider_api_scripts/resources/wordpress/users_batch.jsondeleted file mode 100644index 6b82ce0da..000000000--- a/tests/dags/providers/provider_api_scripts/resources/wordpress/users_batch.json+++ /dev/null@@ -1,28 +0,0 @@-[- {- "_links": {- "collection": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/users"- }- ],- "self": [- {- "href": "https://wordpress.org/photos/wp-json/wp/v2/users/3606"- }- ]- },- "avatar_urls": {- "24": "https://secure.gravatar.com/avatar/5af525fba95b93c4e0c8a03b639c9486?s=24&d=mm&r=g",- "48": "https://secure.gravatar.com/avatar/5af525fba95b93c4e0c8a03b639c9486?s=48&d=mm&r=g",- "96": "https://secure.gravatar.com/avatar/5af525fba95b93c4e0c8a03b639c9486?s=96&d=mm&r=g"- },- "description": "",- "id": 3606,- "link": "https://profiles.wordpress.org/coffee2code/",- "meta": [],- "name": "Scott Reilly",- "slug": "coffee2code",- "url": "http://coffee2code.com"- }-]diff --git a/tests/dags/providers/provider_api_scripts/test_wordpress.py b/tests/dags/providers/provider_api_scripts/test_wordpress.pyindex a27304b88..e82219b88 100644--- a/tests/dags/providers/provider_api_scripts/test_wordpress.py+++ b/tests/dags/providers/provider_api_scripts/test_wordpress.py@@ -9,7 +9,6 @@ import wordpress as wpRESOURCES = Path(__file__).parent / "resources/wordpress"SAMPLE_MEDIA_DATA = RESOURCES / "full_item.json"-SAMPLE_MEDIA_DETAILS = RESOURCES / "full_item_details.json"logging.basicConfig(format="%(asctime)s - %(name)s - %(levelname)s: %(message)s",@@ -18,21 +17,13 @@ logging.basicConfig(def test_get_query_params_returns_defaults():- expected_result = {- "format": "json",- "page": 1,- "per_page": 100,- }+ expected_result = {"format": "json", "page": 1, "per_page": 100, "_embed": "true"}actual_result = wp._get_query_params()assert actual_result == expected_resultdef test_get_query_params_returns_defaults_with_given_page():- expected_result = {- "format": "json",- "page": 3,- "per_page": 100,- }+ expected_result = {"format": "json", "page": 3, "per_page": 100, "_embed": "true"}actual_result = wp._get_query_params(page=3)assert actual_result == expected_result@@ -53,29 +44,6 @@ def test_get_item_page_returns_correctly_with_no_results():assert actual_result == expected_result-def test_process_resource_batch_with_non_users_resources():- # Apply for photo categories, colors and tags too- with open(RESOURCES / "orientations_batch.json") as f:- batch_data = json.load(f)- actual_result = wp._process_resource_batch("photo-orientations", batch_data)- expected_result = {23: "landscape", 24: "portrait", 25: "square"}- assert actual_result == expected_result---def test_process_resource_batch_with_users():- with open(RESOURCES / "users_batch.json") as f:- batch_data = json.load(f)- actual_result = wp._process_resource_batch("users", batch_data)- expected_result = {3606: {"name": "Scott Reilly", "url": "http://coffee2code.com"}}- assert actual_result == expected_result---def test_extract_image_data_returns_none_when_media_data_none():- actual_image_info = wp._extract_image_data(None)- expected_image_info = None- assert actual_image_info is expected_image_info--def test_extract_image_data_returns_none_when_no_foreign_id():with open(SAMPLE_MEDIA_DATA) as f:image_data = json.load(f)@@ -85,10 +53,10 @@ def test_extract_image_data_returns_none_when_no_foreign_id():assert actual_image_info is expected_image_info-def test_extract_image_data_returns_none_when_no_image_details():+def test_extract_image_data_returns_none_when_no_image_url():with open(SAMPLE_MEDIA_DATA) as f:image_data = json.load(f)- image_data.pop("_links", None)+ image_data["_embedded"]["wp:featuredmedia"][0]["media_details"].pop("sizes")actual_image_info = wp._extract_image_data(image_data)assert actual_image_info is None@@ -97,89 +65,77 @@ def test_get_title():with open(SAMPLE_MEDIA_DATA) as f:image_data = json.load(f)actual_result = wp._get_title(image_data)- expected_result = "Lupinus polyphyllus (aka Washington lupine)"+ expected_result = "Coffee Bean with bags"assert actual_result == expected_resultdef test_get_file_info():- with open(SAMPLE_MEDIA_DETAILS) as f:- image_details = json.load(f)+ with open(SAMPLE_MEDIA_DATA) as f:+ image_details = (+ json.load(f)+ .get("_embedded")+ .get("wp:featuredmedia")[0]+ .get("media_details")+ )actual_result = wp._get_file_info(image_details)expected_result = (- "https://pd.w.org/2021/06/56560bf1d69971f38.94814132.jpg", # image_url- 4032, # height- 3024, # width+ "https://pd.w.org/2022/05/203627f31f8770f03.61535278-2048x1366.jpg", # image_url+ 1366, # height+ 2048, # width"jpg", # filetype+ 544284, # filesize)assert actual_result == expected_result-def test_get_creator_data():+def test_get_author_data_when_is_non_empty():with open(SAMPLE_MEDIA_DATA) as f:image_data = json.load(f)- image_related_patch = {- "users": {- 3606: {"name": "Scott Reilly", "url": "http://coffee2code.com"},- }- }- with patch.object(wp, "IMAGE_RELATED_RESOURCES", image_related_patch):- actual_creator, actual_creator_url = wp._get_creator_data(image_data)- expected_creator = "Scott Reilly"- expected_creator_url = "http://coffee2code.com"+ actual_author, actual_author_url = wp._get_author_data(image_data)+ expected_author = "Shusei Toda"+ expected_author_url = "https://shuseitoda.com"+ assert actual_author == expected_author+ assert actual_author_url == expected_author_url- assert actual_creator == expected_creator- assert actual_creator_url == expected_creator_url+def test_get_author_data_handle_no_author():+ with open(SAMPLE_MEDIA_DATA) as f:+ image_data = json.load(f)+ image_data["_embedded"].pop("author", None)+ actual_author, actual_author_url = wp._get_author_data(image_data)+ assert actual_author is None+ assert actual_author_url is None-def test_get_creator_data_handle_no_author():++def test_get_author_data_use_slug_when_name_is_empty():with open(SAMPLE_MEDIA_DATA) as f:image_data = json.load(f)- image_data.pop("author")- image_related_patch = {- "users": {- 3606: {"name": "Scott Reilly", "url": "http://coffee2code.com"},- }- }- with patch.object(wp, "IMAGE_RELATED_RESOURCES", image_related_patch):- actual_creator, actual_creator_url = wp._get_creator_data(image_data)- assert actual_creator is None- assert actual_creator_url is None+ image_data["_embedded"]["author"][0].pop("name")+ actual_author, _ = wp._get_author_data(image_data)+ expected_author = "st810amaze"+ assert actual_author == expected_authordef test_get_metadata():- with open(SAMPLE_MEDIA_DATA) as f1, open(SAMPLE_MEDIA_DETAILS) as f2:- image_data = json.load(f1)- image_details = json.load(f2)- image_related_patch = {- "photo-colors": {15: "green", 36: "purple"},- "photo-orientations": {24: "portrait"},- "photo-categories": {8: "nature"},- }- with patch.object(wp, "IMAGE_RELATED_RESOURCES", image_related_patch):- actual_metadata = wp._get_metadata(image_data, image_details)-+ with open(SAMPLE_MEDIA_DATA) as f:+ image_data = json.load(f)+ image_details = (+ image_data.get("_embedded").get("wp:featuredmedia")[0].get("media_details")+ )+ actual_metadata, actual_tags = wp._get_metadata(image_data, image_details)expected_metadata = {- "aperture": "1.8",- "camera": "Pixel 2 XL",- "created_timestamp": "1591460495",- "focal_length": "4.459",- "iso": "45",- "shutter_speed": "0.00171",- "published_date": "2021-06-08T07:34:17",- "categories": ["nature"],- "colors": ["green", "purple"],- "orientations": ["portrait"],+ "aperture": "4",+ "camera": "ILCE-7M4",+ "created_timestamp": "1652338105",+ "focal_length": "55",+ "iso": "6400",+ "shutter_speed": "0.008",+ "categories": ["food-drink"],+ "colors": ["brown", "orange"],+ "orientation": "landscape",}- assert len(actual_metadata) == len(expected_metadata)+ expected_tags = ["bean", "coffee"]+ # assert len(actual_metadata) == len(expected_metadata)assert actual_metadata == expected_metadata---def test_get_related_data_with_tags():- with open(SAMPLE_MEDIA_DATA) as f:- image_data = json.load(f)- prefetched_tags = {"photo-tags": {35: "flower", 28: "plant"}}- with patch.object(wp, "IMAGE_RELATED_RESOURCES", prefetched_tags):- actual_tags = wp._get_related_data("tags", image_data)- expected_tags = ["flower", "plant"]assert len(actual_tags) == len(expected_tags)- assert sorted(expected_tags) == sorted(actual_tags)+ assert actual_tags == expected_tags