art-institute-of-chicago / data-aggregator

An API of public data collected from several different systems at the Art Institute of Chicago
GNU Affero General Public License v3.0
65 stars 6 forks source link

[Question] Random fetch ? #59

Closed subskill-qbassalair closed 1 year ago

subskill-qbassalair commented 1 year ago

Hello, first of all, thank you for this amazing work, i was wondering if there is a way to fetch random Artwork ? I read the doc but it don't look and i need this feature for a little personal project i have in mind.

In advance, thank you for your answer

nikhiltri commented 1 year ago

Hi Quentin,

Thanks for reaching out! Yes, you can query for a random entry in any of our endpoints. There are a couple existing projects that also involve querying our API for random artworks, which you can reference as examples. Here are some relevant snippets:

The query you'll want is probably similar to the second example. The first example provides an additional term query that filters by "oil paint" material.

You can send the query as POST; it might look something like this:

curl --location --request POST 'https://api.artic.edu/api/v1/search' \
--header 'Content-Type: application/json' \
--data-raw '{
    "resources": "artworks",
    "fields": [
        "id",
        "title",
        "artist_display",
        "date_display",
        "image_id"
    ],
    "boost": false,
    "limit": 1,
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "filter": [
                        {
                            "term": {
                                "is_public_domain": true
                            }
                        },
                        {
                            "exists": {
                                "field": "image_id"
                            }
                        }
                    ]
                }
            },
            "boost_mode": "replace",
            "random_score": {
                "field": "id",
                "seed": '$RANDOM'
            }
        }
    }
}'

Note the use of $RANDOM, that's for cache-busting.

https://stackoverflow.com/questions/1194882/how-to-generate-random-number-in-bash  

Most users who request a random artwork will also want to get an image representing that work. To look at how we provide this let's take a look at the artworks resources for Van Gogh’s The Bedroom:

http://api.artic.edu/api/v1/artworks/28560?fields=id,title,image_id

Note how there is an image_id field. This is the primary image associated with this artwork. (You can find alternative images via the alt_image_ids field.)

You can cross-reference these images on the images endpoint. You probably don't need to do so for your use-case, but for the purpose of this explainer you can take a look here:

http://api.artic.edu/api/v1/images/25c31d8d-21a4-9ea1-1d73-6a2eca4dda7e 

So after retrieving a random artwork, you can take the value of its image_id field, and insert it into this URL template:

https://www.artic.edu/iiif/2/{image_id}/full/843,/0/default.jpg

That will give you the artwork image at the same dimension used on our website. So you can be pretty sure the image will be cached in our global CDN making your app more performant.

Hope this helps! Please don't hesitate to reach out with additional questions. And if you like the way your project turns out, feel free to send it our way. We love seeing what folks build with our API.

Thanks! nikhil

subskill-qbassalair commented 1 year ago

Thank's you for this complet answer, unfortunaly the first link give me a 404 error page, i can't see the code. I'm trying to understand the 2nd exemple but it's a little hard for me haha (i'm still a student).

And I'm using you're API with react.

The query i do :

useEffect(() => { fetch(https://api.artic.edu/api/v1/artworks/123443?fields=id,title,image_id,artist_display) .then(response => response.json()) .then(data => setArtwork(data.data)) .catch(error => setError(error)); }, []);

I tryed to put random number ID but it don't look to work.

Yes, hope i'll can share with you the result of this work ! Ill use three js too, to render 3D artwork, but guess what, I'm still learing three js too haha. So, this is not for tomorrow, but not in a so long time ;)

EDIT : i did like in the 2nd example, change id by date in milliseconds, it's working, but not 100%, sometimes, this id doesn"t existe and give a 404 error. I tryed to multiply milliseconds, but still don"t work everytime :(

nikhiltri commented 1 year ago

Hey @subskill-qbassalair,

My apologies about the bad first link. Github added some cruft to the URL in my comment. Try this: https://github.com/art-institute-of-chicago/aic-bash/blob/master/queries/default-random-oil-painting.json

In your example, you'll want to submit a POST request to https://api.artic.edu/api/v1/artworks and provide something like the JSON in the above link as the body of your request.

Hope this helps! nikhil

subskill-qbassalair commented 1 year ago

Hello. Thank you for this fast answer ! (haha) But is ok i finally got it.

There is the final result, if you want to take a look : https://silly-klepon-0f5dff.netlify.app/

Have a nice day !

nikhiltri commented 1 year ago

Nice!