Пример #1
0
def send_tweet(message_to_tweet: str, url_to_media: str) -> int:
    """
    Send a tweet with an image.
    :param message_to_tweet: Message to send
    :param url_to_media: Image to upload
    :return: Tweet ID (-1 if it failed)
    :raises Exception: Tweet failed to send for some reason
    """
    logging.info('Tweet to send: {}'.format(message_to_tweet))
    try:
        if url_to_media is not None:
            resize_image(url_to_media)
            photo = open(url_to_media, 'rb')
            status = twitter_api.request('statuses/update_with_media',
                                         {'status': message_to_tweet},
                                         {'media[]': photo})
            logging.info('Twitter Status Code: {}'.format(status.status_code))

            response = TwitterAPI.TwitterResponse(status, False).json()
            logging.info('Twitter Response Parsed: {}'.format(response))
            return int(response['id_str'])
        raise Exception("No image attached to tweet")
    except UnicodeDecodeError:
        logging.exception(
            'Your message could not be encoded. Perhaps it contains non-ASCII characters?'
        )
        raise Exception("Tweet failed to send")
Пример #2
0
def test_app_abnormal(event, mocker):
    r = requests.Response()
    r.status_code = 401  # authorization error
    r._content = '{}'.encode()
    res = TwitterAPI.TwitterResponse(r, None)
    mocker.patch('TwitterAPI.TwitterAPI.request', return_value=res)

    with pytest.raises(Exception):
        app.handle(event, config)
Пример #3
0
def test_app_normal(event, mocker):
    r = requests.Response()
    r.status_code = 200
    r._content = '{}'.encode()
    res = TwitterAPI.TwitterResponse(r, None)
    mocker.patch('TwitterAPI.TwitterAPI.request', return_value=res)

    ret = app.handle(event, config)
    assert ret == {'MessageIds': ['dummy']}
Пример #4
0
def test_app_normal(event, mocker):
    r = requests.Response()
    r.status_code = 200
    r._content = '{"id_str": "12345"}'.encode()
    res = TwitterAPI.TwitterResponse(r, None)
    mocker.patch('TwitterAPI.TwitterAPI.request', return_value=res)

    ret = app.handle(event, config)
    assert ret == {'Results': {'dummy': '12345'}}
Пример #5
0
def test_app_already_retweeted(event, mocker):
    r = requests.Response()
    r.status_code = 403
    error = {"errors": [{"code": 187, "message": "Status is a duplicate."}]}
    r._content = json.dumps(error).encode()
    res = TwitterAPI.TwitterResponse(r, None)
    mocker.patch('TwitterAPI.TwitterAPI.request', return_value=res)

    ret = app.handle(event, config)
    assert ret == {'Results': {'dummy': None}}
Пример #6
0
def test_app_already_retweeted(event, mocker):
    r = requests.Response()
    r.status_code = 403
    error = {
        "errors": [{
            "code": 327,
            "message": "You have already retweeted this Tweet."
        }]
    }
    r._content = json.dumps(error).encode()
    res = TwitterAPI.TwitterResponse(r, None)
    mocker.patch('TwitterAPI.TwitterAPI.request', return_value=res)

    ret = app.handle(event, config)
    assert ret == {'MessageIds': ['dummy']}