Skip to content

Instantly share code, notes, and snippets.

@Ttech
Created April 2, 2014 17:18
Show Gist options
  • Save Ttech/9938706 to your computer and use it in GitHub Desktop.
Save Ttech/9938706 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import sys,urllib
from hashlib import sha256
from os.path import basename
from urlparse import urlparse
from random import randint
from wand.color import Color
from wand.image import Image
image_types = ['jpg','jpeg','gif','png','tiff']
tmp_path = '/usr/share/nginx/www/tmp/'
final_path = '/usr/share/nginx/www/pub/'
url_path = 'http://192.168.2.1/pub/'
def modify_url(line):
url = line.split(' ')[0]
output='\n'
file_extension = basename(urlparse(url).path).split('.')[-1]
if file_extension in image_types:
# download file
tmp_file = tmp_path + basename(urlparse(url).path)
try:
urllib.urlretrieve (url, tmp_file)
# do something with the file
with Image(filename=tmp_file) as image:
with image.clone() as magic:
if randint(0,1) is 1:
magic.flip()
else:
magic.rotate(90)
hash_file = sha256(tmp_file).hexdigest() + '.' + file_extension
magic.save(filename=(final_path + hash_file))
output = url_path + hash_file + '\n'
except urllib.ContentTooShortError,wand.exceptions.CorruptImageError:
output = '\n'
return output
while True:
line = sys.stdin.readline().strip()
new_url = modify_url(line)
sys.stdout.write(new_url)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment