GeometrizeTwitterBot  1.0
Python Twitter bot for geometrizing images into geometric primitives
 All Classes Namespaces Files Functions Variables
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
 Loads an image from the given filepath, returns the loaded image, or None if the loading failed. More...
 
def save_image
 Saves an image to the given filepath, returns true on success, false on failure. More...
 
def _download_image
 Downloads an image from a tweet. More...
 
def _tweet_image
 Tweets an image. More...
 
def _tweet_message
 Tweets a simple message. More...
 
def on_on_demand_status_event
 Handles a status change event from the Twitter streaming API. More...
 
def on_account_watcher_status_event
 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

def on_status_event._download_image (   url)
private

Downloads an image from a tweet.

Returns the request content if it succeeds, else None.

39 
40 def _download_image(url):
41  print("Will download image")
42  request = requests.get(url, stream = True)
43  if request.status_code != 200:
44  print("Failed to download image, got status code: " + request.status_code)
45  return None
46  return request.content
def _download_image
Downloads an image from a tweet.
Definition: on_status_event.py:39

Here is the caller graph for this function:

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

Tweets an image.

48 
49 def _tweet_image(image_filepath, message, status_id, api):
50  print("Will tweet image")
51 
52  # Truncate the message
53  max_len = 110
54  trimmed_message = message[:max_len] + (message[max_len:] and '...')
55 
56  api.update_with_media(image_filepath, status = trimmed_message, in_reply_to_status_id = status_id)
def _tweet_image
Tweets an image.
Definition: on_status_event.py:48

Here is the caller graph for this function:

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

Tweets a simple message.

58 
59 def _tweet_message(message, username, status_id, api):
60  print("Will send tweet message: " + message + " to " + username)
61  api.update_status(status = '@{0} {1}'.format(username, message), in_reply_to_status_id = status_id)
def _tweet_message
Tweets a simple message.
Definition: on_status_event.py:58

Here is the caller graph for this function:

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 
21 def load_image(filepath):
22  image = None
23  try:
24  image = Image.open(filepath)
25  except:
26  print("Failed to load image")
27  return image
def load_image
Loads an image from the given filepath, returns the loaded image, or None if the loading failed...
Definition: on_status_event.py:20
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.

123 def on_account_watcher_status_event(api, status):
124  username = status.user.screen_name
125  status_id = status.id
126  message = status.text
127 
128  ImageFile.LOAD_TRUNCATED_IMAGES = True
129 
130  code = dependency_locator.read_geometrize_script("geometrize_shape_choice_template.chai")
131  if code == "":
132  print("Failed to read script")
133  return
134 
135  if re.search('Print Geometrize bot status', message, re.IGNORECASE):
136  print("Received bot status request")
137  _tweet_message('Geometrize bot status is alive', username, status_id, api)
138  return
139 
140  if 'media' in status.entities:
141  for image in status.entities['media']:
142  download_filename = 'temp_' + uuid.uuid4().hex + '.png'
143  download_filepath = dependency_locator.get_geometrize_image_file_absolute_path(download_filename)
144  result_filepath = dependency_locator.get_geometrize_image_file_absolute_path('geometrized_' + download_filename)
145 
146  image_data = _download_image(image['media_url'])
147  if image_data is None:
148  print("Failed to download tweet image")
149  continue
150 
151  image = Image.open(BytesIO(image_data))
152  if not save_image(image, download_filepath):
153  print("Failed to save image to filepath " + download_filepath)
154  continue
155 
156  geometrize_options = {}
157  geometrize_options["::IMAGE_INPUT_PATH::"] = download_filepath
158  geometrize_options["::IMAGE_OUTPUT_PATH::"] = result_filepath
159 
160  shape_code, shape_quantity_dictionary = tweet_parser.make_code_for_shape_tweet(message)
161  response_text = tweet_parser.make_message_for_shape_dictionary(shape_quantity_dictionary)
162  geometrize_options["::IMAGE_TASK_STEP_LOOPS::"] = shape_code
163 
164  if not geometrize.run_geometrize(code, geometrize_options):
165  print("Failed to run geometrize")
166  continue
167 
168  at_username = '@{0}'.format(username)
169 
170  _tweet_image(result_filepath, response_text, None, api)
print("Did tweet image")
def run_geometrize
Executes the given script source code.
Definition: geometrize.py:35
def make_code_for_shape_tweet
Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of ...
Definition: tweet_parser.py:134
def make_message_for_shape_dictionary
Creates a message for the given shape dictionary, suitable for tweeting along with the result of runn...
Definition: tweet_parser.py:158
def read_geometrize_script
Reads a Chaiscript script file out of the Twitter bot scripts folder, returning the text content of t...
Definition: dependency_locator.py:57
def save_image
Saves an image to the given filepath, returns true on success, false on failure.
Definition: on_status_event.py:29
def get_geometrize_image_file_absolute_path
Composes an absolute path for an image file in the image data folder.
Definition: dependency_locator.py:36
def _download_image
Downloads an image from a tweet.
Definition: on_status_event.py:39
def _tweet_message
Tweets a simple message.
Definition: on_status_event.py:58
def on_account_watcher_status_event
Handles a status change event from the Twitter streaming API.
Definition: on_status_event.py:122
def _tweet_image
Tweets an image.
Definition: on_status_event.py:48

Here is the call graph for this function:

Here is the caller graph for this function:

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 
65 def on_on_demand_status_event(api, status):
66  username = status.user.screen_name
67  status_id = status.id
68  message = status.text
69 
70  ImageFile.LOAD_TRUNCATED_IMAGES = True
71 
72  code = dependency_locator.read_geometrize_script("geometrize_shape_choice_template.chai")
73  if code == "":
74  print("Failed to read script")
75  return
76 
77  if re.search('Print Geometrize bot status', message, re.IGNORECASE):
78  print("Received bot status request")
79  _tweet_message('Geometrize bot status is alive', username, status_id, api)
80  return
81 
82  if 'media' in status.entities:
83  for image in status.entities['media']:
84  download_filename = 'temp_' + uuid.uuid4().hex + '.png'
85  download_filepath = dependency_locator.get_geometrize_image_file_absolute_path(download_filename)
86  result_filepath = dependency_locator.get_geometrize_image_file_absolute_path('geometrized_' + download_filename)
87 
88  image_data = _download_image(image['media_url'])
89  if image_data is None:
90  print("Failed to download tweet image")
91  continue
92 
93  image = Image.open(BytesIO(image_data))
94  if not save_image(image, download_filepath):
95  print("Failed to save image to filepath " + download_filepath)
96  continue
97 
98  geometrize_options = {}
99  geometrize_options["::IMAGE_INPUT_PATH::"] = download_filepath
100  geometrize_options["::IMAGE_OUTPUT_PATH::"] = result_filepath
101 
102  shape_code, shape_quantity_dictionary = tweet_parser.make_code_for_shape_tweet(message)
103  response_text = tweet_parser.make_message_for_shape_dictionary(shape_quantity_dictionary)
104  geometrize_options["::IMAGE_TASK_STEP_LOOPS::"] = shape_code
105 
106  if not geometrize.run_geometrize(code, geometrize_options):
107  print("Failed to run geometrize")
108  continue
109 
110  if not username:
111  continue
112 
113  at_username = '@{0}'.format(username)
114 
115  # Do not tweet @yourself when tweeting images - avoids an infinite tweet loop
116  if at_username != config.TWITTER_BOT_USERNAME:
117  _tweet_image(result_filepath, at_username + " " + response_text, status_id, api)
118 
119  print("Did tweet image")
def run_geometrize
Executes the given script source code.
Definition: geometrize.py:35
def make_code_for_shape_tweet
Parses the given tweet, returning the ChaiScript code for Geometrize to run based on the contents of ...
Definition: tweet_parser.py:134
def make_message_for_shape_dictionary
Creates a message for the given shape dictionary, suitable for tweeting along with the result of runn...
Definition: tweet_parser.py:158
def read_geometrize_script
Reads a Chaiscript script file out of the Twitter bot scripts folder, returning the text content of t...
Definition: dependency_locator.py:57
def save_image
Saves an image to the given filepath, returns true on success, false on failure.
Definition: on_status_event.py:29
def get_geometrize_image_file_absolute_path
Composes an absolute path for an image file in the image data folder.
Definition: dependency_locator.py:36
def _download_image
Downloads an image from a tweet.
Definition: on_status_event.py:39
def _tweet_message
Tweets a simple message.
Definition: on_status_event.py:58
def _tweet_image
Tweets an image.
Definition: on_status_event.py:48
def on_on_demand_status_event
Handles a status change event from the Twitter streaming API.
Definition: on_status_event.py:64

Here is the call graph for this function:

Here is the caller graph for this function:

def on_status_event.save_image (   image,
  filepath 
)

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

29 
30 def save_image(image, filepath):
31  try:
32  image.save(filepath)
33  except:
34  print("Failed to save image to: " + filepath)
35  return False
36  return True
def save_image
Saves an image to the given filepath, returns true on success, false on failure.
Definition: on_status_event.py:29

Here is the caller graph for this function: