User:Martyav/Sandbox/API:Images

MediaWiki version:
1.11

GET Request to list embedded media files on provided pages

Note: This module can be used as a generator.

API documentation

edit

prop=images (im)

(main | query | images)
  • This module requires read rights.
  • This module can be used as a generator.
  • Source: MediaWiki
  • License: GPL-2.0-or-later

Returns all files contained on the given pages.

Specific parameters:
Other general parameters are available.
imlimit

How many files to return.

Type: integer or max
The value must be between 1 and 500.
Default: 10
imcontinue

When more results are available, use this to continue. More detailed information on how to continue queries can be found on mediawiki.org.

imimages

Only list these files. Useful for checking whether a certain page has a certain file.

Separate values with | or alternative.
Maximum number of values is 50 (500 for clients that are allowed higher limits).
imdir

The direction in which to list.

One of the following values: ascending, descending
Default: ascending


Example

edit

GET Request

edit
In the below query, we call the API to get a list of image files embedded on the English Wikipedia's page on Albert Einstein.

Response

edit
{
    "continue": {
        "imcontinue": "736|Albert_Einstein_signature_1934.svg",
        "continue": "||"
    },
    "query": {
        "pages": {
            "736": {
                "pageid": 736,
                "ns": 0,
                "title": "Albert Einstein",
                "images": [
                    {
                        "ns": 6,
                        "title": "File:1919 eclipse positive.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Albert Einstein's exam of maturity grades (color2).jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Albert Einstein (Nobel).png"
                    },
                    ...
                ]
            }
        }
    }
}

Sample code

edit

get_page_images.py

#!/usr/bin/python3

"""
    get_page_images.py
    MediaWiki Action API Code Samples
    Demo of `Images` module: Send a GET request to obtain a JSON
    object listing all of the image files embedded on a single
    page.
    MIT license
"""

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

PARAMS = {
    'action':'query',
    'format':'json',
    'prop':'images',
    'titles':'Albert Einstein'
}

R = S.get(url=URL, params=PARAMS)

DATA = R.json()
print(DATA)

Possible errors

edit

None

Parameter History

edit
  • v1.13: Introduced imcontinue, imlimit
  • v1.18: Introduced imimages
  • v1.19: Introduced imdir

See also

edit