コード例 #1
0
ファイル: frinkiac.py プロジェクト: billybobmac/terribot
def run(msg):
    """ Returns an image from the frinkiac search engine. """
    image = tempfile.NamedTemporaryFile(delete=False, suffix='.png')
    message = re.match('^(simpsons\:) (.*)', msg['text'], re.IGNORECASE)
    searchterm = message.group(2)
    searchurl = "https://frinkiac.com/api/search?q=" + percent_encode(searchterm)

    # Return first result...might want to make this configurable.
    searchresult = requests.get(searchurl).json()[0]
    print("SEARCHRESULT: ", searchresult)
    imageurl = "https://frinkiac.com/meme/" + searchresult['Episode'] + "/" + str(searchresult['Timestamp']) + ".jpg"

    print("Downloading & sending image: ", imageurl)
    response = requests.get(imageurl)
    image.write(response.content)
    image.close()
    print('')

    # Initialize the first return value tuple with the image itself - we'll add the subtitles after
    results = ({'action': 'send_photo', 'payload': image.name},)

    # Grab the captions and append them to the tuple
    captionurl = "https://frinkiac.com/api/caption?e=" + searchresult['Episode'] + "&t=" + str(searchresult['Timestamp'])
    captions = requests.get(captionurl)
    for subtitle in captions.json()['Subtitles']:
        results = results + ({'action': 'send_msg', 'payload': subtitle['Content']},)

    # Return our image and the associated captions
    return results
コード例 #2
0
def test_can_percent_encode_list():
    encoded = percent_encode(["knife&fork", "spoon"])
    assert encoded == "knife%26fork&spoon"
コード例 #3
0
def test_can_percent_encode_string():
    encoded = percent_encode("foo")
    assert encoded == "foo"
コード例 #4
0
def test_can_percent_encode_number():
    encoded = percent_encode(12)
    assert encoded == "12"
コード例 #5
0
def test_can_percent_encode_empty_string():
    encoded = percent_encode("")
    assert encoded == ""
コード例 #6
0
def test_can_percent_encode_none():
    encoded = percent_encode(None)
    assert encoded is None
コード例 #7
0
def test_can_percent_encode_none():
    encoded = percent_encode(None)
    assert encoded is None
コード例 #8
0
def test_can_percent_encode_extended_chars():
    encoded = percent_encode("/El Niño/")
    assert encoded == "%2FEl%20Ni%C3%B1o%2F"
コード例 #9
0
def test_can_percent_encode_reserved_chars():
    encoded = percent_encode("20% of $100 = $20")
    assert encoded == "20%25%20of%20%24100%20%3D%20%2420"
コード例 #10
0
def test_can_percent_encode_dictionary():
    encoded = percent_encode(OrderedDict([("one", 1), ("two", 2)]))
    assert encoded == "one=1&two=2"
コード例 #11
0
def test_can_percent_encode_list():
    encoded = percent_encode(["knife&fork", "spoon"])
    assert encoded == "knife%26fork&spoon"
コード例 #12
0
def test_can_percent_encode_string():
    encoded = percent_encode("foo")
    assert encoded == "foo"
コード例 #13
0
def test_can_percent_encode_number():
    encoded = percent_encode(12)
    assert encoded == "12"
コード例 #14
0
def test_can_percent_encode_empty_string():
    encoded = percent_encode("")
    assert encoded == ""
コード例 #15
0
def test_can_percent_encode_dictionary():
    encoded = percent_encode(OrderedDict([("one", 1), ("two", 2)]))
    assert encoded == "one=1&two=2"
コード例 #16
0
def test_can_percent_encode_reserved_chars():
    encoded = percent_encode("20% of $100 = $20")
    assert encoded == "20%25%20of%20%24100%20%3D%20%2420"
コード例 #17
0
def test_can_percent_encode_extended_chars():
    encoded = percent_encode("/El Niño/")
    assert encoded == "%2FEl%20Ni%C3%B1o%2F"
コード例 #18
0
def test_can_percent_encode_with_safe_chars():
    encoded = percent_encode("/El Niño/", safe="/|\\")
    assert encoded == "/El%20Ni%C3%B1o/"
コード例 #19
0
def test_can_percent_encode_with_safe_chars():
    encoded = percent_encode("/El Niño/", safe="/|\\")
    assert encoded == "/El%20Ni%C3%B1o/"