@@ -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 | Permission is hereby granted, free of charge, to any person obtaining a copy | ||||
of this software and associated documentation files (the "Software"), to deal | of this software and associated documentation files (the "Software"), to deal | ||||
@@ -3,7 +3,7 @@ mwparserfromhell | |||||
.. image:: https://img.shields.io/travis/earwig/mwparserfromhell/develop.svg | .. image:: https://img.shields.io/travis/earwig/mwparserfromhell/develop.svg | ||||
:alt: Build Status | :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 | .. image:: https://img.shields.io/coveralls/earwig/mwparserfromhell/develop.svg | ||||
:alt: Coverage Status | :alt: Coverage Status | ||||
@@ -177,36 +177,44 @@ If you're using Pywikibot_, your code might look like this: | |||||
text = page.get() | text = page.get() | ||||
return mwparserfromhell.parse(text) | 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 | .. code-block:: python | ||||
import json | |||||
from urllib.parse import urlencode | |||||
from urllib.request import urlopen | |||||
import requests | |||||
import mwparserfromhell | import mwparserfromhell | ||||
API_URL = "https://en.wikipedia.org/w/api.php" | API_URL = "https://en.wikipedia.org/w/api.php" | ||||
def parse(title): | 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] | revision = res["query"]["pages"][0]["revisions"][0] | ||||
text = revision["slots"]["main"]["content"] | text = revision["slots"]["main"]["content"] | ||||
return mwparserfromhell.parse(text) | 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 | .. _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 | .. _Word-ending links: https://www.mediawiki.org/wiki/Help:Links#linktrail | ||||
.. _EarwigBot: https://github.com/earwig/earwigbot | .. _EarwigBot: https://github.com/earwig/earwigbot | ||||
.. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot | .. _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 |
@@ -42,7 +42,7 @@ master_doc = 'index' | |||||
# General information about the project. | # General information about the project. | ||||
project = u'mwparserfromhell' | 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 | # The version info for the project you're documenting, acts as replacement for | ||||
# |version| and |release|, also used in various other places throughout the | # |version| and |release|, also used in various other places throughout the | ||||
@@ -8,10 +8,10 @@ wikicode. It supports Python 2 and Python 3. | |||||
Developed by Earwig_ with contributions from `Σ`_, Legoktm_, and others. | Developed by Earwig_ with contributions from `Σ`_, Legoktm_, and others. | ||||
Development occurs on GitHub_. | 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 | .. _GitHub: https://github.com/earwig/mwparserfromhell | ||||
Installation | Installation | ||||
@@ -30,8 +30,8 @@ Alternatively, get the latest development version:: | |||||
You can run the comprehensive unit testing suite with | You can run the comprehensive unit testing suite with | ||||
``python setup.py test -q``. | ``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 | Contents | ||||
-------- | -------- | ||||
@@ -7,7 +7,7 @@ Integration | |||||
:func:`mwparserfromhell.parse() <mwparserfromhell.__init__.parse>` on | :func:`mwparserfromhell.parse() <mwparserfromhell.__init__.parse>` on | ||||
:meth:`~earwigbot.wiki.page.Page.get`. | :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 mwparserfromhell | ||||
import pywikibot | import pywikibot | ||||
@@ -18,23 +18,33 @@ If you're using Pywikibot_, your code might look like this:: | |||||
text = page.get() | text = page.get() | ||||
return mwparserfromhell.parse(text) | 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 | import mwparserfromhell | ||||
API_URL = "https://en.wikipedia.org/w/api.php" | API_URL = "https://en.wikipedia.org/w/api.php" | ||||
def parse(title): | 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) | return mwparserfromhell.parse(text) | ||||
.. _EarwigBot: https://github.com/earwig/earwigbot | .. _EarwigBot: https://github.com/earwig/earwigbot | ||||
.. _Pywikibot: https://www.mediawiki.org/wiki/Manual:Pywikibot | .. _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 |
@@ -23,7 +23,7 @@ | |||||
""" | """ | ||||
`mwparserfromhell <https://github.com/earwig/mwparserfromhell>`_ (the MediaWiki | `mwparserfromhell <https://github.com/earwig/mwparserfromhell>`_ (the MediaWiki | ||||
Parser from Hell) is a Python package that provides an easy-to-use and | 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" | __author__ = "Ben Kurtovic" | ||||
@@ -114,9 +114,16 @@ class TestDocs(unittest.TestCase): | |||||
url1 = "https://en.wikipedia.org/w/api.php" | url1 = "https://en.wikipedia.org/w/api.php" | ||||
url2 = "https://en.wikipedia.org/w/index.php?title={0}&action=raw" | url2 = "https://en.wikipedia.org/w/index.php?title={0}&action=raw" | ||||
title = "Test" | 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: | try: | ||||
raw = urlopen(url1, urlencode(data).encode("utf8")).read() | raw = urlopen(url1, urlencode(data).encode("utf8")).read() | ||||
except IOError: | except IOError: | ||||