Ejemplo n.º 1
0
    def functionality(self, command, action):
        functionality = get_functionality(url=self.navigation.url)
        num_choices = 5
        text_response = "Wrong input."

        if command == "show":
            self.dict_contexts["functionality"].index = update_cursor_index(
                action,
                old_idx=self.dict_contexts["functionality"].index,
                step=num_choices,
                size=len(functionality))
            idx_start = self.dict_contexts["functionality"].index
            # TODO
            text_response = get_links_text_response(links=functionality,
                                                    idx_start=idx_start,
                                                    num_choices=num_choices)
        elif command == "open":
            link_url = functionality[self.dict_contexts["functionality"].number
                                     - 1]
            # If the link is valid, update the cursor and visit the page.
            if link_url is not None:
                self.navigation.url = link_url[1]
                text_response = self.visit_page()

        return text_response
Ejemplo n.º 2
0
    def main_text(self, action):
        if action == "openLink":
            # Get URL to visit from the DB.
            link_url = db_get_text_link(
                page_url=self.navigation.url,
                link_num=self.dict_contexts["maintext"].number)

            # If the link is valid, update the cursor and visit the page.
            if link_url is not None:
                self.navigation.url = link_url[0]
                return self.visit_page()
            else:
                return "Wrong input."
        else:
            # Update cursor.
            self.dict_contexts["maintext"].index = update_cursor_index(
                action,
                old_idx=self.dict_contexts["maintext"].index,
                step=2,
                size=10000)
            try:
                # Get sentences from the main text to be shown to the user.
                text_response = get_main_text_sentences(
                    url=self.navigation.url,
                    idx_sentence=self.dict_contexts["maintext"].index,
                    n_sentences=2)
            except IndexError:
                text_response = "No more sentences to read, you have reached the end of the page."
                # Reset cursor position.
                self.dict_contexts["maintext"].index = 0
            except FileNotFoundError:
                text_response = "Sorry, this page is still in the process of being analysed. Try again later!"

        return text_response
Ejemplo n.º 3
0
    def menu(self, command, action):
        # Extract the menu from the crawl results.
        menu = get_menu(self.navigation.url)
        text_response = "Wrong input."
        if command == "show":
            # Update cursor.
            self.dict_contexts["menu"].index = update_cursor_index(
                action=action,
                old_idx=self.dict_contexts["menu"].index,
                step=10,
                size=len(menu))
            # Number of choices that will get displayed to the user at once.
            num_choices = 10
            strings = [item[1] for item in menu]
            # Get text response containing voices to show.
            text_response = show_element(
                strings=strings,
                idx_start=self.dict_contexts["menu"].index,
                num_choices=num_choices)
        elif command == "open":
            # Get all the URLs of the menu links.
            menu_anchors = [tup[2] for tup in menu]
            try:
                # Get URL to visit.
                new_url = menu_anchors[self.dict_contexts["menu"].number - 1]
                # Update cursor and visit the page.
                self.navigation.url = new_url
                text_response = self.visit_page()
            except ValueError:
                pass

        return text_response
Ejemplo n.º 4
0
    def bookmarks(self, command, action):
        text_response = "Command not recognized."
        bookmarks = get_bookmarks(user="******")

        if command == "show":
            if action == "open":
                try:
                    index = self.dict_contexts["bookmarks"].number - 1
                    self.navigation.url = bookmarks[index][0]
                    text_response = self.visit_page()
                except IndexError:
                    text_response = "Wrong input."
            elif action == "delete":
                url = bookmarks[self.dict_contexts["bookmarks"].index]
                text_response = delete_bookmark(url=url, user="******")
            else:
                # Number of choices that will get displayed to the user at once.
                num_choices = 5
                # Update cursor.
                self.dict_contexts["bookmarks"].index = update_cursor_index(
                    action=action,
                    old_idx=self.dict_contexts["bookmarks"].index,
                    step=num_choices,
                    size=len(bookmarks))
                # Get text response containing voices to show.
                text_response = show_element(
                    element=bookmarks,
                    idx_start=self.dict_contexts["bookmarks"].index,
                    num_choices=num_choices)
        elif command == "add":
            name = self.dict_contexts["bookmarks"].name
            text_response = insert_bookmark(url=self.navigation.url, name=name)

        return text_response
Ejemplo n.º 5
0
def get_links_text_response(url, links_type, action, idx_start, num_choices):
    links = []
    if links_type == "all":
        links = read_links(url=url)
    elif links_type == "article":
        links = read_links_article(url=url)

    idx_start = update_cursor_index(action, old_idx=idx_start, step=num_choices, size=len(links))

    # Get the options that will be shown to the user in the text response.
    strings = [tup[0] for tup in links]

    if len(links) > 0:
        # Format of display -> option n: text
        #                      option n+1: text
        text_response = show_element(strings, idx_start, num_choices)
    else:
        text_response = "No links available at the moment."

    return text_response, idx_start
Ejemplo n.º 6
0
    def google_search(self, action):
        try:
            query_results = get_urls_from_google(
                query=self.dict_contexts["googlesearch"].query)
        except Exception:
            error_message = "Error while searching on Google."
            print(red(error_message))
            return error_message

        # Update cursor.
        self.dict_contexts["googlesearch"].index = update_cursor_index(
            action=action,
            old_idx=self.dict_contexts["googlesearch"].index,
            step=1,
            size=5)

        result = query_results[self.dict_contexts["googlesearch"].index]
        text_response = f"""Result number {self.dict_contexts["googlesearch"].index + 1}.
                                Do you want to visit the page: {result[0]} at {get_domain(result[1])}?"""
        self.navigation.url = result[1]

        return text_response