def get_picture(self, tags, rating): """ Get picture with tag and rating :param tags: tags of picture :param rating: picture rating :return: picture url and hash or empty strings :rtype: (str, str) """ logger.info("get_picture()") url = self._url % (tags, rating) logger.debug("Get url: " + str(url)) try: picture_object = requests.get(url).json() except ConnectionError: logger.info("Return empty strings: connection error") return "", "" except JSONDecodeError: logger.info("Picture not found") return "", "" logger.debug("Get picture: " + str(picture_object)) try: logger.info("Try to return picture link and hash") return picture_object[0]['file_url'], picture_object[0]['md5'] except Exception: logger.info("Return empty strings: picture not found") return "", ""
def get_value(element, key, none_allowed=False): method_name = 'get_value' log_text = 'element[{}] - key[{}] - none_allowed[{}]'.format( str(element), str(key), str(none_allowed) ) log_utils.debug(log_text, method_name, __name__) if element is None: raise gen_exception.UndefinedParameterError('element') if key is None: raise gen_exception.UndefinedParameterError('key') try: value = element[key] except KeyError as error: log_text = '{} not defined in {}'.format(str(error), element) log_utils.error(log_text, method_name, __name__) value = None if value is None and not none_allowed: raise gen_exception.MissingValueError(key) log_text = 'value[{}]'.format(str(value)) log_utils.debug(log_text, method_name, __name__) return value
def get_text(element, constants=None, none_allowed=False): method_name = 'get_text' log_text = 'element[{}] - constants[{}] - none_allowed[{}]'.format( str(element), str(constants), str(none_allowed) ) log_utils.debug(log_text, method_name, __name__) text = get_value(element, generic_const.TEXT, constants, none_allowed) log_text = 'value[{}]'.format(str(text)) log_utils.debug(log_text, method_name, __name__) return text
def _get_managed_element(element_type): method_name = "_get_managed_element" log_text = "element_type[{}]".format(str(element_type)) log_utils.debug(log_text, method_name, __name__) try: managed_element = dict_utils.get_value(MANAGED_ELEMENTS, element_type) except generic_exceptions.MissingValueError as mve: log_utils.error(mve, method_name, __name__) raise generic_exceptions.UnrecognizedElementTypeError(element_type) except generic_exceptions.UndefinedParameterError as upe: log_utils.error(upe, method_name, __name__) raise generic_exceptions.UnrecognizedElementTypeError(element_type) return managed_element
def get_method(manager, method_name): try: custom_method_list = manager.CUSTOM_METHODS method = dict_utils.get_value(custom_method_list, method_name, True) except AttributeError as error: logged_method_name = 'get_method' log_text = 'exception[{}]'.format( str(error) ) log_utils.debug(log_text, logged_method_name, __name__) method = None if method is None: method = get_default_method(method_name) return method
def add_choices(choice_list, choices): try: choice = dict_utils.get_value(choices,0) base_manager.get_type(choice) for choice in choices: choice_list.append(choice) except gen_exception.MissingValueError: choice_list.append(choices) except IndexError as error: method_name = 'add_choices' log_text = 'exception[{}]'.format( str(error) ) log_utils.debug(log_text, method_name, __name__) return choice_list
def _download_picture(self, url): """ Download picture as file from url :param url: url for downloading :raise: PictureNotFoundException if error occurring while downloading :rtype: None """ try: raw_picture = requests.get(url) picture = open( "pictures" + os.path.sep + json_handler.constants['default_picture_file'], 'wb') for chunk in raw_picture.iter_content(chunk_size=512 * 1024): if chunk: picture.write(chunk) picture.close() except Exception as e: logger.debug("Error download with exception: " + str(e)) raise DownloadErrorException()
def get_picture(self, tags, rating, from_user): """ Get picture :param tags: picture tags :param rating: picture rating :param from_user: type of chat message from :raise: PictureNotFoundException if picture not found :raise: TagsNotFoundException if request picture without tags :raise: EcchiDeniedException if request ecchi in non private chat :raise: HentaiDeniedException if request hentai on non private chat :rtype: None """ logger.info("PictureGrabber get_picture()") if (not from_user) and rating != "safe": if rating == "questionable": logger.info( "Request ecchi in non private chat. Denied. Current rating: '" + str(rating) + "'") raise EcchiDeniedException else: logger.info( "Request hentai on non private chat. Denied. Current rating: '" + str(rating) + "'") raise HentaiDeniedException logger.debug("Join tags list by '+'") if isinstance(tags, list): if len(tags) != 0: tags = '+'.join(tags) logger.info("Tags in request: '" + str(tags) + "'") else: logger.info( "Not found tags in request. Raise TagsNotFoundException") raise TagsNotFoundException url, picture_hash = self._kon.get_picture(tags, rating) logger.debug("Get url '" + str(url) + "' and hash '" + str(picture_hash) + "'") if picture_hash != "": logger.info("Found picture in Konachan. Returning") self._download_picture(url) else: logger.info("Not found picture in Konachan. Continue") url, picture_hash = self._ya.get_picture(tags, rating) logger.debug("Get url '" + str(url) + "' and hash '" + str(picture_hash) + "'") if picture_hash != "": logger.info("Found picture in Yandere. Returning") self._download_picture(url) else: logger.info( "Not found picture in Yandere. Raise PictureNotFoundException" ) raise PictureNotFoundException()