def change_password(self, password: str) -> bool: """ Checking if the new password is valid :param password: password :type password: str :returns: True if password is right. :rtype: bool :exception exceptions.FailedToParseError: when failed """ from xml.etree import ElementTree from requests.auth import HTTPBasicAuth from pymal import exceptions self.__auth_object = HTTPBasicAuth(self.username, password) data = self.auth_connect(self.__AUTH_CHECKER_URL) if data == 'Invalid credentials': self.__auth_object = None self.__password = None return False xml_user = ElementTree.fromstring(data) if 'user' != xml_user.tag: raise exceptions.FailedToParseError('user == {0:s}'.format(xml_user.tag)) l = list(xml_user) xml_username = l[1] if 'username' != xml_username.tag: raise exceptions.FailedToParseError('username == {0:s}'.format(xml_username.tag)) if self.username != xml_username.text.strip(): raise exceptions.FailedToParseError('username = {0:s}'.format(xml_username.text.strip())) xml_id = l[0] if 'id' != xml_id.tag: raise exceptions.FailedToParseError('id == {0:s}'.format(xml_id.tag)) if self.user_id != int(xml_id.text): raise exceptions.FailedToParseError() self.__password = password data_form = self.__DATA_FORM.format(self.username, password).encode('utf-8') headers = { 'content-type': 'application/x-www-form-urlencoded', 'name': 'loginForm', } self.auth_connect(self.__MY_LOGIN_URL, data=data_form, headers=headers) return True
def check_side_content_div(expected_text: str, div_node: bs4.element.Tag): span_node = div_node.span if span_node is None: raise exceptions.FailedToParseError(div_node) expected_text += ":" if ['dark_text'] != span_node['class']: return False return expected_text == span_node.text.strip()
def get_content_wrapper_div(url: str, connection_function) -> bs4.element.Tag: myanimelist_div = __get_myanimelist_div(url, connection_function) # Getting content wrapper <div> content_wrapper_div = myanimelist_div.find( name="div", attrs={"id": "contentWrapper"}, recursive=False) if content_wrapper_div is None: raise exceptions.FailedToParseError(myanimelist_div) return content_wrapper_div
def __parse_friend_div(div_friend): """ :param div_friend: :type div_friend: :exception FailedToParseError :return: :rtype: """ from pymal import account div_pic = div_friend.find(name="div", attrs={'class': 'picSurround'}) if div_pic is None: raise exceptions.FailedToParseError(div_friend) splited_friend_url = div_pic.a['href'].split('/profile/', 1) if 2 != len(splited_friend_url): raise exceptions.FailedToParseError() return account.Account(splited_friend_url[1])
def __get_myanimelist_div(url: str, connection_function) -> bs4.element.Tag: got_robot = False for try_number in range(consts.RETRY_NUMBER): time.sleep(consts.RETRY_SLEEP) data = connection_function(url) html = bs4.BeautifulSoup(data, "html5lib").html if html.head.find(name="meta", attrs={"name": "robots"}) is not None: got_robot = True continue div = html.body.find(name="div", attrs={"id": 'myanimelist'}) if div is not None: return div if got_robot: raise exceptions.GotRobotError() raise exceptions.FailedToParseError("my anime list div wasn't found")
def reload(self): """ :exception FailedToParseError """ div_wrapper = global_functions.get_content_wrapper_div( self.__url, self.account.connect) if div_wrapper is None: raise exceptions.FailedToParseError() list_div_friend = div_wrapper.findAll(name="div", attrs={"class": "friendBlock"}) self.__friends = frozenset( map(self.__parse_friend_div, list_div_friend)) self._is_loaded = True
def make_set(self_set: set, i: int, list_of_tags: list) -> int: """ return the index after the next <br/> and inserting all the link until it. :type self_set: set :param self_set: a list to append links to :type i: int :param i: an index :type list_of_tags: list :param list_of_tags: list of tags to check the index on :rtype: int """ from pymal import anime from pymal import manga n_i = get_next_index(i, list_of_tags) for i in range(i + 1, n_i, 2): if 'a' != list_of_tags[i].name: exceptions.FailedToParseError(list_of_tags[i].name) tag_href = list_of_tags[i]['href'] if '/anime/' in tag_href: obj = anime.Anime splitter = '/anime/' elif '/manga/' in tag_href: obj = manga.Manga splitter = '/manga/' else: print('unknown tag', tag_href) self_set.add( request.urljoin(consts.HOST_NAME, list_of_tags[i]['href'])) continue obj_id = tag_href.split(splitter)[1].split('/')[0] if not obj_id.isdigit(): print('unknown tag', tag_href) continue self_set.add(obj(int(obj_id))) return n_i