Jump to content

API:Imageinfo/Sample code 1

From mediawiki.org

Python

[edit]
#This file is auto-generated. See modules.json and autogenerator.py for details

#!/usr/bin/python3

"""
    get_imageinfo.py

    MediaWiki API Demos
    Demo of `Imageinfo` module: Get information about an image file.

    MIT License
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "prop": "imageinfo",
    "titles": "File:Billy_Tipton.jpg"
}

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

PAGES = DATA["query"]["pages"]

for k, v in PAGES.items():
    print(v["title"] + " is uploaded by User:" + v["imageinfo"][0]["user"])

PHP

[edit]
<?php

//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_imageinfo.php

    MediaWiki API Demos
    Demo of `Imageinfo` module: Get information about an image file.

    MIT License
*/

$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
    "action" => "query",
    "format" => "json",
    "prop" => "imageinfo",
    "titles" => "File:Billy Tipton.jpg"
];

$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 ) {
    echo( $v["title"] . " is uploaded by User:" . $v["imageinfo"][0]["user"] . "\n" );
}

JavaScript

[edit]
//This file is autogenerated. See modules.json and autogenerator.py for details

/*
    get_imageinfo.js

    MediaWiki API Demos
    Demo of `Imageinfo` module: Get information about an image file.

    MIT License
*/

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

var params = {
    action: "query",
    format: "json",
    prop: "imageinfo",
    titles: "File:Billy Tipton.jpg"
};

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 p in pages) {
            console.log(pages[p].title + " is uploaded by User:" + pages[p].imageinfo[0].user);
        }
    })
    .catch(function(error){console.log(error);});

MediaWiki JS

[edit]
// This file is autogenerated. See modules.json and autogenerator.py for details

/*
	get_imageinfo.js

	MediaWiki API Demos
	Demo of `Imageinfo` module: Get information about an image file.

	MIT License
*/

var params = {
		action: 'query',
		format: 'json',
		prop: 'imageinfo',
		titles: 'File:Billy Tipton.jpg'
	},
	api = new mw.Api();

api.get( params ).done( function ( data ) {
	var pages = data.query.pages,
		p;
	for ( p in pages ) {
		console.log( pages[ p ].title + ' is uploaded by User:' + pages[ p ].imageinfo[ 0 ].user );
	}
} );