Jump to content

User:Didicodes/Sandbox/API:Random

From mediawiki.org
MediaWiki version:
1.12

GET Request to get a list of random pages.

API documentation

[edit]

list=random (rn)

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

Get a set of random pages.

Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, Main Page is the first random page in the list, List of fictional monkeys will always be second, List of people on stamps of Vanuatu third, etc.

Specific parameters:
Other general parameters are available.
rnnamespace

Return pages in these namespaces only.

Values (separate with | or alternative): 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 90, 91, 92, 93, 100, 101, 102, 103, 104, 105, 106, 107, 710, 711, 828, 829, 1198, 1199, 1728, 1729, 2600, 5500, 5501
To specify all values, use *.
rnfilterredir

How to filter for redirects.

One of the following values: all, nonredirects, redirects
Default: nonredirects
rnminsize

Limit to pages with at least this many bytes.

Type: integer
rnmaxsize

Limit to pages with at most this many bytes.

Type: integer
rncontentmodel

Filter pages that have the specified content model.

One of the following values: GadgetDefinition, Graph.JsonConfig, Json.JsonConfig, JsonSchema, MassMessageListContent, NewsletterContent, Scribunto, SecurePoll, css, flow-board, javascript, json, sanitized-css, text, translate-messagebundle, unknown, vue, wikitext, worklist
rnredirect
Deprecated.

Use rnfilterredir=redirects instead.

Type: boolean (details)
rnlimit

Limit how many random pages will be returned.

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

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

Examples:
Return two random pages from the main namespace.
api.php?action=query&list=random&rnnamespace=0&rnlimit=2 [open in sandbox]
Return page info about two random pages from the main namespace.
api.php?action=query&generator=random&grnnamespace=0&grnlimit=2&prop=info [open in sandbox]
Return page info about one random page from the main namespace that has at least 500 bytes of text.
api.php?action=query&list=random&rnnamespace=0&rnlimit=1&minsize=500 [open in sandbox]


Examples

[edit]

Example 1: Get two random pages

[edit]

GET Request

[edit]

Response

[edit]
{
    "batchcomplete": "",
    "continue": {
        "rncontinue": "0.613060281177|0.6130607018929999|3737740|0",
        "continue": "-||"
    },
    "query": {
        "random": [
            {
                "id": 22841705,
                "ns": 0,
                "title": "How We Decide"
            },
            {
                "id": 6705281,
                "ns": 0,
                "title": "RRH Portreath"
            }
        ]
    }
}

Sample code

[edit]

get_two_random_pages.py

#!/usr/bin/python3

"""
    get_two_random_pages.py

    MediaWiki Action API Code Samples
    Demo of `Random` module: get two random pages
    MIT license
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "list": "random",
    "rnnamespace": "0",
    "rnlimit": "2"
}

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

print(DATA)

Example 2: Get random pages

[edit]

GET Request

[edit]

Response

[edit]
{
    "batchcomplete": "",
    "continue": {
        "grncontinue": "0.215787713771|0.215787863486|44399457|0",
        "continue": "grncontinue||"
    },
    "query": {
        "pages": {
            "52897381": {
                "pageid": 52897381,
                "ns": 1,
                "title": "Talk:Dicarpellum",
                "revisions": [
                    {
                        "revid": 760466166,
                        "parentid": 0,
                        "user": "Plantdrew",
                        "timestamp": "2017-01-17T04:15:53Z",
                        "comment": "[[WP:AES|\u2190]]Created page with '{{WikiProject Plants|class=stub|importance=low|needs-image=yes}}'"
                    }
                ],
                "images": [
                    {
                        "ns": 6,
                        "title": "File:Camera-photo Upload.svg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Chamomile@original size.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Rose Amber Flush 20070601.jpg"
                    },
                    {
                        "ns": 6,
                        "title": "File:Symbol stub class.svg"
                    }
                ]
            }
        }
    }
}

Sample code

[edit]

get_random_pages.py

#!/usr/bin/python3

"""
    get_random_pages.py

    MediaWiki Action API Code Samples
    Demo of `Random` module: get list of two or more random pages by specifying rnlimit
    MIT license
"""

import requests

S = requests.Session()

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

PARAMS = {
    "action": "query",
    "format": "json",
    "prop": "revisions|images",
    "generator": "random"
}

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

print(DATA)

Parameter history

[edit]
  • v1.14: Introduced rnredirect
  • v1.26: Deprecated rnredirect
  • v1.26: Introduced rnfilterredir