Skip to content

Instantly share code, notes, and snippets.

@dradtke
Created June 28, 2018 21:35
Show Gist options
  • Save dradtke/fdd0ba96948a38b67b600dd895ff83ca to your computer and use it in GitHub Desktop.
Save dradtke/fdd0ba96948a38b67b600dd895ff83ca to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
from marionette_driver.marionette import Actions, Marionette, Keys
from marionette_driver.errors import NoSuchElementException
from marionette_driver.expected import element_present, element_not_present from marionette_driver import By, Wait
import base64
import os
import time
root_url = 'https://teaas.fyi/'
first = True
client = Marionette('localhost', port=2828)
client.start_session()
def emojify(effect, image):
print("Applying effect '%s' to %s..." % (effect, image))
# Navigate to the effect page and submit the image.
navigate_to(effect)
time.sleep(0.5)
file_input = client.find_element(By.ID, 'emoji')
file_input.send_keys(os.path.abspath(image))
time.sleep(0.5)
resize_64 = client.find_element(By.ID, 'resize64')
resize_64.click()
time.sleep(0.5)
submit = client.find_element(By.CSS_SELECTOR, 'input[type="submit"]')
submit.click()
# Wait until we've officially moved off of the submit page,
# and then wait until the next one loads.
Wait(client).until(element_not_present(By.CSS_SELECTOR, 'input[type="submit"]'))
Wait(client).until(element_present(By.CLASS_NAME, 'container'))
# Download the results.
container = client.find_element(By.CLASS_NAME, 'container')
i = 0
try:
while True:
time.sleep(0.25)
image = container.find_element(By.TAG_NAME, 'img')
src = image.get_attribute('src')
data = None
if src.startswith('data:image/gif;base64,'):
encoded_gif = src[len('data:image/gif;base64,'):]
data = base64.standard_b64decode(encoded_gif)
if data:
i += 1
with open('results/%s_%d.gif' % (effect, i), 'wb') as f:
f.write(data)
else:
print("don't know how to save image with source: " + src)
continue
# Delete the element from the page to indicate that it's been saved.
client.execute_script('var element = document.querySelector(".container img"); element.parentNode.removeChild(element);')
except NoSuchElementException:
pass
def navigate_to(effect):
global first
with client.using_context(client.CONTEXT_CHROME):
urlbar = client.find_element(By.ID, 'urlbar')
if first:
first = False
urlbar.clear()
for char in root_url+effect:
urlbar.send_keys(char)
time.sleep(0.1)
else:
while urlbar.text != root_url:
with client.using_context(client.CONTEXT_CHROME):
urlbar = client.find_element(By.ID, 'urlbar')
urlbar.send_keys([Keys.RIGHT, Keys.BACK_SPACE])
time.sleep(0.1)
urlbar.send_keys(Keys.RIGHT)
for char in effect:
urlbar.send_keys(char)
time.sleep(0.1)
Actions(client).key_down(Keys.ENTER).perform()
image = 'villager.jpg'
for effect in [
'spin',
'fireify',
'parrotify',
'shakefistify',
'dealwithit',
]:
emojify(effect, image)
client.execute_script('alert("Deal With It")')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment