Jump to content

API:Allimages/Beispielcode 1

From mediawiki.org
This page is a translated version of the page API:Allimages/Sample code 1 and the translation is 100% complete.

Python

#!/usr/bin/python3

"""
    get_allimages_by_name.py

    MediaWiki-API-Demos
    List all images in the namespace, starting from files that begin with 'Graffiti_000'.
    Begrenzt die erste Antwort nur auf die ersten drei Bildern.

    MIT-Lizenz
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "list": "allimages",
    "aifrom": "Graffiti_000",
    "ailimit": "3"
}

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

IMAGES = DATA["query"]["allimages"]

for img in IMAGES:
    print(img["title"])

PHP

<?php
/*
    get_allimages_by_name.php

    MediaWiki-API-Demos
    List all images in the namespace, starting from files that begin with 'Graffiti_000'.
    Begrenzt die erste Antwort nur auf die ersten drei Bildern. 

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "list" => "allimages",
    "aifrom" => "Graffiti_000",
    "ailimit" => "3"
];

$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"]["allimages"] as $k => $v ) {
    echo( $v["title"] . "\n" );
}

JavaScript

/*
    get_allimages_by_name.js

    MediaWiki-API-Demos
    List all images in the namespace, starting from files that begin with 'Graffiti_000'.
    Begrenzt die erste Antwort nur auf die ersten drei Bildern. 

    MIT-Lizenz
*/

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

var params = {
    action: "query",
    format: "json",
    list: "allimages",
    aifrom: "Graffiti_000",
    ailimit: "3"
};

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 images = response.query.allimages;
        for (var img in images) {
            console.log(images[img].title);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

/*
	get_allimages_by_name.js

	MediaWiki-API-Demos
	List all images in the namespace, starting from files that begin with 'Graffiti_000'.
	Begrenzt die erste Antwort nur auf die ersten drei Bildern.

	MIT-Lizenz
*/

var params = {
		action: 'query',
		format: 'json',
		list: 'allimages',
		aifrom: 'Graffiti_000',
		ailimit: '3'
	},
	api = new mw.Api();

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