GeometrizeTwitterBot  1.0
Python Twitter bot for geometrizing images into geometric primitives
Functions
on_status_event Namespace Reference

Module containing the code that the bot runs when it receives a status event. More...

Functions

def load_image (filepath)
 Loads an image from the given filepath, returns the loaded image, or None if the loading failed. More...
 
def save_image (image, filepath)
 Saves an image to the given filepath, returns true on success, false on failure. More...
 
def _download_image (url)
 Downloads an image from a tweet. More...
 
def _tweet_image (image_filepath, message, status_id, api)
 Tweets an image. More...
 
def _tweet_message (message, username, status_id, api)
 Tweets a simple message. More...
 
def on_on_demand_status_event (api, status)
 Handles a status change event from the Twitter streaming API. More...
 
def on_account_watcher_status_event (api, status)
 Handles a status change event from the Twitter streaming API. More...
 

Detailed Description

Module containing the code that the bot runs when it receives a status event.

This is where most of the work is triggered e.g. the bot receives a Tweet, it parses the tweet, and figures out how to respond.

Function Documentation

◆ _download_image()

def on_status_event._download_image (   url)
private

Downloads an image from a tweet.

Returns the request content if it succeeds, else None.

39 def _download_image(url):
40  print("Will download image")
41  request = requests.get(url, stream = True)
42  if request.status_code != 200:
43  print("Failed to download image, got status code: " + request.status_code)
44  return None
45  return request.content
46 
Here is the caller graph for this function:

◆ _tweet_image()

def on_status_event._tweet_image (   image_filepath,
  message,
  status_id,
  api 
)
private

Tweets an image.

48 def _tweet_image(image_filepath, message, status_id, api):
49  print("Will tweet image")
50 
51  # Truncate the message
52  max_len = 110
53  trimmed_message = message[:max_len] + (message[max_len:] and '...')
54 
55  api.update_with_media(image_filepath, status = trimmed_message, in_reply_to_status_id = status_id)
56 
Here is the caller graph for this function:

◆ _tweet_message()

def on_status_event._tweet_message (   message,
  username,
  status_id,
  api 
)
private

Tweets a simple message.

58 def _tweet_message(message, username, status_id, api):
59  print("Will send tweet message: " + message + " to " + username)
60  api.update_status(status = '@{0} {1}'.format(username, message), in_reply_to_status_id = status_id)
61 
Here is the caller graph for this function:

◆ load_image()

def on_status_event.load_image (   filepath)

Loads an image from the given filepath, returns the loaded image, or None if the loading failed.

20 def load_image(filepath):
21  image = None
22  try:
23  image = Image.open(filepath)
24  except:
25  print("Failed to load image")
26  return image
27 

◆ on_account_watcher_status_event()

def on_status_event.on_account_watcher_status_event (   api,
  status 
)

Handles a status change event from the Twitter streaming API.

This means waiting for status updates and doing things to response to them.

122 def on_account_watcher_status_event(api, status):
123  username = status.user.screen_name
124  status_id = status.id
125  message = status.text
126 
127  ImageFile.LOAD_TRUNCATED_IMAGES = True
128 
129  code = dependency_locator.read_geometrize_script("geometrize_shape_choice_template.chai")
130  if code == "":
131  print("Failed to read script")
132  return
133 
134  if re.search('Print Geometrize bot status', message, re.IGNORECASE):
135  print("Received bot status request")
136  _tweet_message('Geometrize bot status is alive', username, status_id, api)
137  return
138 
139  if 'media' in status.entities:
140  for image in status.entities['media']:
141  download_filename = 'temp_' + uuid.uuid4().hex + '.png'
142  download_filepath = dependency_locator.get_geometrize_image_file_absolute_path(download_filename)
143  result_filepath = dependency_locator.get_geometrize_image_file_absolute_path('geometrized_' + download_filename)
144 
145  image_data = _download_image(image['media_url'])
146  if image_data is None:
147  print("Failed to download tweet image")
148  continue
149 
150  image = Image.open(BytesIO(image_data))
151  if not save_image(image, download_filepath):
152  print("Failed to save image to filepath " + download_filepath)
153  continue
154 
155  geometrize_options = {}
156  geometrize_options["::IMAGE_INPUT_PATH::"] = download_filepath
157  geometrize_options["::IMAGE_OUTPUT_PATH::"] = result_filepath
158 
159  shape_code, shape_quantity_dictionary = tweet_parser.make_code_for_shape_tweet(message)
160  response_text = tweet_parser.make_message_for_shape_dictionary(shape_quantity_dictionary)
161  geometrize_options["::IMAGE_TASK_STEP_LOOPS::"] = shape_code
162 
163  if not geometrize.run_geometrize(code, geometrize_options):
164  print("Failed to run geometrize")
165  continue
166 
167  at_username = '@{0}'.format(username)
168 
169  _tweet_image(result_filepath, response_text, None, api)
170  print("Did tweet image")
Here is the call graph for this function:
Here is the caller graph for this function:

◆ on_on_demand_status_event()

def on_status_event.on_on_demand_status_event (   api,
  status 
)

Handles a status change event from the Twitter streaming API.

This means waiting for status updates and doing things to response to them.

64 def on_on_demand_status_event(api, status):
65  username = status.user.screen_name
66  status_id = status.id
67  message = status.text
68 
69  ImageFile.LOAD_TRUNCATED_IMAGES = True
70 
71  code = dependency_locator.read_geometrize_script("geometrize_shape_choice_template.chai")
72  if code == "":
73  print("Failed to read script")
74  return
75 
76  if re.search('Print Geometrize bot status', message, re.IGNORECASE):
77  print("Received bot status request")
78  _tweet_message('Geometrize bot status is alive', username, status_id, api)
79  return
80 
81  if 'media' in status.entities:
82  for image in status.entities['media']:
83  download_filename = 'temp_' + uuid.uuid4().hex + '.png'
84  download_filepath = dependency_locator.get_geometrize_image_file_absolute_path(download_filename)
85  result_filepath = dependency_locator.get_geometrize_image_file_absolute_path('geometrized_' + download_filename)
86 
87  image_data = _download_image(image['media_url'])
88  if image_data is None:
89  print("Failed to download tweet image")
90  continue
91 
92  image = Image.open(BytesIO(image_data))
93  if not save_image(image, download_filepath):
94  print("Failed to save image to filepath " + download_filepath)
95  continue
96 
97  geometrize_options = {}
98  geometrize_options["::IMAGE_INPUT_PATH::"] = download_filepath
99  geometrize_options["::IMAGE_OUTPUT_PATH::"] = result_filepath
100 
101  shape_code, shape_quantity_dictionary = tweet_parser.make_code_for_shape_tweet(message)
102  response_text = tweet_parser.make_message_for_shape_dictionary(shape_quantity_dictionary)
103  geometrize_options["::IMAGE_TASK_STEP_LOOPS::"] = shape_code
104 
105  if not geometrize.run_geometrize(code, geometrize_options):
106  print("Failed to run geometrize")
107  continue
108 
109  if not username:
110  continue
111 
112  at_username = '@{0}'.format(username)
113 
114  # Do not tweet @yourself when tweeting images - avoids an infinite tweet loop
115  if at_username != config.TWITTER_BOT_USERNAME:
116  _tweet_image(result_filepath, at_username + " " + response_text, status_id, api)
117 
118  print("Did tweet image")
119 
Here is the call graph for this function:
Here is the caller graph for this function:

◆ save_image()

def on_status_event.save_image (   image,
  filepath 
)

Saves an image to the given filepath, returns true on success, false on failure.

29 def save_image(image, filepath):
30  try:
31  image.save(filepath)
32  except:
33  print("Failed to save image to: " + filepath)
34  return False
35  return True
36 
Here is the caller graph for this function:
on_status_event._tweet_image
def _tweet_image(image_filepath, message, status_id, api)
Tweets an image.
Definition: on_status_event.py:48
on_status_event.on_account_watcher_status_event
def on_account_watcher_status_event(api, status)
Handles a status change event from the Twitter streaming API.
Definition: on_status_event.py:122
on_status_event._download_image
def _download_image(url)
Downloads an image from a tweet.
Definition: on_status_event.py:39
geometrize.run_geometrize
def run_geometrize(code, tags=None)
Executes the given script source code.
Definition: geometrize.py:35
dependency_locator.get_geometrize_image_file_absolute_path
def get_geometrize_image_file_absolute_path(filename)
Composes an absolute path for an image file in the image data folder.
Definition: dependency_locator.py:42
on_status_event.on_on_demand_status_event
def on_on_demand_status_event(api, status)
Handles a status change event from the Twitter streaming API.
Definition: on_status_event.py:64
on_status_event._tweet_message
def _tweet_message(message, username, status_id, api)
Tweets a simple message.
Definition: on_status_event.py:58
tweet_parser.make_code_for_shape_tweet
def make_code_for_shape_tweet(message)
Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of ...
Definition: tweet_parser.py:134
on_status_event.save_image
def save_image(image, filepath)
Saves an image to the given filepath, returns true on success, false on failure.
Definition: on_status_event.py:29
on_status_event.load_image
def load_image(filepath)
Loads an image from the given filepath, returns the loaded image, or None if the loading failed.
Definition: on_status_event.py:20
dependency_locator.read_geometrize_script
def read_geometrize_script(filename)
Reads a Chaiscript script file out of the Twitter bot scripts folder, returning the text content of t...
Definition: dependency_locator.py:63
tweet_parser.make_message_for_shape_dictionary
def make_message_for_shape_dictionary(shape_quantity_dictionary)
Creates a message for the given shape dictionary, suitable for tweeting along with the result of runn...
Definition: tweet_parser.py:158