on_status_event.py 文件源码

python
阅读 24 收藏 0 点赞 0 评论 0

项目:geometrize-twitter-bot 作者: Tw1ddle 项目源码 文件源码
def on_account_watcher_status_event(api, status):
    username = status.user.screen_name
    status_id = status.id
    message = status.text

    ImageFile.LOAD_TRUNCATED_IMAGES = True

    code = dependency_locator.read_geometrize_script("geometrize_shape_choice_template.chai")
    if code == "":
        print("Failed to read script")
        return

    if re.search('Print Geometrize bot status', message, re.IGNORECASE):
        print("Received bot status request")
        _tweet_message('Geometrize bot status is alive', username, status_id, api)
        return

    if 'media' in status.entities:
        for image in status.entities['media']:
            download_filename = 'temp_' + uuid.uuid4().hex + '.png'
            download_filepath = dependency_locator.get_geometrize_image_file_absolute_path(download_filename)
            result_filepath = dependency_locator.get_geometrize_image_file_absolute_path('geometrized_' + download_filename)

            image_data = _download_image(image['media_url'])
            if image_data is None:
                print("Failed to download tweet image")
                continue

            image = Image.open(BytesIO(image_data))
            if not save_image(image, download_filepath):
                print("Failed to save image to filepath " + download_filepath)
                continue

            geometrize_options = {}
            geometrize_options["::IMAGE_INPUT_PATH::"] = download_filepath
            geometrize_options["::IMAGE_OUTPUT_PATH::"] = result_filepath

            shape_code, shape_quantity_dictionary = tweet_parser.make_code_for_shape_tweet(message)
            response_text = tweet_parser.make_message_for_shape_dictionary(shape_quantity_dictionary)
            geometrize_options["::IMAGE_TASK_STEP_LOOPS::"] = shape_code

            if not geometrize.run_geometrize(code, geometrize_options):
                print("Failed to run geometrize")
                continue

            at_username = '@{0}'.format(username)

            _tweet_image(result_filepath, response_text, None, api)
            print("Did tweet image")
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号