Jump to content

API:Images/th: Difference between revisions

From mediawiki.org
Content deleted Content added
Created page with "== Demo app(s) =="
FuzzyBot (talk | contribs)
Updating to match new version of source page
 
(5 intermediate revisions by 2 users not shown)
Line 9: Line 9:


== API documentation ==
== API documentation ==
{{Api help|query+images|frame=yes}}
{| style="color: black; background-color: #f8f8f8; border-spacing: 20px; border: 1px solid darkgray;"
| {{Api help|query+images}}
|}


<span id="Example"></span>
== ตัวอย่าง ==
== ตัวอย่าง ==


Line 58: Line 57:
</syntaxhighlight>
</syntaxhighlight>


<span id="Sample_code"></span>
=== รหัสตัวอย่าง ===
=== รหัสตัวอย่าง ===


Line 66: Line 66:
* {{ll|API:Picture of the day viewer}}
* {{ll|API:Picture of the day viewer}}


<span id="Parameter_history"></span>
<div lang="en" dir="ltr" class="mw-content-ltr">
== Parameter history ==
== ประวัติพารามิเตอร์ (Parameter history) ==
* v1.13: เปิดตัว <code>imcontinue</code>, <code>imlimit</code>
</div>
* v1.18: เปิดตัว <code>imimages</code>
* v1.13: <span lang="en" dir="ltr" class="mw-content-ltr">Introduced <code>imcontinue</code>, <code>imlimit</code></span>
* v1.18: <span lang="en" dir="ltr" class="mw-content-ltr">Introduced <code>imimages</code></span>
* v1.19: เปิดตัว <code>imdir</code>
* v1.19: <span lang="en" dir="ltr" class="mw-content-ltr">Introduced <code>imdir</code></span>


<span id="See_also"></span>
<div lang="en" dir="ltr" class="mw-content-ltr">
== See also ==
== ดูเพิ่ม ==
</div>
* {{ll|API:Imageinfo}} - <span lang="en" dir="ltr" class="mw-content-ltr">Gets information for any titles in the image namespace.</span> <span lang="en" dir="ltr" class="mw-content-ltr">[[API/Architecture work/Planning#Rewrite_imageinfo_as_fileinfo|Will eventually get superseded]]</span> <span lang="en" dir="ltr" class="mw-content-ltr">by <code>prop=fileinfo</code>.</span>
* {{ll|API:Imageinfo}} - <span lang="en" dir="ltr" class="mw-content-ltr">Gets information for any titles in the image namespace.</span> <span lang="en" dir="ltr" class="mw-content-ltr">[[API/Architecture work/Planning#Rewrite_imageinfo_as_fileinfo|Will eventually get superseded]]</span> <span lang="en" dir="ltr" class="mw-content-ltr">by <code>prop=fileinfo</code>.</span>
* {{ll|API:Stashimageinfo}} - <span lang="en" dir="ltr" class="mw-content-ltr">Gets information for stashed images or files in the namespace.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Output is identical to {{ll|API:Imageinfo}}.</span>
* {{ll|API:Stashimageinfo}} - <span lang="en" dir="ltr" class="mw-content-ltr">Gets information for stashed images or files in the namespace.</span> <span lang="en" dir="ltr" class="mw-content-ltr">Output is identical to {{ll|API:Imageinfo}}.</span>

Latest revision as of 18:11, 29 August 2024

เวอร์ชันมีเดียวิกิ:
1.11

GET request to list embedded media files on provided pages.

This module can be used as a generator .

API documentation

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

ตัวอย่าง

GET request

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

{
    "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"
                    },
                    ...
                ]
            }
        }
    }
}

รหัสตัวอย่าง

Python

#!/usr/bin/python3

"""
    get_page_images.py

    MediaWiki API Demos
    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",
    "titles": "Albert Einstein",
    "prop": "images"
}

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

PAGES = DATA['query']['pages']

for k, v in PAGES.items():
    for img in v['images']:
        print(img["title"])

PHP

<?php
/*
    get_page_images.php

    MediaWiki API Demos
    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
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "prop" => "images",
    "titles" => "Albert Einstein",
    "format" => "json"
];

$url = $endPoint . "?" . http_build_query( $params );

$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );

$result = json_decode( $output, true );

foreach( $result["query"]["pages"] as $k => $v ) {
    foreach( $v["images"] as $k => $v ) {
        echo( $v["title"] . "\n" );
    }
}

JavaScript

/*
    get_page_images.js

    MediaWiki API Demos
    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
*/

var url = "https://en.wikipedia.org/w/api.php"; 

var params = {
    action: "query",
    prop: "images",
    titles: "Albert Einstein",
    format: "json"
};

url = url + "?origin=*";
Object.keys(params).forEach(function(key){url += "&" + key + "=" + params[key];});

fetch(url)
    .then(function(response){return response.json();})
    .then(function(response) {
        var pages = response.query.pages;
        for (var page in pages) {
            for (var img of pages[page].images) {
                console.log(img.title);
            }
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_page_images.js

	MediaWiki API Demos
	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
*/

var params = {
		action: 'query',
		prop: 'images',
		titles: 'Albert Einstein',
		format: 'json'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		page;
	for ( page in pages ) {
		pages[ page ].images.forEach( function ( img ) {
			console.log( img.title );
		} );
	}
} );

Demo app(s)

ประวัติพารามิเตอร์ (Parameter history)

  • v1.13: เปิดตัว imcontinue, imlimit
  • v1.18: เปิดตัว imimages
  • v1.19: เปิดตัว imdir

ดูเพิ่ม