Browse Source

Switch to requests for basic API example (closes #219); update links

tags/v0.6
Ben Kurtovic 5 years ago
parent
commit
b6e4c59004
7 changed files with 68 additions and 43 deletions
  1. +1
    -1
      LICENSE
  2. +27
    -19
      README.rst
  3. +1
    -1
      docs/conf.py
  4. +6
    -6
      docs/index.rst
  5. +22
    -12
      docs/integration.rst
  6. +1
    -1
      mwparserfromhell/__init__.py
  7. +10
    -3
      tests/test_docs.py

+ 1
- 1
LICENSE View File

@@ -1,4 +1,4 @@
Copyright (C) 2012-2018 Ben Kurtovic <ben.kurtovic@gmail.com>
Copyright (C) 2012-2019 Ben Kurtovic <ben.kurtovic@gmail.com>

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal


+ 27
- 19
README.rst View File

@@ -3,7 +3,7 @@ mwparserfromhell

.. image:: https://img.shields.io/travis/earwig/mwparserfromhell/develop.svg
:alt: Build Status
:target: http://travis-ci.org/earwig/mwparserfromhell
:target: https://travis-ci.org/earwig/mwparserfromhell

.. image:: https://img.shields.io/coveralls/earwig/mwparserfromhell/develop.svg
:alt: Coverage Status
@@ -177,36 +177,44 @@ If you're using Pywikibot_, your code might look like this:
text = page.get()
return mwparserfromhell.parse(text)

If you're not using a library, you can parse any page using the following
Python 3 code (via the API_):
If you're not using a library, you can parse any page with the following
Python 3 code (using the API_ and the requests_ library):

.. code-block:: python

import json
from urllib.parse import urlencode
from urllib.request import urlopen
import requests
import mwparserfromhell

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

def parse(title):
data = {"action": "query", "prop": "revisions", "rvprop": "content",
"rvslots": "main", "rvlimit": 1, "titles": title,
"format": "json", "formatversion": "2"}
raw = urlopen(API_URL, urlencode(data).encode()).read()
res = json.loads(raw)
params = {
"action": "query",
"prop": "revisions",
"rvprop": "content",
"rvslots": "main",
"rvlimit": 1,
"titles": title,
"format": "json",
"formatversion": "2",
}
headers = {"User-Agent": "My-Bot-Name/1.0"}
req = requests.get(API_URL, headers=headers, params=params)
res = req.json()
revision = res["query"]["pages"][0]["revisions"][0]
text = revision["slots"]["main"]["content"]
return mwparserfromhell.parse(text)

.. _MediaWiki: http://mediawiki.org
.. _ReadTheDocs: http://mwparserfromhell.readthedocs.io
.. _Earwig: http://en.wikipedia.org/wiki/User:The_Earwig
.. _Σ: http://en.wikipedia.org/wiki/User:%CE%A3
.. _Legoktm: http://en.wikipedia.org/wiki/User:Legoktm
.. _MediaWiki: https://www.mediawiki.org
.. _ReadTheDocs: https://mwparserfromhell.readthedocs.io
.. _Earwig: https://en.wikipedia.org/wiki/User:The_Earwig
.. _Σ: https://en.wikipedia.org/wiki/User:%CE%A3
.. _Legoktm: https://en.wikipedia.org/wiki/User:Legoktm
.. _GitHub: https://github.com/earwig/mwparserfromhell
.. _Python Package Index: http://pypi.python.org
.. _get pip: http://pypi.python.org/pypi/pip
.. _Python Package Index: https://pypi.org/
.. _get pip: https://pypi.org/project/pip/
.. _Word-ending links: https://www.mediawiki.org/wiki/Help:Links#linktrail
.. _EarwigBot: https://github.com/earwig/earwigbot
.. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot
.. _API: http://mediawiki.org/wiki/API
.. _API: https://www.mediawiki.org/wiki/API:Main_page
.. _requests: https://2.python-requests.org

+ 1
- 1
docs/conf.py View File

@@ -42,7 +42,7 @@ master_doc = 'index'

# General information about the project.
project = u'mwparserfromhell'
copyright = u'2012–2018 Ben Kurtovic'
copyright = u'2012–2019 Ben Kurtovic'

# The version info for the project you're documenting, acts as replacement for
# |version| and |release|, also used in various other places throughout the


+ 6
- 6
docs/index.rst View File

@@ -8,10 +8,10 @@ wikicode. It supports Python 2 and Python 3.
Developed by Earwig_ with contributions from `Σ`_, Legoktm_, and others.
Development occurs on GitHub_.

.. _MediaWiki: http://mediawiki.org
.. _Earwig: http://en.wikipedia.org/wiki/User:The_Earwig
.. _Σ: http://en.wikipedia.org/wiki/User:%CE%A3
.. _Legoktm: http://en.wikipedia.org/wiki/User:Legoktm
.. _MediaWiki: https://www.mediawiki.org
.. _Earwig: https://en.wikipedia.org/wiki/User:The_Earwig
.. _Σ: https://en.wikipedia.org/wiki/User:%CE%A3
.. _Legoktm: https://en.wikipedia.org/wiki/User:Legoktm
.. _GitHub: https://github.com/earwig/mwparserfromhell

Installation
@@ -30,8 +30,8 @@ Alternatively, get the latest development version::
You can run the comprehensive unit testing suite with
``python setup.py test -q``.

.. _Python Package Index: http://pypi.python.org
.. _get pip: http://pypi.python.org/pypi/pip
.. _Python Package Index: https://pypi.org/
.. _get pip: https://pypi.org/project/pip/

Contents
--------


+ 22
- 12
docs/integration.rst View File

@@ -7,7 +7,7 @@ Integration
:func:`mwparserfromhell.parse() <mwparserfromhell.__init__.parse>` on
:meth:`~earwigbot.wiki.page.Page.get`.

If you're using Pywikibot_, your code might look like this::
If you're using Pywikibot_, your code might look like this:

import mwparserfromhell
import pywikibot
@@ -18,23 +18,33 @@ If you're using Pywikibot_, your code might look like this::
text = page.get()
return mwparserfromhell.parse(text)

If you're not using a library, you can parse any page using the following code
(via the API_)::
If you're not using a library, you can parse any page with the following
Python 3 code (using the API_ and the requests_ library):

import json
from urllib.parse import urlencode
from urllib.request import urlopen
import requests
import mwparserfromhell

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

def parse(title):
data = {"action": "query", "prop": "revisions", "rvlimit": 1,
"rvprop": "content", "format": "json", "titles": title}
raw = urlopen(API_URL, urlencode(data).encode()).read()
res = json.loads(raw)
text = list(res["query"]["pages"].values())[0]["revisions"][0]["*"]
params = {
"action": "query",
"prop": "revisions",
"rvprop": "content",
"rvslots": "main",
"rvlimit": 1,
"titles": title,
"format": "json",
"formatversion": "2",
}
headers = {"User-Agent": "My-Bot-Name/1.0"}
req = requests.get(API_URL, headers=headers, params=params)
res = req.json()
revision = res["query"]["pages"][0]["revisions"][0]
text = revision["slots"]["main"]["content"]
return mwparserfromhell.parse(text)

.. _EarwigBot: https://github.com/earwig/earwigbot
.. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot
.. _API: http://mediawiki.org/wiki/API
.. _API: https://www.mediawiki.org/wiki/API:Main_page
.. _requests: https://2.python-requests.org

+ 1
- 1
mwparserfromhell/__init__.py View File

@@ -23,7 +23,7 @@
"""
`mwparserfromhell <https://github.com/earwig/mwparserfromhell>`_ (the MediaWiki
Parser from Hell) is a Python package that provides an easy-to-use and
outrageously powerful parser for `MediaWiki <http://mediawiki.org>`_ wikicode.
outrageously powerful parser for `MediaWiki <https://www.mediawiki.org>`_ wikicode.
"""

__author__ = "Ben Kurtovic"


+ 10
- 3
tests/test_docs.py View File

@@ -114,9 +114,16 @@ class TestDocs(unittest.TestCase):
url1 = "https://en.wikipedia.org/w/api.php"
url2 = "https://en.wikipedia.org/w/index.php?title={0}&action=raw"
title = "Test"
data = {"action": "query", "prop": "revisions", "rvprop": "content",
"rvslots": "main", "rvlimit": 1, "titles": title,
"format": "json", "formatversion": "2"}
data = {
"action": "query",
"prop": "revisions",
"rvprop": "content",
"rvslots": "main",
"rvlimit": 1,
"titles": title,
"format": "json",
"formatversion": "2",
}
try:
raw = urlopen(url1, urlencode(data).encode("utf8")).read()
except IOError:


Loading…
Cancel
Save