示例#1
0
def get_html(search_url):
    """Opens a search_url and returns a response object text
    """
    try:
        result = requests.get(search_url)
        result.raise_for_status()
        return result.text
    except (requests.RequestException, ValueError) as e:
        print("Connection error!")
        logging.exception(f"Error {e}")
        return False
def add_new_user(session, user_list):
    """This function adds a new user to current session.
    :param current session object
    :param user_list: list of user names needed to be added, uploaded from data.py
    :return: session object
    """
    for index, data in enumerate(user_list):
        if isinstance(data, str) and data:
            new_user = DatabaseModels.User(user_name=data)
            session.add(new_user)
        else:
            logging.exception(f"Пользователь {index} {data} is not added")
    db_commit(session)
    return session
def add_new_post_category(session, post_category_list):
    """This function adds a new post category to current session.
    :param session: current session object
    :param post_category_list: list of categories needed to be added, uploaded from data.py
    :return: session object
    """
    for index, data in enumerate(post_category_list):
        if isinstance(data, str) and data:
            new_category = DatabaseModels.PostsCategory(category_name=data)
            session.add(new_category)
        else:
            logging.exception(f"Категория {index} {data} is not added")
    db_commit(session)
    return session
def add_new_tags(session, tag_list):
    """This function adds a new tag to current session.
    :param session: current session object
    :param tag_list: list of tags needed to be added, uploaded from data.py
    :return: session object
    """
    for index, data in enumerate(tag_list):
        if isinstance(data, str) and data:
            new_tag = DatabaseModels.Tags(tag_name=data)
            session.add(new_tag)
        else:
            logging.exception(f"Тег {index} {data} is not added")
    db_commit(session)
    return session
示例#5
0
def input_is_recursive():
    """Ask for user choice for recursive choice, checks for blank input. In case of incorrect input data this func
    will continuously ask for input.
    @return: True for recursive search and False for not-recursive
    """
    is_search_recursive = None
    while is_search_recursive == None:
        is_search_recursive = input(
            "Please type 1 if you want to use recursive search and 0 if not:\n"
        ).strip()
        is_search_recursive = checking_for_blank_input(is_search_recursive)
        try:
            is_search_recursive = bool(int(is_search_recursive))
        except ValueError:
            logging.exception(
                f"ValueError in is_search_recursive. User has entered {is_search_recursive}"
            )
            print("Please enter a number! 1 - for recursive and 0 - for not")
            is_search_recursive = None
    return is_search_recursive
示例#6
0
def input_qt_url_to_find():
    """Ask for user choice for quantity of search results. Checks for blank input or if user input 0. In case of
    incorrect input data this func will continuously ask for input.
    @return: total_url_qt as int
    """
    total_url_qt = 0
    while not total_url_qt:
        try:
            total_url_qt = int(
                input(
                    "How many search results do you want? Please type in a integer from 10 up to "
                    "50:\n").strip())
            if total_url_qt <= 0:
                print(
                    "You have entered a 0 or negative number. \n Please try again"
                )
                total_url_qt = 0
        except ValueError:
            logging.exception(f"ValueError in input_qt_url_to_find.")
            print("Please enter a integer")
    return total_url_qt
示例#7
0

def print_results(posts_found, username, tags_qt):
    """This function prints the search results in console
    :param my_query: list of results found get_user_posts
    :param username: string, User, whose posts you want to get
    :param tags_qt: int, min qt of tags post should have to be selected
    """
    successful_message = f"У пользователя {username} найдено {len(posts_found)} постов с {tags_qt} тегами и больше \n" \
                         f"Список постов и кол-во тегов:"
    zero_result_found_message = f"Постов с {tags_qt} тегами у пользователя {username} не найдено."
    message_to_print = successful_message if len(posts_found) > 0 else zero_result_found_message
    print(message_to_print)
    for result in posts_found:
        print(result)


if __name__ == "__main__":
    session = create_db_connection()
    username = "******"
    tags_qt = 5
    try:
        posts_found = get_user_posts(session, username, tags_qt)
        print_results(posts_found, username, tags_qt)
    except TypeError:
        logging.exception("TypeError")
        print("Ничего не найдено")
    except OperationalError:
        logging.exception("OperationalError")
        print(f"БД сйчас недоступна")