API:Categorymembers/Sample code 2
Appearance
Python
[edit]#!/usr/bin/python3
"""
get_recent_category_items.py
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
"""
import requests
S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php"
PARAMS = {
"cmdir": "desc",
"format": "json",
"list": "categorymembers",
"action": "query",
"cmtitle": "Category:Physics",
"cmsort": "timestamp"
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()
PAGES = DATA["query"]["categorymembers"]
for page in PAGES:
print(page["title"])
PHP
[edit]<?php
/*
get_recent_category_items.php
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
$endPoint = "https://en.wikipedia.org/w/api.php";
$params = [
"action" => "query",
"list" => "categorymembers",
"cmtitle" => "Category:Physics",
"cmsort" => "timestamp",
"cmdir" => "desc",
"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"]["categorymembers"] as $page ) {
echo( $page["title"] . "\n" );
}
JavaScript
[edit]/*
get_recent_category_items.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
var url = "https://en.wikipedia.org/w/api.php";
var params = {
action: "query",
list: "categorymembers",
cmtitle: "Category:Physics",
cmsort: "timestamp",
cmdir: "desc",
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.categorymembers;
for (var page in pages) {
console.log(pages[page].title);
}
})
.catch(function(error){console.log(error);});
MediaWiki JS
[edit]/*
get_recent_category_items.js
MediaWiki API Demos
Demo of `Categorymembers` module : Get the ten articles most recently added to a category
MIT License
*/
var params = {
action: 'query',
list: 'categorymembers',
cmtitle: 'Category:Physics',
cmsort: 'timestamp',
cmdir: 'desc',
format: 'json'
},
api = new mw.Api();
api.get( params ).done( function ( data ) {
var pages = data.query.categorymembers,
page;
for ( page in pages ) {
console.log( pages[ page ].title );
}
} );