Advertisement
Guest User

Untitled

a guest
Nov 7th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.38 KB | None | 0 0
  1. def login(self, username, password=False, remember=False, force=False, verify=True, domain=None):
  2.         """Login to the site
  3.        
  4.         remember - saves cookies to a file - the filename will be:
  5.         hash(username - apibase).cookies
  6.         the cookies will be saved in the current directory, change cookiepath
  7.         to use a different location
  8.         force - forces login over the API even if a cookie file exists
  9.         and overwrites an existing cookie file if remember is True
  10.         verify - Checks cookie validity with isLoggedIn()
  11.         domain - domain name, required for some auth systems like LDAP
  12.        
  13.         """
  14.         if not force:
  15.             try:   
  16.                 cookiefile = self.cookiepath + str(hash(username+' - '+self.apibase))+'.cookies'
  17.                 self.cookies.load(self, cookiefile, True, True)
  18.                 self.username = username
  19.                 if not verify or self.isLoggedIn(self.username):
  20.                     return True
  21.             except:
  22.                 pass
  23.         if not password:
  24.             from getpass import getpass
  25.             password = getpass("Wiki password for "+username+": ")
  26.         def loginerror(info):
  27.             try:
  28.                 print info['login']['result']
  29.             except:
  30.                 print info['error']['code']
  31.                 print info['error']['info']
  32.             return False
  33.         data = {
  34.             "action" : "login",
  35.             "lgname" : username,
  36.             "lgpassword" : password,
  37.         }
  38.         if domain is not None:
  39.             data["lgdomain"] = domain
  40.         if self.maxlag < 120:
  41.             data['maxlag'] = 120
  42.         req = api.APIRequest(self, data)
  43.         info = req.query()
  44.         if info['login']['result'] == "Success":
  45.             self.username = username
  46.         elif info['login']['result'] == "NeedToken":
  47.             req.changeParam('lgtoken', info['login']['token'])
  48.             info = req.query()
  49.             if info['login']['result'] == "Success":
  50.                 self.username = username
  51.             else:
  52.                 return loginerror(info)
  53.         else:
  54.             return loginerror(info)
  55.         if not self.siteinfo:
  56.             self.setSiteinfo()
  57.         params = {
  58.             'action': 'query',
  59.             'meta': 'userinfo',
  60.             'uiprop': 'rights',
  61.         }
  62.         if self.maxlag < 120:
  63.             params['maxlag'] = 120
  64.         req = api.APIRequest(self, params)
  65.         info = req.query(False)
  66.         user_rights = info['query']['userinfo']['rights']
  67.         if 'apihighlimits' in user_rights:
  68.             self.limit = 5000
  69.         if remember:
  70.             cookiefile = self.cookiepath + str(hash(self.username+' - '+self.apibase))+'.cookies'
  71.             self.cookies.save(self, cookiefile, True, True)
  72.         if self.useragent == "python-wikitools/%s" % VERSION:
  73.             self.useragent = "python-wikitools/%s (User:%s)" % (VERSION, self.username)
  74.         return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement