def handle_item(item, notification, service, test): """Callback for Timeline updates.""" if "userActions" in notification: for action in notification["userActions"]: if "type" in action and action["type"] == "SHARE": break else: # No SHARE action return else: # No SHARE action return if "recipients" in item: for rec in item["recipients"]: if rec["id"] == "instaglass_sepia": break else: # Item not meant for this service return else: # Item not meant for this service return imageId = None if "attachments" in item: for att in item["attachments"]: if att["contentType"].startswith("image/"): imageId = att["id"] break if imageId is None: logging.info("No suitable attachment") return attachment_metadata = service.timeline().attachments().get( itemId=item["id"], attachmentId=imageId).execute() content_url = attachment_metadata.get("contentUrl") resp, content = service._http.request(content_url) if resp.status != 200: logging.info("Couldn't fetch attachment") tempimg = cStringIO.StringIO(content) im = Image.open(tempimg) new_im = _apply_sepia_filter(im) f = cStringIO.StringIO() new_im.save(f, "JPEG") content = f.getvalue() f.close() new_item = {} new_item["menuItems"] = [{"action": "SHARE"}] result = upload.multipart_insert(new_item, content, "image/jpeg", service, test) logging.info(result)
def handle_item(item, service, test): """Callback for Timeline updates.""" if "recipients" in item: for rec in item["recipients"]: if rec["id"] == "add_a_cat": break else: # Item not meant for this service return else: # Item not meant for this service return imageId = None if "attachments" in item: for att in item["attachments"]: if att["contentType"].startswith("image/"): imageId = att["id"] break if imageId is None: logging.info("No suitable attachment") return attachment_metadata = service.timeline().attachments().get( itemId=item["id"], attachmentId=imageId).execute() content_url = attachment_metadata.get("contentUrl") resp, content = service._http.request(content_url) if resp.status != 200: logging.info("Couldn't fetch attachment") tempimg = cStringIO.StringIO(content) im = Image.open(tempimg) cat = random.randint(1, _NUM_CATS) cat_image = Image.open("res/cat%s.png" % cat) zoom = im.size[0] / 640 cat_image.resize((cat_image.size[0] * zoom, cat_image.size[1] * zoom), Image.ANTIALIAS) x = random.randint(0, im.size[0] - cat_image.size[0]) y = random.randint(0, im.size[1] - cat_image.size[1]) im.paste(cat_image, (x, y), cat_image) f = cStringIO.StringIO() im.save(f, "JPEG") content = f.getvalue() f.close() new_item = {} new_item["menuItems"] = [{"action": "SHARE"}] result = upload.multipart_insert(new_item, content, "image/jpeg", service, test) logging.info(result)