Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def login(self, username, password=False, remember=False, force=False, verify=True, domain=None):
- """Login to the site
- remember - saves cookies to a file - the filename will be:
- hash(username - apibase).cookies
- the cookies will be saved in the current directory, change cookiepath
- to use a different location
- force - forces login over the API even if a cookie file exists
- and overwrites an existing cookie file if remember is True
- verify - Checks cookie validity with isLoggedIn()
- domain - domain name, required for some auth systems like LDAP
- """
- if not force:
- try:
- cookiefile = self.cookiepath + str(hash(username+' - '+self.apibase))+'.cookies'
- self.cookies.load(self, cookiefile, True, True)
- self.username = username
- if not verify or self.isLoggedIn(self.username):
- return True
- except:
- pass
- if not password:
- from getpass import getpass
- password = getpass("Wiki password for "+username+": ")
- def loginerror(info):
- try:
- print info['login']['result']
- except:
- print info['error']['code']
- print info['error']['info']
- return False
- data = {
- "action" : "login",
- "lgname" : username,
- "lgpassword" : password,
- }
- if domain is not None:
- data["lgdomain"] = domain
- if self.maxlag < 120:
- data['maxlag'] = 120
- req = api.APIRequest(self, data)
- info = req.query()
- if info['login']['result'] == "Success":
- self.username = username
- elif info['login']['result'] == "NeedToken":
- req.changeParam('lgtoken', info['login']['token'])
- info = req.query()
- if info['login']['result'] == "Success":
- self.username = username
- else:
- return loginerror(info)
- else:
- return loginerror(info)
- if not self.siteinfo:
- self.setSiteinfo()
- params = {
- 'action': 'query',
- 'meta': 'userinfo',
- 'uiprop': 'rights',
- }
- if self.maxlag < 120:
- params['maxlag'] = 120
- req = api.APIRequest(self, params)
- info = req.query(False)
- user_rights = info['query']['userinfo']['rights']
- if 'apihighlimits' in user_rights:
- self.limit = 5000
- if remember:
- cookiefile = self.cookiepath + str(hash(self.username+' - '+self.apibase))+'.cookies'
- self.cookies.save(self, cookiefile, True, True)
- if self.useragent == "python-wikitools/%s" % VERSION:
- self.useragent = "python-wikitools/%s (User:%s)" % (VERSION, self.username)
- return True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement