def save(self, text = u'', summary = u'', minor = False, bot = True): if not self.site.logged_in and self.site.force_login: # Should we really check for this? raise errors.LoginError(self.site) if self.site.blocked: raise errors.UserBlocked(self.site.blocked) if not self.can('edit'): raise errors.ProtectedPageError(self) if not text: text = self.text if not self.site.writeapi: return OldPage.save(self, text = text, summary = summary, minor = False) data = {} if minor: data['minor'] = '1' if not minor: data['notminor'] = '1' if self.last_rev_time: data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time) if self.edit_time: data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S', self.edit_time) if bot: data['bot'] = '1' try: result = self.site.api('edit', title = self.name, text = text, summary = summary, token = self.get_token('edit'), **data) if result['edit'].get('result').lower() == 'failure': raise errors.EditError(self, result['edit']) except errors.APIError, e: if e.code == 'editconflict': raise errors.EditError(self, text, summary, e.info) elif e.code in ('protectedtitle', 'cantcreate', 'cantcreate-anon', 'noimageredirect-anon', 'noimageredirect', 'noedit-anon', 'noedit'): raise errors.ProtectedPageError(self, e.code, e.info) else: raise
def login(self, username = None, password = None, cookies = None): if self.initialized: self.require(1, 10) if username and password: self.credentials = (username, password) if cookies: if self.host not in self.conn.cookies: self.conn.cookies[self.host] = http.CookieJar() self.conn.cookies[self.host].update(cookies) if self.credentials: wait_token = self.wait_token() while True: login = self.api('login', lgname = self.credentials[0], lgpassword = self.credentials[1]) if login['login']['result'] == 'Success': break elif login['login']['result'] == 'Throttled': self.wait(wait_token, login['login'].get('wait', 5)) else: raise errors.LoginError(self, login['login']) if self.initialized: info = self.api('query', meta = 'userinfo', uiprop = 'groups|rights') userinfo = compatibility.userinfo(info, self.require(1, 12, raise_error = False)) self.username = userinfo['name'] self.groups = userinfo.get('groups', []) self.rights = userinfo.get('rights', []) self.tokens = {} else: self.site_init()
def save(self, text=u'', summary=u'', minor=False, bot=True, **kwargs): if not self.site.logged_in and self.site.force_login: # Should we really check for this? raise errors.LoginError(self.site) if self.site.blocked: raise errors.UserBlocked(self.site.blocked) if not self.can('edit'): raise errors.ProtectedPageError(self) if not text: text = self.text if not self.site.writeapi: return OldPage.save(self, text=text, summary=summary, minor=False) data = {} if minor: data['minor'] = '1' if not minor: data['notminor'] = '1' if self.last_rev_time: data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time) if self.edit_time: data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S', self.edit_time) if bot: data['bot'] = '1' data.update(kwargs) def do_edit(): result = self.site.api('edit', title=self.name, text=text, summary=summary, token=self.get_token('edit'), **data) if result['edit'].get('result').lower() == 'failure': raise errors.EditError(self, result['edit']) return result try: result = do_edit() except errors.APIError as e: if e.code == 'badtoken': # Retry, but only once to avoid an infinite loop self.get_token('edit', force=True) try: result = do_edit() except errors.APIError as e: self.handle_edit_error(e, summary) else: self.handle_edit_error(e, summary) if result['edit'] == 'Success': self.last_rev_time = client.parse_timestamp(result['newtimestamp']) return result['edit']
def login(self, username=None, password=None, cookies=None, domain=None): """Login to the wiki.""" if username and password: self.credentials = (username, password, domain) if cookies: if self.host not in self.conn.cookies: self.conn.cookies[self.host] = http.CookieJar() self.conn.cookies[self.host].update(cookies) if self.credentials: wait_token = self.wait_token() kwargs = { 'lgname': self.credentials[0], 'lgpassword': self.credentials[1] } if self.credentials[2]: kwargs['lgdomain'] = self.credentials[2] while True: login = self.api('login', **kwargs) if login['login']['result'] == 'Success': break elif login['login']['result'] == 'NeedToken': kwargs['lgtoken'] = login['login']['token'] elif login['login']['result'] == 'Throttled': self.wait(wait_token, login['login'].get('wait', 5)) else: raise errors.LoginError(self, login['login']) if self.initialized: info = self.api('query', meta='userinfo', uiprop='groups|rights') userinfo = info['query']['userinfo'] self.username = userinfo['name'] self.groups = userinfo.get('groups', []) self.rights = userinfo.get('rights', []) self.tokens = {} else: self.site_init()
def save(self, text, summary=u'', minor=False, bot=True, section=None, **kwargs): """ Update the text of a section or the whole page by performing an edit operation. """ if not self.site.logged_in and self.site.force_login: # Should we really check for this? raise errors.LoginError( self.site, 'By default, mwclient protects you from accidentally ' + 'editing without being logged in. If you actually want to edit without ' 'logging in, you can set force_login on the Site object to False.' ) if self.site.blocked: raise errors.UserBlocked(self.site.blocked) if not self.can('edit'): raise errors.ProtectedPageError(self) if self.section is not None and section is None: warnings.warn( 'From mwclient version 0.8.0, the `save()` method will no longer ' + 'implicitly use the `section` parameter from the last `text()` or ' + '`edit()` call. Please pass the `section` parameter explicitly to ' + 'the save() method to save only a single section.', category=DeprecationWarning, stacklevel=2) section = self.section if not self.site.writeapi: raise errors.NoWriteApi(self) data = {} if minor: data['minor'] = '1' if not minor: data['notminor'] = '1' if self.last_rev_time: data['basetimestamp'] = time.strftime('%Y%m%d%H%M%S', self.last_rev_time) if self.edit_time: data['starttimestamp'] = time.strftime('%Y%m%d%H%M%S', self.edit_time) if bot: data['bot'] = '1' if section: data['section'] = section data.update(kwargs) def do_edit(): result = self.site.api('edit', title=self.name, text=text, summary=summary, token=self.get_token('edit'), **data) if result['edit'].get('result').lower() == 'failure': raise errors.EditError(self, result['edit']) return result try: result = do_edit() except errors.APIError as e: if e.code == 'badtoken': # Retry, but only once to avoid an infinite loop self.get_token('edit', force=True) try: result = do_edit() except errors.APIError as e: self.handle_edit_error(e, summary) else: self.handle_edit_error(e, summary) # 'newtimestamp' is not included if no change was made if 'newtimestamp' in result['edit'].keys(): self.last_rev_time = client.parse_timestamp( result['edit'].get('newtimestamp')) return result['edit']
def __init__( self, optimum_ID=None, password=None, device_ID=None, # TODO get deviceID somehow, test wifi_MAC=None, device_type=None, device_os=None, user_agent=None): """ :param optimum_ID: str :param password: str :param device_ID: str :param wifi_MAC: str :param device_type: str :param device_os: str :param user_agent: str """ self.search_count = 1 if not optimum_ID: raise errors.LoginError("No Optimum ID supplied") else: self.optimum_ID = optimum_ID if wifi_MAC: self.wifi_MAC = wifi_MAC else: self.wifi_MAC = str(uuid.uuid4()) if device_type: self.device_type = device_type else: self.device_type = "mac" if device_os: self.device_os = device_os else: self.device_os = "10.9.5.0" if password: self.password = password else: self.password = getpass.getpass() if not device_ID: if os.path.exists( "deviceID.txt" ): # temp for easy testing w/o hardcoding my Device ID, sorry! with open("deviceID.txt", "r") as tmpfile: self.device_ID = tmpfile.read() else: raise ValueError("No device_ID supplied") else: self.device_ID = device_ID if user_agent: self.user_agent = user_agent else: self.user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.78.2 (KHTML, like Gecko)" # send auth requests payload = { "request_type": "AUTHENTICATE", "username": self.optimum_ID, "password": self.password, "deviceId": self.device_ID, "version": "4.11", "deviceType": self.device_type, "os": self.device_os, "WifiMAC": self.wifi_MAC, "wifiRSSI": "NA" } auth_conn = requests.get(base_url.auth, params=payload) if auth_conn.status_code == 200: auth_xml = minidom.parseString(auth_conn.text) code = auth_xml.getElementsByTagName("Code")[0].childNodes[0].data if code != "0": message = auth_xml.getElementsByTagName( "Message")[0].childNodes[0].data raise errors.LoginError(message) self.AuthToken = str( auth_xml.getElementsByTagName("AuthToken") [0].childNodes[0].data) self.device_ID = str( auth_xml.getElementsByTagName("DeviceId") [0].childNodes[0].data) self.home_ID = str( auth_xml.getElementsByTagName("HomeId")[0].childNodes[0].data) self.hub_ID = str( auth_xml.getElementsByTagName("HubId")[0].childNodes[0].data) self.enhanced_hub_ID = str( auth_xml.getElementsByTagName("EnhancedHubId") [0].childNodes[0].data) self.service_group_ID = str( auth_xml.getElementsByTagName("ServiceGroupId") [0].childNodes[0].data) self.corp = str( auth_xml.getElementsByTagName("Corp") [0].childNodes[0].data) # first n digits of acct number! xml_boxes = auth_xml.getElementsByTagName("Boxes")[0].childNodes self.boxes = {} for box in xml_boxes: box_serial = str(box.childNodes[0].firstChild.data) box_name = str(box.childNodes[1].firstChild.data) box_resolution = str(box.childNodes[2].firstChild.data) box_type = str(box.childNodes[3].firstChild.data) box_space = self.getBoxFreeSpaceBySerial(box_serial) new_box = Box(box_name, box_serial, box_resolution, box_type, box_space) self.boxes[ new_box. name] = new_box # maybe these should be dict instead of objs elif auth_conn.status_code == 500: raise errors.LoginError("Couldn't Login!")