Skip to content

Instantly share code, notes, and snippets.

@Ttech
Created April 4, 2014 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ttech/9967836 to your computer and use it in GitHub Desktop.
Save Ttech/9967836 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','png']
tmp_path = '/var/www/tmp/'
final_path = '/var/www/pub/'
url_path = 'http://192.168.2.1/pub/'
log_path = '/var/log/squid-magic.log'
def debug_rewrite(message):
with open(log_path, "a+") as log:
log.write(message + "\n")
def modify_url(line):
debug_rewrite('Notice: Processing Image: %s' % line)
arguments = line.split(' ')
# well, we need this to pass args back to the function.
url = arguments[0]
try:
file_extension = basename(urlparse(url).path).split('.')[-1]
except:
debug_rewrite('Notice: No filename dete')
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 = '301:' + url_path + hash_file + '\n'
debug_rewrite("Success: Converted Image: %s => %s" % (url,output))
except:
# (IOError,urllib.ContentTooShortError,wand.exceptions.CorruptImageError) as e:
output = '\n'
debug_rewrite('Error: An exception has occured. Skipping processing.')
else:
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