Example #1
0
def socli(query):
    """
    SoCLI Code
    :param query: Query to search on Stack Overflow.
    If google_search is true uses Google search to find the best result.
    Else, use Stack Overflow default search mechanism.
    :return:
    """
    query = printer.urlencode(query)
    try:
        if search.google_search:
            questions = search.get_questions_for_query_google(query)
            res_url = questions[0][2]  # Gets the first result
            display_results(res_url)
        else:
            questions = search.get_questions_for_query(query)
            res_url = questions[0][2]
            display_results(search.so_url + res_url)  # Returned URL is relative to SO homepage
    except UnicodeEncodeError as e:
        printer.showerror(e)
        printer.print_warning("\n\nEncoding error: Use \"chcp 65001\" command before using socli...")
        sys.exit(0)
    except requests.exceptions.ConnectionError:
        printer.print_fail("Please check your internet connectivity...")
    except Exception as e:
        printer.showerror(e)
        sys.exit(0)
Example #2
0
def socli(query):
    """
    SoCLI Code
    :param query: Query to search on Stack Overflow.
    If search_enging == 'google' is true uses Google search to find the best result.
    Else, use Stack Overflow default search mechanism.
    :return:
    """
    query = printer.urlencode(query)
    try:
        if search.search_engine == 'google':
            questions = search.get_questions_for_query_google(query)
            res_url = questions[0][2]  # Gets the first result
            display_results(res_url)
        elif search.search_engine == 'ddg':
            questions = search.get_questions_for_query_duckduckgo(query)
            res_url = questions[0][2]
            display_results(search.so_url +
                            res_url)  # Returned URL is relative to SO homepage
        else:
            questions = search.get_questions_for_query(query)
            res_url = questions[0][2]
            display_results(search.so_url +
                            res_url)  # Returned URL is relative to SO homepage

    except IndexError:
        if len(questions) > 1:
            for i in range(1, len(questions)):
                res_url = questions[i][2]
                try:
                    display_results(res_url)
                except IndexError:
                    continue
                break
        printer.print_warning("No results found...")

    except UnicodeEncodeError as e:
        printer.showerror(e)
        printer.print_warning(
            "\n\nEncoding error: Use \"chcp 65001\" command before using socli..."
        )
        sys.exit(0)
    except requests.exceptions.ConnectionError:
        printer.print_fail("Please check your internet connectivity...")
    except Exception as e:
        printer.showerror(e)
        sys.exit(0)
Example #3
0
def socli_browse_interactive(query_tag):
    """
    Interactive mode
    :return:
    """
    if sys.platform == 'win32':
        return socli_browse_interactive_windows(query_tag)

    class SelectQuestionPage(urwid.WidgetWrap):

        def display_text(self, index, question):
            question_text, question_desc, _ = question
            text = [
                ("warning", u"{}. {}\n".format(index, question_text)),
                question_desc + "\n",
            ]
            return text

        def __init__(self, questions):
            self.questions = questions
            self.cachedQuestions = [None for _ in range(10)]
            widgets = [self.display_text(i, q) for i, q in enumerate(questions)]
            self.questions_box = tui.ScrollableTextBox(widgets)
            self.header = tui.UnicodeText(('less-important', 'Select a question below:\n'))
            self.footerText = '0-' + str(len(self.questions) - 1) + ': select a question, any other key: exit.'
            self.errorText = tui.UnicodeText.to_unicode('Question numbers range from 0-' +
                                                        str(len(self.questions) - 1) +
                                                        ". Please select a valid question number.")
            self.footer = tui.UnicodeText(self.footerText)
            self.footerText = tui.UnicodeText.to_unicode(self.footerText)
            frame = urwid.Frame(header=self.header,
                                body=urwid.Filler(self.questions_box, height=('relative', 100), valign='top'),
                                footer=self.footer)
            urwid.WidgetWrap.__init__(self, frame)

        # Override parent method
        def selectable(self):
            return True

        def keypress(self, size, key):
            if key in '0123456789':
                try:
                    question_url = self.questions[int(key)][2]
                    self.footer.set_text(self.footerText)
                    self.select_question(question_url, int(key))
                except IndexError:
                    self.footer.set_text(self.errorText)
            elif key in {'down', 'up'}:
                self.questions_box.keypress(size, key)
            else:
                raise urwid.ExitMainLoop()

        def select_question(self, url, index):
            if self.cachedQuestions[index] is not None:
                tui.question_post = self.cachedQuestions[index]
                tui.MAIN_LOOP.widget = tui.question_post
            else:
                if not search.google_search:
                    url = search.so_url + url
                question_title, question_desc, question_stats, answers = search.get_question_stats_and_answer(url)
                question_post = tui.QuestionPage((answers, question_title, question_desc, question_stats, url))
                self.cachedQuestions[index] = question_post
                tui.MAIN_LOOP.widget = question_post

    tui.display_header = tui.Header()

    try:
        if search.google_search:
            questions = search.get_questions_for_query_google(query)
        else:
            # print('hurr')
            questions = search.get_questions_for_query(query_tag)
            # print(questions)

        question_page = SelectQuestionPage(questions)

        tui.MAIN_LOOP = tui.EditedMainLoop(question_page, printer.palette)
        tui.MAIN_LOOP.run()

    except UnicodeEncodeError:
        printer.print_warning("\n\nEncoding error: Use \"chcp 65001\" command before using socli...")
        sys.exit(0)
    except requests.exceptions.ConnectionError:
        printer.print_fail("Please check your internet connectivity...")
    except Exception as e:
        printer.showerror(e)
        # print("Hurra")
        print("exiting...")
        sys.exit(0)
Example #4
0
 def test06_searchSO(self):
     try:
         _search.get_questions_for_query(self.squery)
     except Exception:
         raise SoCLITestingException("Search Stack Overflow test failed.")