diff options
| -rw-r--r-- | config.py | 2 | ||||
| -rw-r--r-- | lib/__init__.py | 0 | ||||
| -rw-r--r-- | lib/dumpfm.py | 59 | ||||
| -rw-r--r-- | lib/imbreak.py | 28 | ||||
| -rw-r--r-- | lib/imbreak_params_defaults.py | 61 | ||||
| -rw-r--r-- | lib/imgradient.py | 37 | ||||
| -rw-r--r-- | lib/imgradient_params_defaults.py | 181 | ||||
| -rw-r--r-- | lib/imgrid_params_defaults.py | 153 | ||||
| -rw-r--r-- | lib/impattern.py | 28 | ||||
| -rw-r--r-- | lib/utils.py | 49 |
10 files changed, 598 insertions, 0 deletions
diff --git a/config.py b/config.py new file mode 100644 index 0000000..cb13b7c --- /dev/null +++ b/config.py @@ -0,0 +1,2 @@ +USERNAME = "RICHARD_GIOVANNI" +TEST_URL = "http://i.asdf.us/im/fc/imBreak5qI6DN2_1425433157_.png" diff --git a/lib/__init__.py b/lib/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/lib/__init__.py diff --git a/lib/dumpfm.py b/lib/dumpfm.py new file mode 100644 index 0000000..e7f2688 --- /dev/null +++ b/lib/dumpfm.py @@ -0,0 +1,59 @@ +#!/usr/bin/python2.7 +import urllib +import urllib2 +import simplejson as json +import sys +import re +import os +urlencode = urllib.urlencode +urlopen = urllib2.urlopen +Request = urllib2.Request + +DEFAULT_TERM = "pizza" + +#{{{ +def get_request(url): + try: + headers = { + 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)', + 'Accept': '*/*' + } + req = Request(url, None, headers) + response = urlopen(req) + return response.read() + except IOError, e: + if hasattr(e, 'code'): + sys.stderr.write( '%s - ERROR %s' % (url, e.code)) + else: + sys.stderr.write(str(e)) + sys.exit(1) +#}}} +class Image_url(): + def __init__(self, result): + self._url = result['url'] + def url(self): + if self._url[0] == '/': + return "{}{}".format('http://dump.fm/images', self._url) + else : + return "{}{}".format('http://', self._url) +class DumpFmImageSearch: + def __init__(self): + self._search_url = 'http://dump.fm/cmd/search/' + def search(self, *args): # something like this should work. + if len(args) < 1: + sys.stderr.write("Must provide search term"); + raise ValueError + terms = "+".join(map(lambda x : urllib.quote(x), args)); + url = "{}{}".format(self._search_url, terms) + resp = get_request(url); + return map( + lambda x: Image_url(x), + json.loads(resp) + ) + + +if __name__ == '__main__': + term = sys.argv[1] if len(sys.argv) > 1 else DEFAULT_TERM + s = DumpFmImageSearch() + images = s.search(term) + print images[0].url() diff --git a/lib/imbreak.py b/lib/imbreak.py new file mode 100644 index 0000000..046f51b --- /dev/null +++ b/lib/imbreak.py @@ -0,0 +1,28 @@ +#!/usr/bin/python2.7 +import urllib +import urllib2 +import simplejson as json +import random +import sys +from lib.utils import post_request + +IMBREAK_URL = "http://asdf.us/im/api/imbreak" + +class ImBreak(): + def __init__(self): + self._required_keys = [ + "url", + "breaktype", + "finalformat", + "breakmode", + "breakangle", + "username", + "expanded" + ] + def new(self, params): + for k in self._required_keys: + if not k in params: + params[k] = ""; + self.params = params + return json.loads(post_request(IMBREAK_URL, self.params)) + diff --git a/lib/imbreak_params_defaults.py b/lib/imbreak_params_defaults.py new file mode 100644 index 0000000..1d9a81d --- /dev/null +++ b/lib/imbreak_params_defaults.py @@ -0,0 +1,61 @@ +#!/usr/bin/python2.7 +import random +from config import USERNAME, TEST_URL +from lib.utils import Pb_Api_Params + +class ImBreakParams_FromDefaults(Pb_Api_Params): + def __init__(self): + self.weighted_breaktype = [ + {"value":"CLASSIC", "weight": 1}, + {"value":"REDUX", "weight": 1}, + {"value":"BLURRY_BREAK", "weight": 1}, + {"value":"BLURRY_BREAK_2", "weight": 1}, + {"value":"SWIPE", "weight": 1}, + {"value":"RGB_WASH", "weight": 1}, + {"value":"RGB_WASH_2", "weight": 1}, + {"value":"NOISY_BREAK", "weight": 1}, + {"value":"BROKEN_VIGNETTE", "weight": 1}, + {"value":"FAX_MACHINE", "weight": 1}, + {"value":"STRIPES", "weight": 1}, + {"value":"PHOTOCOPY", "weight": 1}, + ] + self.weighted_breakmode = [ + {"value":"extreme", "weight": 1}, + {"value":"subtle", "weight": 1}, + ] + self.weighted_finalformat = [ + {"value":"png", "weight": 5}, + {"value":"jpg", "weight": 2}, + {"value":"gif", "weight": 2}, + ] + self.weighted_breakangle = [ + {"value":0, "weight": 9}, + {"value":90, "weight": 2}, + {"value":180, "weight": 2}, + {"value":270, "weight": 2}, + {"value":random.randint(0,360), "weight": 4}, + ] + self.weighted_expanded = [ + {"value": "" , "weight": 11 }, + {"value": 1, "weight": 2} + ] + def from_random(self, url=TEST_URL): + return { + "url" : url, + "username" : USERNAME, + "breakmode" : self._weighted_choice( self.weighted_breakmode ), + "breaktype" : self._weighted_choice( self.weighted_breaktype ), + "breakangle" : self._weighted_choice( self.weighted_breakangle ), + "finalformat" : self._weighted_choice( self.weighted_finalformat ), + "expanded" : self._weighted_choice( self.weighted_expanded ), + } + def from_default(self, url=TEST_URL): + return { + "url" : url, + "username" : USERNAME, + "breakmode" : self._default_choice( self.default.breakmode ), + "breaktype" : self._default_choice( self.default.breaktype ), + "breakangle" : self._default_choice( self.default.breakangle ), + "finalformat" : self._default_choice( self.default.finalformat ), + "expanded" : self._default_choice( self.default.expanded ), + } diff --git a/lib/imgradient.py b/lib/imgradient.py new file mode 100644 index 0000000..af743f1 --- /dev/null +++ b/lib/imgradient.py @@ -0,0 +1,37 @@ +#!/usr/bin/python2.7 +import urllib +import urllib2 +import simplejson as json +import random +import sys +from lib.utils import post_request, Service + +IMGRADIENT_URL = "http://asdf.us/im/api/imgradient" + + + +class ImGradient(Service): + def __init__(self): + self.url = IMGRADIENT_URL + self._required_keys = [ + "width", "height", + "color1", "color2", + "stripes", + "stripenumber", "stripeintensity", + "blurriness", + "contrast", + "brightness", "saturation", "hue", + "halftone", + "bevel", "percentbeveled", + "rotate", "flip", "flop", "tilt", + "filetype", + "gradienttype", + "username", + ] + def new(self, params): + for k in self._required_keys: + if not k in params: + params[k] = ""; + self.params = params + return json.loads(post_request(IMGRADIENT_URL, self.params)) + diff --git a/lib/imgradient_params_defaults.py b/lib/imgradient_params_defaults.py new file mode 100644 index 0000000..140939f --- /dev/null +++ b/lib/imgradient_params_defaults.py @@ -0,0 +1,181 @@ +#!/usr/bin/python2.7 +import random +from config import USERNAME, TEST_URL +from lib.utils import Pb_Api_Params + +# "width", "height", +# "color1", "color2", +# "stripes", +# "stripenumber", "stripeintensity", +# "blurriness", +# "contrast", +# "brightness", "saturation", "hue", +# "halftone", +# "bevel", "percentbeveled", +# "rotate", "flip", "flop", "tilt", +# "filetype", +# "gradienttype", +# "username", + +class ImGradientParams_FromDefaults(Pb_Api_Params): + def __init__(self): + self.weighted_width = [ + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, + ] + self.weighted_height = [ + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, + ] + self.weighted_colors = [ + { "value" : + "rgb({},{},{})".format( + random.randint(0,255), + random.randint(0,255), + random.randint(0,255), + ), + "weight" : 1 }, + { "value" : "black", "weight" : 1 }, + { "value" : "white", "weight" : 1 }, + ] + self.weighted_stripes = [ + {"value":"true", "weight": 1}, + {"value":"false", "weight": 1}, + ] + self.weighted_stripenumber = [ + {"value":1, "weight": 1}, + {"value":2, "weight": 1}, + {"value":random.randint(0,400), "weight": 1}, + ] + self.weighted_stripeintensity = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,2000), "weight": 1}, + ] + self.weighted_blurriness = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,20), "weight": 1}, + ] + self.weighted_contrast = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,20), "weight": 1}, + ] + self.weighted_brightness = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,200), "weight": 1}, + ] + self.weighted_saturation = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,200), "weight": 1}, + ] + self.weighted_hue = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,200), "weight": 1}, + ] + self.weighted_halftone = [ + { "value" : "checkeredfade", "weight": 1 }, + { "value" : "etchedtransition", "weight": 1 }, + { "value" : "bendaydots", "weight": 1 }, + { "value" : "smallerdots1", "weight": 1 }, + { "value" : "smallerdots2", "weight": 1 }, + { "value" : "flatstripes", "weight": 1 }, + ] + self.weighted_bevel = [ + { "value" : "", "weight" : 4 }, + { "value" : "flatout", "weight" : 1 }, + { "value" : "flatinner", "weight" : 1 }, + { "value" : "evenlyframed", "weight" : 1 }, + { "value" : "biginner", "weight" : 1 }, + { "value" : "bigouter", "weight" : 1 }, + { "value" : "dramaticflatout", "weight" : 1 }, + { "value" : "dramaticflatinner", "weight" : 1 }, + ] + self.weighted_percentbeveled = [ + { "value" : random.randint(0,99), "weight": 1 }, + { "value" : "", "weight": 4 }, + ] + self.weighted_rotate = [ + {"value":"", "weight": 9}, + {"value":90, "weight": 2}, + {"value":180, "weight": 2}, + {"value":270, "weight": 2}, + {"value":random.randint(0,360), "weight": 4}, + ] + self.weighted_flop = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_flip = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_tilt = [ + {"value":"", "weight": 9}, + {"value":90, "weight": 2}, + {"value":180, "weight": 2}, + {"value":270, "weight": 2}, + {"value":random.randint(0,360), "weight": 4}, + ] + self.weighted_filetype = [ + {"value":"png", "weight": 5}, + {"value":"jpg", "weight": 2}, + {"value":"gif", "weight": 2}, + ] + self.weighted_gradienttype = [ + { "value" : "canvas", "weight" : 1 }, + { "value" : "gradient", "weight" : 3 }, + { "value" : "radial", "weight" : 1 }, + { "value" : "colorspace", "weight" : 1 }, + { "value" : "plasmawash", "weight" : 1 }, + { "value" : "gradientwash", "weight" : 1 }, + { "value" : "mirrored", "weight" : 1 }, + { "value" : "noise", "weight" : 1 }, + ] + def from_random(self): + return { + "username" : USERNAME, + "width" : self._weighted_choice( self.weighted_width ), + "height" : self._weighted_choice( self.weighted_height ), + "color1" : self._weighted_choice( self.weighted_colors ), + "color2" : self._weighted_choice( self.weighted_colors ), + "stripenumber" : self._weighted_choice( self.weighted_stripenumber ), + "stripeintensity" : self._weighted_choice( self.weighted_stripeintensity ), + "blurriness" : self._weighted_choice( self.weighted_blurriness ), + "contrast" : self._weighted_choice( self.weighted_contrast ), + "brightness" : self._weighted_choice( self.weighted_brightness ), + "saturation" : self._weighted_choice( self.weighted_saturation ), + "hue" : self._weighted_choice( self.weighted_hue ), + "halftone" : self._weighted_choice( self.weighted_halftone ), + "bevel" : self._weighted_choice( self.weighted_bevel ), + "percentbeveled" : self._weighted_choice( self.weighted_percentbeveled ), + "rotate" : self._weighted_choice( self.weighted_rotate ), + "flip" : self._weighted_choice( self.weighted_flip ), + "flop" : self._weighted_choice( self.weighted_flop ), + "tilt" : self._weighted_choice( self.weighted_tilt ), + "filetype" : self._weighted_choice( self.weighted_filetype ), + "gradienttype" : self._weighted_choice( self.weighted_gradienttype ), + } + def from_default(self): + return { + "url" : url, + "username" : USERNAME, + "width" : self._default_choice( self.weighted_width ), + "height" : self._default_choice( self.weighted_height ), + "color1" : self._default_choice( self.weighted_colors ), + "color2" : self._default_choice( self.weighted_colors ), + "stripenumber" : self._default_choice( self.weighted_stripenumber ), + "stripeintensity" : self._default_choice( self.weighted_stripeintensity ), + "blurriness" : self._default_choice( self.weighted_blurriness ), + "contrast" : self._default_choice( self.weighted_contrast ), + "brightness" : self._default_choice( self.weighted_brightness ), + "saturation" : self._default_choice( self.weighted_saturation ), + "hue" : self._default_choice( self.weighted_hue ), + "halftone" : self._default_choice( self.weighted_halftone ), + "bevel" : self._default_choice( self.weighted_bevel ), + "percentbeveled" : self._default_choice( self.weighted_percentbeveled ), + "rotate" : self._default_choice( self.weighted_rotate ), + "flip" : self._default_choice( self.weighted_flip ), + "flop" : self._default_choice( self.weighted_flop ), + "tilt" : self._default_choice( self.weighted_tilt ), + "filetype" : self._default_choice( self.weighted_filetype ), + "gradienttype" : self._default_choice( self.weighted_gradienttype ), + } diff --git a/lib/imgrid_params_defaults.py b/lib/imgrid_params_defaults.py new file mode 100644 index 0000000..cc84546 --- /dev/null +++ b/lib/imgrid_params_defaults.py @@ -0,0 +1,153 @@ +#!/usr/bin/python2.7 +import random +from config import USERNAME, TEST_URL +from lib.utils import Pb_Api_Params + + +class ImGradientParams_FromDefaults(Pb_Api_Params): + def __init__(self): + self.weighted_width = [ + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, + ] + self.weighted_height = [ + { "value" : 400, "weight" : 1 }, + { "value" : 600, "weight" : 1 }, + ] + self.weighted_colors = [ + { "value" : + "rgb({},{},{})".format( + random.randint(0,255), + random.randint(0,255), + random.randint(0,255), + ), + "weight" : 1 }, + { "value" : "black", "weight" : 1 }, + { "value" : "white", "weight" : 1 }, + { "value" : "silver", "weight" : 1 }, + ] + self.weighted_linethickness = [ + {"value":1, "weight": 2}, + {"value":2, "weight": 1}, + {"value":random.randint(1,100), "weight": 1}, + {"value":random.randint(1,200), "weight": 1}, + ] + self.weighted_opacity = [ + {"value":1.0, "weight": 2}, + {"value":0.5, "weight": 1}, + {"value":float(random.randint(0,10)/10.0), "weight": 1}, + ] + self.weighted_spacing = [ + {"value":10, "weight": 1}, + {"value":random.randint(10,400), "weight": 1}, + {"value":random.randint(10,100), "weight": 1}, + ] + self.weighted_vlines = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_hlines = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_shadow = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_stripeintensity = [ + {"value": "", "weight": 3}, + {"value": random.randint(0,2000), "weight": 1}, + ] + self.weighted_swing = [ + {"value": "", "weight": 3}, + {"value": random.randint(-180,180), "weight": 1}, + ] + self.weighted_tilt = [ + {"value": "", "weight": 3}, + {"value": random.randint(-180,180), "weight": 1}, + ] + self.weighted_roll = [ + {"value": "", "weight": 3}, + {"value": random.randint(-180,180), "weight": 1}, + ] + self.weighted_zoom = [ + {"value": "", "weight": 3}, + {"value": random.randint(1,12), "weight": 1}, + {"value": random.randint(-1,-12), "weight": 1}, + ] + self.weighted_trim = [ + {"value":"", "weight": 1}, + {"value":"true", "weight": 1}, + ] + self.weighted_transition = [ + { "value" : "background", weight: 1 }, + { "value" : "dither", weight: 1 }, + { "value" : "random", weight: 1 }, + { "value" : "tile", weight: 1 }, + { "value" : "edge", weight: 1 }, + ] + self.weighted_format = [ + {"value":"png", "weight": 5}, + {"value":"jpg", "weight": 2}, + {"value":"gif", "weight": 2}, + ] + def from_random(self, imageinstead="", bgimage="", planebgimage=""): + return { + "username" : USERNAME, + "width" : self._weighted_choice( self.weighted_width ), + "height" : self._weighted_choice( self.weighted_height ), + + "skycolor" : self._weighted_choice( self.weighted_colors ), + "planebgcolor" : self._weighted_choice( self.weighted_colors ), + "bgcolor" : self._weighted_choice( self.weighted_colors ), + "linecolor" : self._weighted_choice( self.weighted_colors ), + + "bgimage" : bgimage, + "imageinstead" : imageinstead, + "planebgimage" : planebgimage, + + "linethickness" : self._weighted_choice( self.weighted_linethickness ), + "opacity" : self._weighted_choice( self.weighted_opacity ), + "spacing" : self._weighted_choice( self.weighted_spacing ), + "vlines" : self._weighted_choice( self.weighted_vlines ), + "hlines" : self._weighted_choice( self.weighted_hlines ), + "shadow" : self._weighted_choice( self.weighted_shadow ), + + "swing" : self._weighted_choice( self.weighted_swing ), + "tilt" : self._weighted_choice( self.weighted_tilt ), + "roll" : self._weighted_choice( self.weighted_roll ), + "zoom" : self._weighted_choice( self.weighted_zoom ), + "transition" : self._weighted_choice( self.weighted_transition ), + "trim" : self._weighted_choice( self.weighted_trim ), + "format" : self._weighted_choice( self.weighted_format ), + } + def from_default(self, imageinstead="", bgimage="", planebgimage=""): + return { + "username" : USERNAME, + "width" : self._default_choice( self.weighted_width ), + "height" : self._default_choice( self.weighted_height ), + + "skycolor" : self._default_choice( self.weighted_colors ), + "planebgcolor" : self._default_choice( self.weighted_colors ), + "bgcolor" : self._default_choice( self.weighted_colors ), + "linecolor" : self._default_choice( self.weighted_colors ), + + "bgimage" : bgimage, + "imageinstead" : imageinstead, + "planebgimage" : planebgimage, + + "linethickness" : self._default_choice( self.weighted_linethickness ), + "opacity" : self._default_choice( self.weighted_opacity ), + "spacing" : self._default_choice( self.weighted_spacing ), + "vlines" : self._default_choice( self.weighted_vlines ), + "hlines" : self._default_choice( self.weighted_hlines ), + "shadow" : self._default_choice( self.weighted_shadow ), + + "swing" : self._default_choice( self.weighted_swing ), + "tilt" : self._default_choice( self.weighted_tilt ), + "roll" : self._default_choice( self.weighted_roll ), + "zoom" : self._default_choice( self.weighted_zoom ), + "transition" : self._default_choice( self.weighted_transition ), + "trim" : self._default_choice( self.weighted_trim ), + "format" : self._default_choice( self.weighted_format ), + } diff --git a/lib/impattern.py b/lib/impattern.py new file mode 100644 index 0000000..0e4f120 --- /dev/null +++ b/lib/impattern.py @@ -0,0 +1,28 @@ +#!/usr/bin/python2.7 +import urllib +import urllib2 +import simplejson as json +import random +import sys +from lib.utils import post_request, Service + +IMPATTERN_URL = "http://asdf.us/im/api/impattern" + + + +class ImGradient(Service): + def __init__(self): + self.url = IMPATTERN_URL + self._required_keys = [ + "pattern_url", + "pattern_data", + "username", + "image_url", + ] + def new(self, params): + for k in self._required_keys: + if not k in params: + params[k] = ""; + self.params = params + return json.loads(post_request(IMPATTERN_URL, self.params)) + diff --git a/lib/utils.py b/lib/utils.py new file mode 100644 index 0000000..0ad9aa3 --- /dev/null +++ b/lib/utils.py @@ -0,0 +1,49 @@ +import urllib +import urllib2 +import sys +import random +def post_request(url, params): + params = urllib.urlencode(params) + headers = { + "Content-type": "application/x-www-form-urlencoded", + "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36", + "Accept": "text/plain" + } + try: + req = urllib2.Request(url, params, headers) + response = urllib2.urlopen(req) + return response.read() + except Exception as e: + sys.stderr.write(str(e)) + raise + +class Service: + def __init__(self): + self._required_keys = [] + self.url = "" + def new(self, params): + for k in self._required_keys: + if not k in params: + params[k] = ""; + self.current_params = params + return json.loads(post_request(self.url, self.current_params)) + +class Pb_Api_Params: + def _weighted_choice(self, param): + weights_total = sum(map(lambda x: x["weight"], param)) + choice = random.randint(0, weights_total) + position = 0 + for elem in param: + position += elem["weight"] + if position >= choice: + return elem["value"] + def _default_choice(self, param): + heaviest_idx = 0 + heaviest_weight = 0 + idx = 0 + for elem in param: + if elem["weight"] > heaviest_weight: + heaviest_weight = elem["weight"] + heaviest_idx = idx; + idx += 1 + return param[heaviest_idx]["value"] |
