Пример #1
0
def joke(bot_input, bot_output):
    # These code snippets use an open-source library. http://unirest.io/python
    # joke = web.get_json_with_headers(
    #     "https://webknox-jokes.p.mashape.com/jokes/random?maxLength=100",
    #     headers={
    #         "X-Mashape-Key": "LM6TApHaSPmshsDRI2S9h2W5eYvNp1Iin3Vjsn2ZncHbuAbsHt",
    #     "Accept": "application/json"
    #     })

    url = "https://webknox-jokes.p.mashape.com/jokes/search?&keywords=" + bot_input.input_string + "&minRating=5&numJokes=1"
    headers={
        "X-Mashape-Key": "LM6TApHaSPmshsDRI2S9h2W5eYvNp1Iin3Vjsn2ZncHbuAbsHt",
        "Accept": "application/json"
      }
    story = web.get_json_with_headers(url, headers)
    if story and len(story):
        bot_output.say(story[0]["joke"])
    else:
        bot_output.say("Couldn't find a joke or you're out of free jokes for the day (limit 5)")
Пример #2
0
def new_question(category=None):
    url = 'https://privnio-trivia.p.mashape.com/exec?category=' + bot_input.input_string + '&method=getQuestions&v=1'
    headers={
        "X-Mashape-Key": "LM6TApHaSPmshsDRI2S9h2W5eYvNp1Iin3Vjsn2ZncHbuAbsHt",
        "Accept": "application/json"
    }

    result = web.get_json_with_headers(url, headers)
    if not result or not result["success"]:
        bot_output.say("Couldn't get the questions.  Maybe you're over your quota?")
        return


    questionPage = web.get_raw('http://www.quinterest.org/php/studyresults.php?amount=1&categ=All&difficulty=MS&tournamentyear=All')

    current_trivia['question'] = re.search("<p><em>Question:</em>(.*)</p>").groups()[0]
    current_trivia['category'] = re.search("(<b>.*</b>){4}<b>(.*)</b><p>").groups()[1]
    current_trivia['answer'] = re.search("<em><strong>Answer:</strong></em>(.*)</div>").groups()[0]
    current_trivia['guess'] = 0
    current_trivia['multiplier'] = 1