Guest User

Untitled

a guest
Sep 19th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import requests, json
  3.  
  4. def mediawiki_login(botuser,botpass,api_url):
  5. """
  6. Authenticate and login to a parsed mediawiki.
  7. Pass the login user, password and the api endpoint.
  8. """
  9. # Start a session to store login cookies etc
  10. session = requests.Session()
  11. # See cookies
  12. # print(session.cookies.get_dict())
  13.  
  14. # Get login token
  15. r = session.get(api_url,params={"action":"query","meta":"tokens","format":"json","type":"login"})
  16. r.raise_for_status()
  17. logintoken = r.json()['query']['tokens']['logintoken']
  18.  
  19. # Login with our login token
  20. r = session.post(api_url,params={"format":"json","action":"login","lgname":botuser},data={"lgpassword":botpass,"lgtoken":logintoken})
  21. r.raise_for_status()
  22.  
  23. return session
  24.  
  25. ## Example of how to use this function.
  26. def main():
  27. """
  28. Test the mediawiki_login() function.
  29. """
  30. username = example
  31. password = ex_pass
  32.  
  33. mw_connection = mediawiki_login(username,password)
  34.  
  35. # Example request to get a list of pages in a category.
  36. r = mw_connection.get(api_url,params={"format":"json","action":"query","list":"categorymembers","cmtitle":"Category:Example_Category"})
  37.  
  38. print(json.dumps(r.json(), indent=4))
  39.  
  40. if __name__ == '__main__':
  41. main()
Add Comment
Please, Sign In to add comment