Skip to content

Commit

Permalink
add ability to login on initialization and when setting api url (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Apr 22, 2020
1 parent e9c44d7 commit 928ab97
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 18 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,14 @@
# MediaWiki Changelog

## Version 0.6.4

* Add ability to login during initialization [issue #79](https://github.com/barrust/mediawiki/issues/79)

## Version 0.6.3

* Capture timeout exception
* bs4 does not support `hasattr` but uses `*.has_attr()`

## Version 0.6.2

* Add `allpages` functionaloty [PR #75](https://github.com/barrust/mediawiki/pull/75)
Expand Down
44 changes: 31 additions & 13 deletions mediawiki/mediawiki.py
Expand Up @@ -22,7 +22,7 @@
from .utilities import memoize

URL = "https://github.com/barrust/mediawiki"
VERSION = "0.6.3"
VERSION = "0.6.4"


class MediaWiki(object):
Expand All @@ -42,7 +42,9 @@ class MediaWiki(object):
requests; defults to a library version but per \
the MediaWiki API documentation it recommends \
setting a unique one and not using the \
library's default user-agent string """
library's default user-agent string
username (str): The username to use to log into the MediaWiki
password (str): The password to use to log into the MediaWiki """

def __init__(
self,
Expand All @@ -53,6 +55,8 @@ def __init__(
rate_limit_wait=timedelta(milliseconds=50),
cat_prefix="Category",
user_agent=None,
username=None,
password=None,
):
""" Init Function """
self._version = VERSION
Expand Down Expand Up @@ -83,11 +87,14 @@ def __init__(
self._refresh_interval = None
self._use_cache = True

# call helper functions to get everything set up
self._reset_session()

# for login information
self._is_logged_in = False
if password is not None and username is not None:
self.login(username, password)

# call helper functions to get everything set up
self._reset_session()
try:
self._get_site_info()
except MediaWikiException:
Expand Down Expand Up @@ -232,7 +239,10 @@ def user_agent(self):

@user_agent.setter
def user_agent(self, user_agent):
""" Set the new user agent string """
""" Set the new user agent string
Note: Will need to re-log into the MediaWiki if user agent string \
is changed """
self._user_agent = user_agent
self._reset_session()

Expand Down Expand Up @@ -314,20 +324,32 @@ def login(self, username, password, strict=True):
return False

# non-properties
def set_api_url(self, api_url="https://{lang}.wikipedia.org/w/api.php", lang="en"):
def set_api_url(
self,
api_url="https://{lang}.wikipedia.org/w/api.php",
lang="en",
username=None,
password=None,
):
""" Set the API URL and language
Args:
api_url (str): API URL to use
lang (str): Language of the API URL
username (str): The username, if needed, to log into the MediaWiki site
password (str): The password, if needed, to log into the MediaWiki site
Raises:
:py:func:`mediawiki.exceptions.MediaWikiAPIURLError`: if the \
url is not a valid MediaWiki site """
url is not a valid MediaWiki site or login fails """
old_api_url = self._api_url
old_lang = self._lang
self._lang = lang.lower()
self._api_url = api_url.format(lang=self._lang)

self._is_logged_in = False
try:
if username is not None and password is not None:
self.login(username, password)
self._get_site_info()
self.__supported_languages = None # reset this
except (requests.exceptions.ConnectTimeout, MediaWikiException):
Expand Down Expand Up @@ -389,7 +411,7 @@ def random(self, pages=1):
return titles

@memoize
def allpages(self, query='', results=10):
def allpages(self, query="", results=10):
""" Request all pages from mediawiki instance
Args:
Expand All @@ -398,11 +420,7 @@ def allpages(self, query='', results=10):
Returns:
list: The pages that meet the search query
"""
query_params = {
"list": "allpages",
"aplimit": results,
"apfrom": query,
}
query_params = {"list": "allpages", "aplimit": results, "apfrom": query}

request = self.wiki_request(query_params)

Expand Down
7 changes: 2 additions & 5 deletions mediawiki/mediawikipage.py
Expand Up @@ -362,15 +362,12 @@ def langlinks(self):
https://www.mediawiki.org/wiki/API:Langlinks """

if self._langlinks is None:
params = {
'prop': 'langlinks',
'cllimit': 'max',
}
params = {"prop": "langlinks", "cllimit": "max"}
query_result = self._continued_query(params)

langlinks = dict()
for lang_info in query_result:
langlinks[lang_info['lang']] = lang_info['*']
langlinks[lang_info["lang"]] = lang_info["*"]
self._langlinks = langlinks
return self._langlinks

Expand Down

0 comments on commit 928ab97

Please sign in to comment.