Exemple #1
0
def soi():
    '''
    Displays Slack-formatted most-upvoted answer of the most upvoted
    question from the query.

    soi ~ StackOverflow-Inline

    Example:
        /soi python list comprehension
    '''
    # Get objects on the application context pushed on startup
    so = flask._app_ctx_stack.so

    text = request.values.get('text')

    # Perform google search
    sr = google_search_stackoverflow_query(text, pages=1)
    # Extract each question nid from results
    so_qnids = []
    for result in sr:
        qnid = extract_question_nid_from_url(result.link)
        if qnid is not None:
            so_qnids.append(qnid)
        else:
            logging.warning("Found invalid URL in search: "
                            "{}".format(result.link))
    if not so_qnids:
        return "*No questions for the given query were found! :(*"
    # Fetch Questions using SO API
    so_qs = so.questions(so_qnids)
    # Sort Questions by score
    so_qs = sorted(so_qs, key=lambda q: q.score, reverse=True)

    top_question = so_qs[0]
    answer_body = get_question_top_answer_body(so=so, question=top_question)
    question_str = get_response_string(top_question)

    resp_list = [question_str]
    if answer_body is None:
        resp_list.append("\n\n*The top question for your query is unanswered! "
                         ":(*")
    else:
        resp_list.append("\n\n")
        resp_list.append(html2slack(answer_body))

    return Response("".join(resp_list),
                    content_type='text/plain; charset=utf-8')
def test__html2slack():
    html = """
    <pre><code>foobar</code></pre>
    """
    slack = html2slack(html)
    assert slack.strip() == "```\n\n    foobar\n```"