mwapi


Namemwapi JSON
Version 0.6.1 PyPI version JSON
download
home_pagehttps://github.com/mediawiki-utilities/python-mwapi
SummarySimple wrapper for the Mediawiki API
upload_time2022-07-26 16:00:49
maintainer
docs_urlhttps://pythonhosted.org/mwapi/
authorAaron Halfaker
requires_python
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI
coveralls test coverage No coveralls.
            # MediaWiki API

This MIT-licensed library provides a very simple convenience wrapper 
around the [MediaWiki API](https://www.mediawiki.org/wiki/API), 
including support for authenticated sessions. It requires Python 3
and that your wiki is using MediaWiki 1.15.3 or greater.

* **Installation:** ``pip install mwapi``
* **Documentation:** https://pythonhosted.org/mwapi
* **Repository:** https://github.com/mediawiki-utilities/python-mwapi
* **License:** MIT

## Examples

### Single query

    >>> import mwapi
    >>>
    >>> session = mwapi.Session('https://en.wikipedia.org')
    >>>
    >>> print(session.get(action='query', meta='userinfo'))
    {'query': {'userinfo': {'anon': '', 'name': '75.72.203.28', 'id': 0}},
     'batchcomplete': ''}
    >>>
    >>> print(session.get(action='query', prop='revisions', revids=32423425))
    {'query': {'pages': {'1429626': {'ns': 0, 'revisions': [{'user':
     'Wknight94', 'parentid': 32276615, 'comment':
     '/* References */ Removing less-specific cat', 'revid': 32423425,
     'timestamp': '2005-12-23T00:07:17Z'}], 'title': 'Grigol Ordzhonikidze',
     'pageid': 1429626}}}, 'batchcomplete': ''}

### Query with continuation

```python
import mwapi
from mwapi.errors import APIError

session = mwapi.Session('https://en.wikipedia.org/')

# If passed a `continuation` parameter, returns an iterable over a continued query.
# On each iteration, a new request is made for the next portion of the results.
continued = session.get(
    formatversion=2,
    action='query',
    generator='categorymembers',
    gcmtitle='Category:17th-century classical composers',
    gcmlimit=100,  # 100 results per request
    continuation=True)

pages = []
try:
    for portion in continued:
        if 'query' in portion:
            for page in portion['query']['pages']:
                pages.append(page['title'])
        else:
            print("MediaWiki returned empty result batch.")
except APIError as error:
    raise ValueError(
        "MediaWiki returned an error:", str(error)
    )

print("Fetched {} pages".format(len(pages)))
```

### Asynchronous single query

```python
import asyncio
import aiohttp
import mwapi

async def query():
    async with aiohttp.ClientSession() as s:
        session = mwapi.AsyncSession(
                    'https://en.wikipedia.org',
                    user_agent='mwapi async demo',
                    session=s)
        response = await asyncio.create_task(
            session.get(action='query', prop='revisions', revids=32423425)
        )
    print(response)

asyncio.run(query())
```    

### Asynchronous query with continuation

```python
import asyncio
import aiohttp

import mwapi
from mwapi.errors import APIError

async def query():
    async with aiohttp.ClientSession() as s:
        session = mwapi.AsyncSession(
                    'https://en.wikipedia.org',
                    user_agent='mwapi async demo',
                    session=s)

        continued = await asyncio.create_task(
            session.get(
                formatversion=2,
                action='query',
                generator='categorymembers',
                gcmtitle='Category:17th-century classical composers',
                gcmlimit=100,  # 100 results per request
                continuation=True)
        )
        pages = []
        try:
            async for portion in continued:
                if 'query' in portion:
                    for page in portion['query']['pages']:
                        pages.append(page['title'])
                else:
                    print("MediaWiki returned empty result batch.")
        except APIError as error:
            raise ValueError(
                "MediaWiki returned an error:", str(error)
            )
    print("Fetched {} pages".format(len(pages)))

asyncio.run(query())
```

## Authors
* YuviPanda -- https://github.com/yuvipanda
* Aaron Halfaker -- https://github.com/halfak



            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/mediawiki-utilities/python-mwapi",
    "name": "mwapi",
    "maintainer": "",
    "docs_url": "https://pythonhosted.org/mwapi/",
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "",
    "author": "Aaron Halfaker",
    "author_email": "aaron.halfaker@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/9f/2f/2dca9cba4f080be2d09bcc8fbc7215eba6f49c2b967c407e0d3f7ac0ae05/mwapi-0.6.1.tar.gz",
    "platform": null,
    "description": "# MediaWiki API\n\nThis MIT-licensed library provides a very simple convenience wrapper \naround the [MediaWiki API](https://www.mediawiki.org/wiki/API), \nincluding support for authenticated sessions. It requires Python 3\nand that your wiki is using MediaWiki 1.15.3 or greater.\n\n* **Installation:** ``pip install mwapi``\n* **Documentation:** https://pythonhosted.org/mwapi\n* **Repository:** https://github.com/mediawiki-utilities/python-mwapi\n* **License:** MIT\n\n## Examples\n\n### Single query\n\n    >>> import mwapi\n    >>>\n    >>> session = mwapi.Session('https://en.wikipedia.org')\n    >>>\n    >>> print(session.get(action='query', meta='userinfo'))\n    {'query': {'userinfo': {'anon': '', 'name': '75.72.203.28', 'id': 0}},\n     'batchcomplete': ''}\n    >>>\n    >>> print(session.get(action='query', prop='revisions', revids=32423425))\n    {'query': {'pages': {'1429626': {'ns': 0, 'revisions': [{'user':\n     'Wknight94', 'parentid': 32276615, 'comment':\n     '/* References */ Removing less-specific cat', 'revid': 32423425,\n     'timestamp': '2005-12-23T00:07:17Z'}], 'title': 'Grigol Ordzhonikidze',\n     'pageid': 1429626}}}, 'batchcomplete': ''}\n\n### Query with continuation\n\n```python\nimport mwapi\nfrom mwapi.errors import APIError\n\nsession = mwapi.Session('https://en.wikipedia.org/')\n\n# If passed a `continuation` parameter, returns an iterable over a continued query.\n# On each iteration, a new request is made for the next portion of the results.\ncontinued = session.get(\n    formatversion=2,\n    action='query',\n    generator='categorymembers',\n    gcmtitle='Category:17th-century classical composers',\n    gcmlimit=100,  # 100 results per request\n    continuation=True)\n\npages = []\ntry:\n    for portion in continued:\n        if 'query' in portion:\n            for page in portion['query']['pages']:\n                pages.append(page['title'])\n        else:\n            print(\"MediaWiki returned empty result batch.\")\nexcept APIError as error:\n    raise ValueError(\n        \"MediaWiki returned an error:\", str(error)\n    )\n\nprint(\"Fetched {} pages\".format(len(pages)))\n```\n\n### Asynchronous single query\n\n```python\nimport asyncio\nimport aiohttp\nimport mwapi\n\nasync def query():\n    async with aiohttp.ClientSession() as s:\n        session = mwapi.AsyncSession(\n                    'https://en.wikipedia.org',\n                    user_agent='mwapi async demo',\n                    session=s)\n        response = await asyncio.create_task(\n            session.get(action='query', prop='revisions', revids=32423425)\n        )\n    print(response)\n\nasyncio.run(query())\n```    \n\n### Asynchronous query with continuation\n\n```python\nimport asyncio\nimport aiohttp\n\nimport mwapi\nfrom mwapi.errors import APIError\n\nasync def query():\n    async with aiohttp.ClientSession() as s:\n        session = mwapi.AsyncSession(\n                    'https://en.wikipedia.org',\n                    user_agent='mwapi async demo',\n                    session=s)\n\n        continued = await asyncio.create_task(\n            session.get(\n                formatversion=2,\n                action='query',\n                generator='categorymembers',\n                gcmtitle='Category:17th-century classical composers',\n                gcmlimit=100,  # 100 results per request\n                continuation=True)\n        )\n        pages = []\n        try:\n            async for portion in continued:\n                if 'query' in portion:\n                    for page in portion['query']['pages']:\n                        pages.append(page['title'])\n                else:\n                    print(\"MediaWiki returned empty result batch.\")\n        except APIError as error:\n            raise ValueError(\n                \"MediaWiki returned an error:\", str(error)\n            )\n    print(\"Fetched {} pages\".format(len(pages)))\n\nasyncio.run(query())\n```\n\n## Authors\n* YuviPanda -- https://github.com/yuvipanda\n* Aaron Halfaker -- https://github.com/halfak\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple wrapper for the Mediawiki API",
    "version": "0.6.1",
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "md5": "dfebed7ab2f36c39e23b4eb514861c2c",
                "sha256": "bf38a712915405953f4618c879dd6562f668464b7138df7951a4ac17ffc07173"
            },
            "downloads": -1,
            "filename": "mwapi-0.6.1-py2.py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "dfebed7ab2f36c39e23b4eb514861c2c",
            "packagetype": "bdist_wheel",
            "python_version": "py2.py3",
            "requires_python": null,
            "size": 12204,
            "upload_time": "2022-07-26T16:00:46",
            "upload_time_iso_8601": "2022-07-26T16:00:46.230745Z",
            "url": "https://files.pythonhosted.org/packages/43/98/9b7cc0be4f664233d2ea420ac62fbf8a1b5f3c1bca067e795723b401fa5b/mwapi-0.6.1-py2.py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "md5": "7613e2518201f104ff28aec49e9a2b75",
                "sha256": "bc8917d675b78860b085d85bc9883e96ae66f80d42a3d214742ff46c32bd38fe"
            },
            "downloads": -1,
            "filename": "mwapi-0.6.1.tar.gz",
            "has_sig": false,
            "md5_digest": "7613e2518201f104ff28aec49e9a2b75",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 10580,
            "upload_time": "2022-07-26T16:00:49",
            "upload_time_iso_8601": "2022-07-26T16:00:49.116955Z",
            "url": "https://files.pythonhosted.org/packages/9f/2f/2dca9cba4f080be2d09bcc8fbc7215eba6f49c2b967c407e0d3f7ac0ae05/mwapi-0.6.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-07-26 16:00:49",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "github_user": "mediawiki-utilities",
    "github_project": "python-mwapi",
    "travis_ci": true,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "mwapi"
}
        
Elapsed time: 0.10617s