Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import requests, json
- def mediawiki_login(botuser,botpass,api_url):
- """
- Authenticate and login to a parsed mediawiki.
- Pass the login user, password and the api endpoint.
- """
- # Start a session to store login cookies etc
- session = requests.Session()
- # See cookies
- # print(session.cookies.get_dict())
- # Get login token
- r = session.get(api_url,params={"action":"query","meta":"tokens","format":"json","type":"login"})
- r.raise_for_status()
- logintoken = r.json()['query']['tokens']['logintoken']
- # Login with our login token
- r = session.post(api_url,params={"format":"json","action":"login","lgname":botuser},data={"lgpassword":botpass,"lgtoken":logintoken})
- r.raise_for_status()
- return session
- ## Example of how to use this function.
- def main():
- """
- Test the mediawiki_login() function.
- """
- username = example
- password = ex_pass
- mw_connection = mediawiki_login(username,password)
- # Example request to get a list of pages in a category.
- r = mw_connection.get(api_url,params={"format":"json","action":"query","list":"categorymembers","cmtitle":"Category:Example_Category"})
- print(json.dumps(r.json(), indent=4))
- if __name__ == '__main__':
- main()
Add Comment
Please, Sign In to add comment