diff options
| author | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-08 12:46:23 -0800 |
|---|---|---|
| committer | pepperpepperpepper <pepper@scannerjammer.com> | 2015-12-08 12:46:23 -0800 |
| commit | 0b994e3b71c2c0b463d100edd07745af565c7a59 (patch) | |
| tree | 2a7c7b5834859684400e770a22eecb62fc41592b | |
| parent | 17fff32864cf33bbec2b531a6123618acf74f3f7 (diff) | |
changed structure, added utils
| -rw-r--r-- | ricky/config.py | 6 | ||||
| -rw-r--r-- | ricky/params.py | 3 | ||||
| -rw-r--r-- | ricky/pb.py | 38 | ||||
| -rw-r--r-- | ricky/pbbreaker/__init__.py | 1 | ||||
| -rw-r--r-- | ricky/pbbreaker/params.py | 5 | ||||
| -rw-r--r-- | ricky/pbgradient/params.py | 3 | ||||
| -rw-r--r-- | ricky/pbgrid/__init__.py | 1 | ||||
| -rw-r--r-- | ricky/pbgrid/params.py | 4 | ||||
| -rwxr-xr-x | ricky/pbpattern/__init__.py | 1 | ||||
| -rw-r--r-- | ricky/pbpattern/params.py | 5 | ||||
| -rw-r--r-- | ricky/utils.py | 34 |
11 files changed, 56 insertions, 45 deletions
diff --git a/ricky/config.py b/ricky/config.py index 2040435..0cbcbb1 100644 --- a/ricky/config.py +++ b/ricky/config.py @@ -21,6 +21,12 @@ IMGRADIENT_URL = _add_pb_base("im/api/imgradient") IMBREAK_URL = _add_pb_base("im/api/imbreak") PB_DATA_URL = _add_pb_base("im/data") +# aliased for merge +PBPATTERN_URL = _add_pb_base("im/api/impattern") +PBGRID_URL = _add_pb_base("im/api/imgrid") +PBGRADIENT_URL = _add_pb_base("im/api/imgradient") +PBBREAKER_URL = _add_pb_base("im/api/imbreak") + # offline mode, true by default OFFLINE = True diff --git a/ricky/params.py b/ricky/params.py index 8724a0d..b12902a 100644 --- a/ricky/params.py +++ b/ricky/params.py @@ -9,7 +9,8 @@ from ricky.config import PROBABILITIES_DIR class Params(object): def __init__(self, *args): self._api = None - self._params = args + self._url = None + self._params = tuple(args) def __getitem__(self, name): """getter for the param by name""" diff --git a/ricky/pb.py b/ricky/pb.py index 4a94a46..456ff85 100644 --- a/ricky/pb.py +++ b/ricky/pb.py @@ -1,47 +1,15 @@ import os -import urllib -import urllib2 import sys import simplejson as json from ricky.config import OFFLINE, PB_DATA_URL +import ricky.utils as utils class Pb(object): def __init__(self): - self._required_keys = [] self.url = "" self._offline = OFFLINE - def http_request(self, 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: - if params: - req = urllib2.Request(url, params, headers) - else: - req = urllib2.Request(url, params, headers) - response = urllib2.urlopen(req) - return response.read() - except ValueError: - sys.stderr.write( - "Bad Post params or Url sent to photoblaster" - "api.\n" - ) - except urllib2.URLError: - sys.stderr.write( - "Could not complete post request to the given url:\n" + - ("URL: %s\n" % url) + - ("PARAMS: %s\n" % params) - ) - def call(self, params): if self._offline: sys.path.append("./photoblaster") @@ -56,7 +24,7 @@ class Pb(object): instance.file_s3move() return instance.file_dict() return json.loads( - self.http_request(self.url, params=params.as_dict()) + utils.http_request(self.url, params=params.as_dict()) ) def data_from_url(self, url): @@ -77,5 +45,5 @@ class Pb(object): sys.stderr.write("No usable data found in db\n") return None else: - print self.http_request("%s?newfile=%s" % (PB_DATA_URL, newfile)) + print utils.http_request("%s?newfile=%s" % (PB_DATA_URL, newfile)) raise NotImplementedError("Not yet implemented\n") diff --git a/ricky/pbbreaker/__init__.py b/ricky/pbbreaker/__init__.py index 2a1c105..e17f77f 100644 --- a/ricky/pbbreaker/__init__.py +++ b/ricky/pbbreaker/__init__.py @@ -10,6 +10,5 @@ class PbBreaker(Pb): def params_init(self): new_params = Params() - # new_params = self.get_from_server() new_params.api = self return new_params diff --git a/ricky/pbbreaker/params.py b/ricky/pbbreaker/params.py index 57c93f3..e8438b1 100644 --- a/ricky/pbbreaker/params.py +++ b/ricky/pbbreaker/params.py @@ -4,7 +4,7 @@ from ricky.param.imageurl import PbageUrl from ricky.param.enum import Enum from ricky.param.constrainednumber import ConstrainedNumber from ricky.param.bool import Bool - +from ricky.config import PBBREAKER_URL _BREAKTYPE_OPTIONS = [ "CLASSIC", @@ -33,7 +33,7 @@ _FINALFORMAT_OPTIONS = [ class Params(_Params): def __init__(self): - self._params = ( + super(Params, self).__init__( Username(name="username", required=False), PbageUrl(name="url", required=True), Enum( @@ -56,3 +56,4 @@ class Params(_Params): options=_BREAKMODE_OPTIONS), Bool(name="expanded", required=False) ) + self._url = PBBREAKER_URL diff --git a/ricky/pbgradient/params.py b/ricky/pbgradient/params.py index 48e25f5..54686ef 100644 --- a/ricky/pbgradient/params.py +++ b/ricky/pbgradient/params.py @@ -4,7 +4,7 @@ from ricky.param.enum import Enum from ricky.param.constrainednumber import ConstrainedNumber from ricky.param.bool import Bool from ricky.param.color import Color - +from ricky.config import PBGRADIENT_URL _HALFTONE_OPTIONS = [ "", @@ -164,3 +164,4 @@ class Params(_Params): required=False ) ) + self._url = PBGRADIENT_URL diff --git a/ricky/pbgrid/__init__.py b/ricky/pbgrid/__init__.py index 4189231..d94b0a1 100644 --- a/ricky/pbgrid/__init__.py +++ b/ricky/pbgrid/__init__.py @@ -9,6 +9,5 @@ class PbGrid(Pb): def params_init(self): new_params = Params() - # new_params = self.get_from_server() new_params.api = self return new_params diff --git a/ricky/pbgrid/params.py b/ricky/pbgrid/params.py index 850e180..abf0b3b 100644 --- a/ricky/pbgrid/params.py +++ b/ricky/pbgrid/params.py @@ -5,6 +5,7 @@ from ricky.param.enum import Enum from ricky.param.constrainednumber import ConstrainedNumber from ricky.param.color import Color from ricky.param.bool import Bool +from ricky.config import PBGRID_URL _TRANSITION_OPTIONS = [ "background", @@ -23,7 +24,7 @@ _FILETYPE_OPTIONS = [ class Params(_Params): def __init__(self): - self._params = ( + super(Params, self).__init__( Username(name="username", required=False), PbageUrl(name="bgimage", required=False), PbageUrl(name="imageinstead", required=False), @@ -101,6 +102,7 @@ class Params(_Params): prec=1 ) ) + self._url = PBGRID_URL def _test_values(self): return not any([ diff --git a/ricky/pbpattern/__init__.py b/ricky/pbpattern/__init__.py index fe475cb..4351713 100755 --- a/ricky/pbpattern/__init__.py +++ b/ricky/pbpattern/__init__.py @@ -9,6 +9,5 @@ class PbPattern(Pb): def params_init(self): new_params = Params() - # new_params = self.get_from_server() new_params.api = self return new_params diff --git a/ricky/pbpattern/params.py b/ricky/pbpattern/params.py index ea789cd..8c3a885 100644 --- a/ricky/pbpattern/params.py +++ b/ricky/pbpattern/params.py @@ -2,12 +2,12 @@ from ricky.params import Params as _Params from ricky.param.username import Username from ricky.param.imageurl import PbageUrl from ricky.param.enum import Enum -from ricky.config import PATTERN_URL_BASE +from ricky.config import PATTERN_URL_BASE, PBPATTERN_URL class Params(_Params): def __init__(self): - self._params = ( + super(Params, self).__init__( Username(name="username", required=False), PbageUrl(name="image_url", required=True), Enum( @@ -16,6 +16,7 @@ class Params(_Params): options=self._get_pattern_urls() ) ) + self._url = PBPATTERN_URL def _get_pattern_urls(self): return set( diff --git a/ricky/utils.py b/ricky/utils.py new file mode 100644 index 0000000..e887fa0 --- /dev/null +++ b/ricky/utils.py @@ -0,0 +1,34 @@ +import sys +import urllib +import urllib2 + + +def http_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: + if params: + req = urllib2.Request(url, params, headers) + else: + req = urllib2.Request(url, params, headers) + response = urllib2.urlopen(req) + return response.read() + except ValueError: + sys.stderr.write( + "Bad Post params or Url sent to photoblaster" + "api.\n" + ) + except urllib2.URLError: + sys.stderr.write( + "Could not complete post request to the given url:\n" + + ("URL: %s\n" % url) + + ("PARAMS: %s\n" % params) + ) |
