Beispiel #1
0
def display_results(url, dup_link=None, json_output=False):
    """
    Display result page
    :param url: URL of the search result
    :param dup_link: URL to the duplicate question visited from
    :param json_output: JSON output flag
    :return:
    """
    search.random_headers()
    res_page = requests.get(url, headers=search.header)
    search.captcha_check(res_page.url)
    question_title, question_desc, question_stats, answers, comments, dup_url = \
        search.get_question_stats_and_answer_and_comments(url)
    if json_output:
        sys.stdout.write(
            urllib.parse.unquote(
                json.dumps({
                    'title': question_title,
                    'desc': question_desc,
                    'stats': question_stats,
                    'answers': answers,
                    'comments': comments,
                    'dup_url': dup_url,
                })))
    else:
        tui.display_header = tui.Header()
        tui.question_post = tui.QuestionPage(
            (url, question_title, question_desc, question_stats, answers,
             comments, dup_url, dup_link))
        tui.MAIN_LOOP = tui.EditedMainLoop(tui.question_post, palette)
        tui.MAIN_LOOP.run()
Beispiel #2
0
def display_results(url):
    """
    Display result page
    :param url: URL of the search result
    :return:
    """
    search.random_headers()
    res_page = requests.get(url, headers=search.header)
    search.captcha_check(res_page.url)
    tui.display_header = tui.Header()
    question_title, question_desc, question_stats, answers = search.get_question_stats_and_answer(url)
    tui.question_post = tui.QuestionPage((answers, question_title, question_desc, question_stats, url))
    tui.MAIN_LOOP = tui.EditedMainLoop(tui.question_post, palette)
    tui.MAIN_LOOP.run()
Beispiel #3
0
def socli_browse_interactive_windows(query_tag):
    """
    Interactive mode for -b browse
    :param query_tag:
    :return:
    """
    try:
        search_res = requests.get(search.so_burl + query_tag)
        search.captcha_check(search_res.url)
        soup = BeautifulSoup(search_res.text, 'html.parser')
        try:
            soup.find_all("div", class_="question-summary")[0]  # For explicitly raising exception
            tmp = (soup.find_all("div", class_="question-summary"))
            i = 0
            question_local_url = []
            print(printer.bold("\nSelect a question below:\n"))
            while i < len(tmp):
                if i == 10:
                    break  # limiting results
                question_text = ' '.join((tmp[i].a.get_text()).split())
                question_text = question_text.replace("Q: ", "")
                printer.print_warning(str(i + 1) + ". " + printer.display_str(question_text))
                q_tag = (soup.find_all("div", class_="question-summary"))[i]
                answers = [s.get_text() for s in q_tag.find_all("a", class_="post-tag")][0:]
                ques_tags = " ".join(str(x) for x in answers)
                question_local_url.append(tmp[i].a.get("href"))
                print("  " + printer.display_str(ques_tags) + "\n")
                i = i + 1
            try:
                op = int(printer.inputs("\nType the option no to continue or any other key to exit:"))
                while 1:
                    if (op > 0) and (op <= i):
                        display_results(search.so_burl + question_local_url[op - 1])
                        cnt = 1  # this is because the 1st post is the question itself
                        while 1:
                            global tmpsoup
                            qna = printer.inputs(
                                "Type " + printer.bold("o") + " to open in browser, " + printer.bold(
                                    "n") + " to next answer, " + printer.bold(
                                    "b") + " for previous answer or any other key to exit:")
                            if qna in ["n", "N"]:
                                try:
                                    answer = (tmpsoup.find_all("div", class_="js-post-body")[cnt + 1].get_text())
                                    printer.print_green("\n\nAnswer:\n")
                                    print("-------\n" + answer + "\n-------\n")
                                    cnt = cnt + 1
                                except IndexError:
                                    printer.print_warning(" No more answers found for this question. Exiting...")
                                    sys.exit(0)
                                continue
                            elif qna in ["b", "B"]:
                                if cnt == 1:
                                    printer.print_warning(" You cant go further back. You are on the first answer!")
                                    continue
                                answer = (tmpsoup.find_all("div", class_="js-post-body")[cnt - 1].get_text())
                                printer.print_green("\n\nAnswer:\n")
                                print("-------\n" + answer + "\n-------\n")
                                cnt = cnt - 1
                                continue
                            elif qna in ["o", "O"]:
                                import webbrowser
                                printer.print_warning("Opening in your browser...")
                                webbrowser.open(search.so_burl + question_local_url[op - 1])
                            else:
                                break
                        sys.exit(0)
                    else:
                        op = int(input("\n\nWrong option. select the option no to continue:"))
            except Exception as e:
                printer.showerror(e)
                printer.print_warning("\n Exiting...")
                sys.exit(0)
        except IndexError:
            printer.print_warning("No results found...")
            sys.exit(0)

    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)
        sys.exit(0)