Sign in
Log inSign up
Stop distracting your self each time you search for an answer AND BE MORE PRODUCTIVE!

Stop distracting your self each time you search for an answer AND BE MORE PRODUCTIVE!

Benny Elgazar's photo
Benny Elgazar
·Jan 27, 2019

Intro

As a developer, we search each day for answers to common problems during a code writing. StackOverflow is our best friend while seeking answers. The problem begins when we start looking at other things and the question we were seeking is just a window to check our whatssup, personal mail and our favorite news site.

Well, this kind of behavior cut a respectable timeshare and the distraction interrupt the overall development.

We can be more productive in no times, I knew I can improve my productivity I just had to think how.

Solution

I open vs.code and started to wrap up a nice CLI using python I knew for a fact that python has numerous libraries for query URL, parsing URL content, and display results interactively.

That's when I created stackfast.

Fency logo in 3...2...1..

    ________    _____             ______      ________                _____ 
    __  ___/    __  /______ _________  /__    ___  __/  ______ _________  /_
    _____ \     _  __/  __ `/  ___/_  //_/    __  /_    _  __ `/_  ___/  __/
    ____/ /     / /_ / /_/ // /__ _  ,<       _  __/    / /_/ /_(__  )/ /_  
    /____/      \__/ \__,_/ \___/ /_/|_|      /_/       \__,_/ /____/ \__/  
   // Ask stackoverflow fast.. ♫                                        v0.1  ( We need some kind of version ¯\_(ツ)_/¯ )

Some code - without it why we are even talking?

def display_selected_post_discussion(question_data: Dict[str, str]) -> None:
    # get's the question_data dict parse and display the post discussion.
    # // input: question_data: {"url": "", "question": ""}

    print()
    ColorPrint.print_fail("QUESTION: [" + question_data["question"] + "]")
    question_full_input = requests.get(URL + question_data["url"])
    data = BeautifulSoup(question_full_input.text, "html.parser")
    print(data.find(name="div", attrs={"class": "post-text"}).text)
    ColorPrint.print_pass("ANSWERS: ")
    ColorPrint.print_warn("=======================================")
    for idx, answer in enumerate(data.find_all(name="div", attrs={"class": "post-text"})[1:]):
        print(f"Answer #{idx + 1}\n{answer.text.strip()}")
        ColorPrint.print_warn("=======================================")


def dig_top_matched_simillar_questions(question: str) -> List[Dict[str, str]]:
    # dig stackoverflow page by the user input question and find the most relevant 
    # questions.
    # // input: question.
    # // return: list of dicts e.g [{url: "xxx", question: "zzz"}]

    data = requests.get(URL + "/search?tab=relevance&q=" + question)
    soup = BeautifulSoup(data.text, 'html.parser')
    questions = []
    for item in soup.find_all(name="div", class_="result-link"):
        try:
            url = (
                str(item).split("href=")[1]
                         .split(" title")[0]
                         .strip()
                         .replace('"', "")
                         .replace("?r=SearchResults", "")
            )
            question = item.text.strip().replace("Q: ", "").replace("A:", "").strip()
            questions.append(
                {"url": url, "question": question}
            )
        except:
            pass
    return questions

The 2 important functions:


dig_top_matched_simillar_questions - get the relevant posts according to your question and parse & display it nicely.

display_selected_post_discussion - display the disscussion. question & answers.

Demo

demo

Repository on GitHub HERE

Comments are Welcome!

Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct