Ejemplo n.º 1
0
    def __call__(self, command, args={}):
        """ Main Api Function
        - encodes and sends <command> with optional [args] to Poloniex api
        - raises 'poloniex.PoloniexError' if an api key or secret is missing
            (and the command is 'private'), if the <command> is not valid, or
            if an error is returned from poloniex.com
        - returns decoded json api message """
        # get command type
        cmdType = self.checkCmd(command)

        # pass the command
        args['command'] = command
        payload = {}
        # add timeout
        payload['timeout'] = self.timeout

        # private?
        if cmdType == 'Private':
            payload['url'] = 'https://poloniex.com/tradingApi'

            # wait for coach
            if self.coach:
                self.coach.wait()

            # set nonce
            args['nonce'] = self.nonce

            # add args to payload
            payload['data'] = args

            # sign data with our Secret
            sign = _new(
                self.secret.encode('utf-8'),
                _urlencode(args).encode('utf-8'),
                _sha512)

            # add headers to payload
            payload['headers'] = {'Sign': sign.hexdigest(),
                                  'Key': self.key}

            # send the call
            ret = _post(**payload)

            # return data
            return self.handleReturned(ret.text)

        # public?
        if cmdType == 'Public':
            # encode url
            payload['url'] = 'https://poloniex.com/public?' + _urlencode(args)

            # wait for coach
            if self.coach:
                self.coach.wait()

            # send the call
            ret = _get(**payload)

            # return data
            return self.handleReturned(ret.text)
Ejemplo n.º 2
0
def cancel_order_huobi(key, order_id):
    HUOBI_CANCEL_PATH = HUOBI_CANCEL_ORDER + str(order_id) + "/submitcancel"
    final_url = HUOBI_API_URL + HUOBI_CANCEL_PATH + "?"

    body = init_body(key)

    message = _urlencode(body).encode('utf8')

    msg = "POST\n{base_url}\n{path}\n{msg1}".format(base_url=HUOBI_API_ONLY,
                                                    path=HUOBI_CANCEL_PATH,
                                                    msg1=message)

    signature = sign_string_256_base64(key.secret, msg)

    body.append(("Signature", signature))

    final_url += _urlencode(body).encode('utf8')

    body = {}

    post_details = PostRequestDetails(final_url, HUOBI_POST_HEADERS, body)

    if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP:
        msg = "cancel_order_huobi: url - {url} headers - {headers} body - {body}".format(
            url=final_url, headers=HUOBI_POST_HEADERS, body=body)
        print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP)
        log_to_file(msg, "market_utils.log")

    err_msg = "cancel huobi order with id {id}".format(id=order_id)

    return send_post_request_with_logging(post_details, err_msg)
Ejemplo n.º 3
0
    def urlencode(query, doseq=False, safe='', encoding=None, errors=None):
        """
        common urlencode function

        """
        try:
            # Python v3.x
            return _urlencode(
                query, doseq=doseq, safe=safe, encoding=encoding,
                errors=errors)

        except TypeError:
            # Python v2.7
            return _urlencode(query)
Ejemplo n.º 4
0
    def unsubscribe(self, chan):
        """ Sends the 'unsubscribe' command for <chan> """
        # account chan?
        if chan in ['1000', 1000]:
            # sending commands to 'account' requires a key, secret and nonce
            if not self.key or not self.secret:
                raise PoloniexError(
                    "self.key and self.secret needed for 'account' channel")
            payload = {'nonce': self.nonce}
            payload_encoded = _urlencode(payload)
            sign = _new(self.secret.encode('utf-8'),
                        payload_encoded.encode('utf-8'), _sha512)

            self.socket.send(
                _dumps({
                    'command': 'unsubscribe',
                    'channel': chan,
                    'sign': sign.hexdigest(),
                    'key': self.key,
                    'payload': payload_encoded
                }))
        else:
            self.socket.send(
                _dumps({
                    'command': 'unsubscribe',
                    'channel': chan
                }))
Ejemplo n.º 5
0
    def __call__(self, route, args={}):
        """
        Main Api Function
        - raises 'cryptocompare.CryptoCompareError' if the <route> is not valid, or
            if an error is returned from Cryptocompare API
        - returns decoded json api message
        """
        if route in API_URL_ROUTES:
            url = self.api_url
            if route in MAP_ROUTES:
                url += MAP_ROUTES[route]
            else:
                url += route
        elif route in WEB_URL_ROUTES:
            url = self.web_url + route
        else:
            raise CryptoCompareError("Invalid Command!: %s" % command)

        ret = _get(url + "?" + _urlencode(args), timeout=self.timeout)

        jsonout = _loads(ret.text,
                         parse_float=self.parse_float,
                         parse_int=self.parse_int)

        if "Response" in jsonout:
            if jsonout["Response"] == "Success":
                return jsonout
            else:
                raise CryptoCompareError(jsonout["Message"])
        return jsonout
Ejemplo n.º 6
0
 def marketTradeHist(self, pair, start=False, end=False):
     """
     Returns public trade history for <pair>
     starting at <start> and ending at [end=time()]
     """
     if self.coach:
         self.coach.wait()
     args = {
         'command': 'returnTradeHistory',
         'currencyPair': str(pair).upper()
     }
     if start:
         args['start'] = start
     if end:
         args['end'] = end
     ret = _get('https://poloniex.com/public?' + _urlencode(args),
                timeout=self.timeout)
     # decode json
     if not self.jsonNums:
         jsonout = _loads(ret.text, parse_float=str)
     else:
         jsonout = _loads(ret.text,
                          parse_float=self.jsonNums,
                          parse_int=self.jsonNums)
     # check if poloniex returned an error
     if 'error' in jsonout:
         raise PoloniexError(jsonout['error'])
     return jsonout
Ejemplo n.º 7
0
def private_order(command, args={}):
    global API_key, Secret

    err = True
    while err:
        try:
            args['command'] = command
            args['nonce'] = nonce()
            postData = _urlencode(args)

            sign = _new(Secret.encode('utf-8'), postData.encode('utf-8'),
                        _sha512)

            ret = _post('https://poloniex.com/tradingApi',
                        data=args,
                        headers={
                            'Sign': sign.hexdigest(),
                            'Key': API_key
                        })

            return _loads(ret.text, parse_float=str)
        except KeyboardInterrupt:
            exit()
        except Exception:
            print(ret)
            print("### ERROR INESPERADO TIPO:", sys.exc_info()[1])
            print('### ERROR AL EJECUTAR PRIVATE ORDER ' + command + ' ###')
            print('### ESPERANDO 30 SEGUNDOS ###')
            time.sleep(30)
Ejemplo n.º 8
0
    def unsubscribe(self, chan):
        """ Sends the 'unsubscribe' command for <chan> """
        if isinstance(chan, int):
            chan = self._getChannelName(str(chan))
        # account chan?
        if chan == 'account':
            # sending commands to 'account' requires a key, secret and nonce
            if not self.key or not self.secret:
                raise PoloniexError(
                    "self.key and self.secret needed for 'account' channel")
            self.channels[chan]['sub'] = False
            payload = {'nonce': self.nonce}
            payload_encoded = _urlencode(payload)
            sign = _new(self.secret.encode('utf-8'),
                        payload_encoded.encode('utf-8'), _sha512)

            self.socket.send(
                _dumps({
                    'command': 'unsubscribe',
                    'channel': self.channels[chan]['id'],
                    'sign': sign.hexdigest(),
                    'key': self.key,
                    'payload': payload_encoded
                }))
        else:
            self.channels[chan]['sub'] = False
            self.socket.send(
                _dumps({
                    'command': 'unsubscribe',
                    'channel': self.channels[chan]['id']
                }))
Ejemplo n.º 9
0
def cancel_order_bittrex(key, order_id):
    # https://bittrex.com/api/v1.1/market/cancel?apikey=API_KEY&uuid=ORDER_UUID
    final_url = BITTREX_CANCEL_ORDER + key.api_key + "&nonce=" + str(
        generate_nonce())

    body = {
        "uuid": order_id,
    }

    final_url += _urlencode(body)

    headers = {"apisign": signed_string(final_url, key.secret)}

    post_details = PostRequestDetails(final_url, headers, body)

    if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP:
        msg = "cancel_order_bittrex: {res}".format(res=post_details)
        print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP)
        log_to_file(msg, "market_utils.log")

    err_msg = "cancel bittrex order with id {id}".format(id=order_id)

    res = send_get_request_with_header(post_details.final_url,
                                       post_details.headers,
                                       err_msg,
                                       timeout=BITTREX_DEAL_TIMEOUT)

    if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP:
        print_to_console(res, LOG_ALL_MARKET_RELATED_CRAP)
        log_to_file(res, "market_utils.log")

    return res
Ejemplo n.º 10
0
 def marketTradeHist(self, currencyPair, start=False, end=False):
     """ Returns the past 200 trades for a given market, or up to 50,000
     trades between a range specified in UNIX timestamps by the "start" and
     "end" parameters. """
     if self.coach:
         self.coach.wait()
     args = {
         'command': 'returnTradeHistory',
         'currencyPair': str(currencyPair).upper()
     }
     if start:
         args['start'] = start
     if end:
         args['end'] = end
     ret = _get('https://poloniex.com/public?' + _urlencode(args),
                timeout=self.timeout)
     # decode json
     if not self.jsonNums:
         jsonout = _loads(ret.text, parse_float=str)
     else:
         jsonout = _loads(ret.text,
                          parse_float=self.jsonNums,
                          parse_int=self.jsonNums)
     # check if poloniex returned an error
     if 'error' in jsonout:
         raise PoloniexError(jsonout['error'])
     return jsonout
Ejemplo n.º 11
0
 def urlencode(query):
     # Dict[str, str] -> str
     return py2_decode(
         _urlencode({
             py2_encode(param): py2_encode(arg)
             for param, arg in query.items()
         }))
Ejemplo n.º 12
0
def urlencode(string, encoding):
    try:
        from urllib import quote as _urlencode
    except ImportError:
        from urllib.parse import quote as _urlencode

    return _urlencode(string.encode(encoding))
Ejemplo n.º 13
0
def urlencode(params):
    utf8_params = encode_params_utf8(params)
    urlencoded = _urlencode(utf8_params)
    if isinstance(urlencoded, unicode_type):  # PY3 returns unicode
        return urlencoded
    else:
        return urlencoded.decode("utf-8")
Ejemplo n.º 14
0
    def urlencode(query, doseq=0, safe='', encoding=None, errors=None):

        if encoding is None:
            encoding = 'utf-8'
        if errors is None:
            errors = 'strict'

        def encode_value(v):
            try:
                return codecs.encode(v, encoding, errors)
            except UnicodeError:
                raise
            except (AttributeError, TypeError):
                return str(v)

        try:
            quoted = []
            for name, value in query:
                quoted.append((encode_value(name), encode_value(value)))
            query = quoted
        except UnicodeError:
            raise
        except (TypeError, ValueError):  # pragma no cover
            # doesn't look like a sequence of tuples, maybe a dict?
            try:
                quoted = {}
                for name, value in query.items():
                    quoted[encode_value(name)] = encode_value(value)
                query = quoted
            except AttributeError:  # not a dictionary either
                pass

        return _urlencode(query, doseq=doseq)
Ejemplo n.º 15
0
def urlencode(params):
    utf8_params = encode_params_utf8(params)
    urlencoded = _urlencode(utf8_params)
    if isinstance(urlencoded, unicode_type):  # PY3 returns unicode
        return urlencoded
    else:
        return urlencoded.decode("utf-8")
Ejemplo n.º 16
0
    def __call__(self, command, args={}):
        """ Main Api Function
        - encodes and sends <command> with optional [args] to Poloniex api
        - raises 'poloniex.PoloniexError' if an api key or secret is missing
            (and the command is 'private'), if the <command> is not valid, or
            if an error is returned from poloniex.com
        - returns decoded json api message """
        global PUBLIC_COMMANDS, PRIVATE_COMMANDS

        # check in with the coach
        if self.coach:
            self.coach.wait()

        # pass the command
        args['command'] = command

        # private?
        if command in PRIVATE_COMMANDS:
            # check for keys
            if not self.key or not self.secret:
                raise PoloniexError("An Api Key and Secret needed!")
            # set nonce
            args['nonce'] = self.nonce
            # encode arguments for url
            postData = _urlencode(args)
            # sign postData with our Secret
            sign = _new(self.secret.encode('utf-8'), postData.encode('utf-8'),
                        _sha512)
            # post request
            ret = _post('https://poloniex.com/tradingApi',
                        data=args,
                        headers={
                            'Sign': sign.hexdigest(),
                            'Key': self.key
                        },
                        timeout=self.timeout)
            # decode json
            return self.parseJson(ret.text)

        # public?
        elif command in PUBLIC_COMMANDS:
            ret = _get('https://poloniex.com/public?' + _urlencode(args),
                       timeout=self.timeout)
            # decode json
            return self.parseJson(ret.text)
        else:
            raise PoloniexError("Invalid Command!: %s" % command)
Ejemplo n.º 17
0
def generate_body_and_url_get_request(key, base_url, path):
    body = [('AccessKeyId', key.api_key), ('SignatureMethod', 'HmacSHA256'),
            ('SignatureVersion', 2),
            ('Timestamp',
             ts_to_string_utc(get_now_seconds_utc(), '%Y-%m-%dT%H:%M:%S'))]

    message = _urlencode(body).encode('utf8')

    msg = "GET\n{base_url}\n{path}\n{msg1}".format(base_url=base_url,
                                                   path=path,
                                                   msg1=message)

    signature = sign_string_256_base64(key.secret, msg)

    body.append(("Signature", signature))

    return body, _urlencode(body)
Ejemplo n.º 18
0
    def urlencode(d, *args, **kwargs):
        if hasattr(d, 'items'):
            d = list(d.items())
        else:
            d = list(d)

        d = [(_reencode(k), _reencode(v)) for k, v in d]

        return _urlencode(d, *args, **kwargs)
Ejemplo n.º 19
0
    def urlencode(d, *args, **kwargs):
        if hasattr(d, 'items'):
            d = list(d.items())
        else:
            d = list(d)

        d = [(_reencode(k), _reencode(v)) for k, v in d]

        return _urlencode(d, *args, **kwargs)
Ejemplo n.º 20
0
def sendsms2sedes(cls):
    link, data = '{host}:{port}/sendsms'.format(
        **cls.application.slave_server), _urlencode(
            dict(msg=cls.msg.tenor, personas=cls.persons2Json()))
    print link
    response = yield asyncClient(link=link, data=data)
    if response:
        for id_per in cls.personas:
            _agendasCrt.save(persona=id_per, mensaje=cls.msg.id_msj)
    else:
        _messagesCrt.delete(id_msj=cls.msg.id_msj, id_user=cls.current_user.id)
Ejemplo n.º 21
0
    def __call__(self, command, args={}, api={}):
        global PUBLIC_COMMANDS, PRIVATE_COMMANDS
        args['action'] = command
        '/index.php?page=api'
        api['api_key'] = self.key
        if command in PRIVATE_COMMANDS:
            if not self.url or not self.key:
                raise mposextractorError(
                    "url and an api key is required for this command!")

            ret = _get(self.url + '/index.php?page=api&' + _urlencode(args) +
                       "&" + _urlencode(api))
            return ret.text

        elif command in PUBLIC_COMMANDS:
            ret = _get('https://etn.suprnova.cc/index.php?page=api&' +
                       _urlencode(args))
            return ret.text
        else:
            raise mposextractorError("Invalid Command!")
Ejemplo n.º 22
0
def deleteAgendasOnSedes(cls, agendas=list()):
    #print cls.application.slave_server
    print '{}: {}'.format(cls.__class__.__name__, agendas)
    if isinstance(agendas, list) and len(agendas):
        link, data = '{host}:{port}/delete_agendas'.format(
            **cls.application.slave_server), _urlencode(dict(agendas=agendas))
        response = yield asyncClient(link=link, data=data)
        if response:
            print 'Agendas has been delete on SEDES'
        else:
            print 'Lost connection with SEDES'
Ejemplo n.º 23
0
    def urlencode(query, doseq=False, safe='', encoding=None, errors=None):
        """Convert a mapping object or a sequence of two-element tuples

        Wrapper to Python's unquote while remaining compatible with both
        Python 2 & 3 since the reference to this function changed between
        versions.

        The resulting string is a series of key=value pairs separated by '&'
        characters, where both key and value are quoted using the quote()
        function.

        Note: If the dictionary entry contains an entry that is set to None
              it is not included in the final result set. If you want to
              pass in an empty variable, set it to an empty string.

        Args:
            query (dict): The dictionary to encode
            doseq (:obj:`bool`, optional): Handle sequences
            safe (:obj:`str`): non-ascii characters and URI specific ones that
                you do not wish to escape (if detected). Setting this string
                to an empty one causes everything to be escaped.
            encoding (:obj:`str`, optional): encoding type
            errors (:obj:`str`, errors): how to handle invalid character found
                in encoded string (defined by encoding)

        Returns:
            str: The escaped parameters returned as a string
        """
        # Tidy query by eliminating any records set to None
        _query = {k: v for (k, v) in iteritems(query) if v is not None}
        try:
            # Python v3.x
            return _urlencode(_query,
                              doseq=doseq,
                              safe=safe,
                              encoding=encoding,
                              errors=errors)

        except TypeError:
            # Python v2.7
            return _urlencode(_query)
Ejemplo n.º 24
0
def sendsms2sedes(cls):
    link, data = (
        "{host}:{port}/sendsms".format(**cls.application.slave_server),
        _urlencode(dict(msg=cls.msg.tenor, personas=cls.persons2Json())),
    )
    print link
    response = yield asyncClient(link=link, data=data)
    if response:
        for id_per in cls.personas:
            _agendasCrt.save(persona=id_per, mensaje=cls.msg.id_msj)
    else:
        _messagesCrt.delete(id_msj=cls.msg.id_msj, id_user=cls.current_user.id)
Ejemplo n.º 25
0
def generate_post_request(final_url, body, key):
    signature = signed_body_256(body, key.secret)

    body["signature"] = signature

    final_url += _urlencode(body)

    headers = {"X-MBX-APIKEY": key.api_key}

    # Yeah, body after that should be empty
    body = {}

    return PostRequestDetails(final_url, headers, body)
Ejemplo n.º 26
0
def deleteAgendasOnSedes(cls, agendas=list()):
    # print cls.application.slave_server
    print "{}: {}".format(cls.__class__.__name__, agendas)
    if isinstance(agendas, list) and len(agendas):
        link, data = (
            "{host}:{port}/delete_agendas".format(**cls.application.slave_server),
            _urlencode(dict(agendas=agendas)),
        )
        response = yield asyncClient(link=link, data=data)
        if response:
            print "Agendas has been delete on SEDES"
        else:
            print "Lost connection with SEDES"
Ejemplo n.º 27
0
def capes_for(department_form_val):
    form, select = _search_form_and_select_tag()
    field_name = select.get(NAME)
    # method = form.get(HTTP_METHOD)
    # if method != HTTP_GET:
        # raise ValueError("Expected GET form submission method; Got "+repr(method))
    action = form.get(ACTION)
    dest_url = _urljoin(CAPE_SEARCH_URL, action)
    dept_url = "%s?%s" % (dest_url, _urlencode({field_name:department_form_val}))
    tree = url2tree(dept_url)
    for link in section_links(tree):
        cape = parse_detailed_page(link)
        if cape is not None:
            yield cape
Ejemplo n.º 28
0
def capes_for(department_form_val):
    form, select = _search_form_and_select_tag()
    field_name = select.get(NAME)
    # method = form.get(HTTP_METHOD)
    # if method != HTTP_GET:
    # raise ValueError("Expected GET form submission method; Got "+repr(method))
    action = form.get(ACTION)
    dest_url = _urljoin(CAPE_SEARCH_URL, action)
    dept_url = "%s?%s" % (dest_url,
                          _urlencode({field_name: department_form_val}))
    tree = url2tree(dept_url)
    for link in section_links(tree):
        cape = parse_detailed_page(link)
        if cape is not None:
            yield cape
Ejemplo n.º 29
0
def get_balance_bittrex_post_details(key):
    final_url = BITTREX_CHECK_BALANCE + key.api_key + "&nonce=" + str(
        generate_nonce())

    body = {}

    final_url += _urlencode(body)

    headers = {"apisign": signed_string(final_url, key.secret)}

    res = PostRequestDetails(final_url, headers, body)

    if should_print_debug():
        print_to_console(res, LOG_ALL_MARKET_NETWORK_RELATED_CRAP)

    return res
Ejemplo n.º 30
0
def public_order(command, args={}):

    err = True
    while err:
        try:
            args['command'] = command
            ret = _get('https://poloniex.com/public?' + _urlencode(args))
            return _loads(ret.text, parse_float=str)
        except KeyboardInterrupt:
            exit()
        except Exception:
            print(ret)
            print("### ERROR INESPERADO TIPO:", sys.exc_info()[1])
            print('### ERROR AL EJECUTAR PUBLIC ORDER ' + command + ' ###')
            print('### ESPERANDO 30 SEGUNDOS ###')
            time.sleep(30)
Ejemplo n.º 31
0
 def marketTradeHist(self, currencyPair, start=False, end=False):
     """ Returns the past 200 trades for a given market, or up to 50,000
     trades between a range specified in UNIX timestamps by the "start" and
     "end" parameters. """
     if self.coach:
         self.coach.wait()
     args = {'command': 'returnTradeHistory',
             'currencyPair': str(currencyPair).upper()}
     if start:
         args['start'] = start
     if end:
         args['end'] = end
     ret = _get(
         'https://poloniex.com/public?' + _urlencode(args),
         timeout=self.timeout)
     # decode json
     return self.handleReturned(ret.text)
Ejemplo n.º 32
0
    def analyze(self,
                url,
                locale=LOCALE_ENGLISH_US,
                strategy=STRATEGY_DESKTOP,
                rules=None,
                connect_timeout=_GLOBAL_DEFAULT_TIMEOUT):
        """Makes a request to the Insights API to analyze the specified URL.
           The optional locale specifies the locale for the results (defaults
           to 'en_US').  The optional strategy specifies whether to analyze the
           page from a 'desktop' or 'mobile' perspective.  Use the optional
           'rules' argument to specify the list of rules to use in analysis (by
           default, all rules are run)."""

        # Construct URL
        query_data = {
            "key": [self.__key],
            "locale": [locale],
            "prettyprint": ["false"],
            "strategy": [strategy],
            "url": [url]
        }
        if rules is not None:
            query_data["rule"] = rules
        user_ip = self.__user_ip
        if user_ip is not None:
            query_data["userIp"] = [user_ip]
        path = "%s?%s" % (self.__URL_PATH, _urlencode(query_data, True))

        # Perform analysis
        conn = _HTTPSConnection(self.__URL_DOMAIN, timeout=connect_timeout)
        try:
            conn.request("GET", path)
            response = conn.getresponse()
            raw_data = response.read()
            status = response.status
        finally:
            conn.close()

        # Decode the response and handle the result
        data = _loads(raw_data)
        if status != 200:
            raise _Error(data)
        return _Analysis(data)
Ejemplo n.º 33
0
def sign_kraken(body, urlpath, secret):
    """ Sign request data according to Kraken's scheme.
    :param body: API request parameters
    :type body: dict
    :param urlpath: API URL path sans host
    :type urlpath: str
    :returns: signature digest
    """

    postdata = _urlencode(body)

    # Unicode-objects must be encoded before hashing
    encoded = (str(body['nonce']) + postdata).encode()
    message = urlpath.encode() + hashlib.sha256(encoded).digest()

    signature = hmac.new(base64.b64decode(secret), message, hashlib.sha512)
    sigdigest = base64.b64encode(signature.digest())

    return sigdigest.decode()
Ejemplo n.º 34
0
def urlencode(params):
    """Urlencode a multidimensional dict."""

    # Not doing duck typing here. Will make debugging easier.
    if not isinstance(params, dict):
        raise TypeError("Only dicts are supported.")

    params = flatten(params)

    url_params = {}
    for param in params:
        value = param.pop()

        name = parametrize(param)
        if isinstance(value, (list, tuple)):
            name += "[]"

        url_params[name] = value

    return _urlencode(url_params, doseq=True)
Ejemplo n.º 35
0
    def analyze(self, url, locale=LOCALE_ENGLISH_US, strategy=STRATEGY_DESKTOP,
                rules=None, connect_timeout=_GLOBAL_DEFAULT_TIMEOUT):

        """Makes a request to the Insights API to analyze the specified URL.
           The optional locale specifies the locale for the results (defaults
           to 'en_US').  The optional strategy specifies whether to analyze the
           page from a 'desktop' or 'mobile' perspective.  Use the optional
           'rules' argument to specify the list of rules to use in analysis (by
           default, all rules are run)."""

        # Construct URL
        query_data = {
            "key": [self.__key],
            "locale": [locale],
            "prettyprint": ["false"],
            "strategy": [strategy],
            "url": [url]
        }
        if rules is not None:
            query_data["rule"] = rules
        user_ip = self.__user_ip
        if user_ip is not None:
            query_data["userIp"] = [user_ip]
        path = "%s?%s" % (self.__URL_PATH, _urlencode(query_data, True))

        # Perform analysis
        conn = _HTTPSConnection(self.__URL_DOMAIN, timeout=connect_timeout)
        try:
            conn.request("GET", path)
            response = conn.getresponse()
            raw_data = response.read()
            status = response.status
        finally:
            conn.close()

        # Decode the response and handle the result
        data = _loads(raw_data)
        if status != 200:
            raise _Error(data)
        return _Analysis(data)
Ejemplo n.º 36
0
def add_sell_order_bittrex_url(key, pair_name, price, amount):
    # https://bittrex.com/api/v1.1/market/selllimit?apikey=API_KEY&market=BTC-LTC&quantity=1.2&rate=1.3
    final_url = BITTREX_SELL_ORDER + key.api_key + "&nonce=" + str(generate_nonce())

    body = {
        "market": pair_name,
        "quantity": float_to_str(amount),
        "rate": float_to_str(price)
    }

    final_url += _urlencode(body)

    headers = {"apisign": signed_string(final_url, key.secret)}

    res = PostRequestDetails(final_url, headers, body)

    if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP:
        msg = "add_sell_order_bittrex: {res}".format(res=res)
        print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP)
        log_to_file(msg, "market_utils.log")

    return res
Ejemplo n.º 37
0
def get_order_history_bittrex_post_details(key, pair_name):
    final_url = BITTREX_GET_TRADE_HISTORY + key.api_key + "&nonce=" + str(
        generate_nonce())

    if pair_name != "all":
        body = {"market": pair_name}
    else:
        body = {}

    final_url += _urlencode(body)

    headers = {"apisign": signed_string(final_url, key.secret)}

    post_details = PostRequestDetails(final_url, headers, body)

    if get_logging_level() >= LOG_ALL_MARKET_RELATED_CRAP:
        msg = "get_order_history_bittrex_post_details: {res}".format(
            res=post_details)
        print_to_console(msg, LOG_ALL_MARKET_RELATED_CRAP)
        log_to_file(msg, "market_utils.log")

    return post_details
Ejemplo n.º 38
0
def urlencode(query, doseq=True):
    if not doseq:
        return _urlencode(query, doseq)
    if hasattr(query, 'items'):
        query = query.items()
    return _urlencode([(k,_stringify(v)) for k,v in query], doseq)
Ejemplo n.º 39
0
def url(path="", qs="", script_name=None, base=None, relative=None):
    """Create an absolute URL for the given path.
    
    If 'path' starts with a slash ('/'), this will return
        (base + script_name + path + qs).
    If it does not start with a slash, this returns
        (base + script_name [+ request.path_info] + path + qs).
    
    If script_name is None, cherrypy.request will be used
    to find a script_name, if available.
    
    If base is None, cherrypy.request.base will be used (if available).
    Note that you can use cherrypy.tools.proxy to change this.
    
    Finally, note that this function can be used to obtain an absolute URL
    for the current request path (minus the querystring) by passing no args.
    If you call url(qs=cherrypy.request.query_string), you should get the
    original browser URL (assuming no internal redirections).
    
    If relative is None or not provided, request.app.relative_urls will
    be used (if available, else False). If False, the output will be an
    absolute URL (including the scheme, host, vhost, and script_name).
    If True, the output will instead be a URL that is relative to the
    current request path, perhaps including '..' atoms. If relative is
    the string 'server', the output will instead be a URL that is
    relative to the server root; i.e., it will start with a slash.
    """
    if isinstance(qs, (tuple, list, dict)):
        qs = _urlencode(qs)
    if qs:
        qs = '?' + qs
    
    if request.app:
        if not path.startswith("/"):
            # Append/remove trailing slash from path_info as needed
            # (this is to support mistyped URL's without redirecting;
            # if you want to redirect, use tools.trailing_slash).
            pi = request.path_info
            if request.is_index is True:
                if not pi.endswith('/'):
                    pi = pi + '/'
            elif request.is_index is False:
                if pi.endswith('/') and pi != '/':
                    pi = pi[:-1]
            
            if path == "":
                path = pi
            else:
                path = _urljoin(pi, path)
        
        if script_name is None:
            script_name = request.script_name
        if base is None:
            base = request.base
        
        newurl = base + script_name + path + qs
    else:
        # No request.app (we're being called outside a request).
        # We'll have to guess the base from server.* attributes.
        # This will produce very different results from the above
        # if you're using vhosts or tools.proxy.
        if base is None:
            base = server.base()
        
        path = (script_name or "") + path
        newurl = base + path + qs
    
    if './' in newurl:
        # Normalize the URL by removing ./ and ../
        atoms = []
        for atom in newurl.split('/'):
            if atom == '.':
                pass
            elif atom == '..':
                atoms.pop()
            else:
                atoms.append(atom)
        newurl = '/'.join(atoms)
    
    # At this point, we should have a fully-qualified absolute URL.
    
    if relative is None:
        relative = getattr(request.app, "relative_urls", False)
    
    # See http://www.ietf.org/rfc/rfc2396.txt
    if relative == 'server':
        # "A relative reference beginning with a single slash character is
        # termed an absolute-path reference, as defined by <abs_path>..."
        # This is also sometimes called "server-relative".
        newurl = '/' + '/'.join(newurl.split('/', 3)[3:])
    elif relative:
        # "A relative reference that does not begin with a scheme name
        # or a slash character is termed a relative-path reference."
        old = url().split('/')[:-1]
        new = newurl.split('/')
        while old and new:
            a, b = old[0], new[0]
            if a != b:
                break
            old.pop(0)
            new.pop(0)
        new = (['..'] * len(old)) + new
        newurl = '/'.join(new)
    
    return newurl
Ejemplo n.º 40
0
    def __init__(self,
                 method,
                 uri,
                 args=None,
                 argtypes=None,
                 args_as=None,
                 accept_mediaranges=('application/json', '*/*; q=0.01'),
                 auth=None,
                 authtype='basic',
                 origin=None,
                 headers=None,
                 **kwargs):

        method_kwargs = {}

        headers = _odict(headers or ())
        method_kwargs['headers'] = headers
        if origin is not None:
            headers['Origin'] = origin
        headers.setdefault('Accept', ', '.join(accept_mediaranges))

        if args:
            if argtypes:
                untyped_args = [arg for arg in args if arg not in argtypes]
            else:
                untyped_args = args.keys()
            if untyped_args:
                raise ValueError('missing type specifications for request'
                                  ' arguments {}'.format(untyped_args))

            if args_as is None:
                if _http.method_defines_body_semantics(method.upper()):
                    args_as = 'body'
                else:
                    args_as = 'query'

            if args_as == 'query':
                args_json = {name: argtypes[name](value).json()
                             for name, value in args.items()}
                method_kwargs['params'] = args_json

            elif args_as == 'body':
                args_webobjects = {name: argtypes[name](value)
                                   for name, value in args.items()}
                body = _webtypes.json_dumps(args_webobjects)
                method_kwargs['data'] = body

                headers['Content-Type'] = 'application/json'

            elif args_as == 'body_urlencoded':
                args_json = {name: argtypes[name](value).json()
                             for name, value in args.items()}
                body = _urlencode(args_json)
                method_kwargs['data'] = body

                headers['Content-Type'] = 'application/x-www-form-urlencoded'

            else:
                raise ValueError('invalid argument mechanism {!r}; expected'
                                  ' one of {}'
                                  .format(args_as,
                                          ('query', 'body',
                                           'body_urlencoded')))

        if auth is not None:
            try:
                auth_class = _requests_auth_classmap[authtype]
            except KeyError:
                raise ValueError('invalid authentication type {!r}; expected'
                                  ' one of {}'
                                  .format(authtype, ('basic', 'digest')))
            method_kwargs['auth'] = auth_class(*auth)

        method_kwargs.update(kwargs)

        super(WebRequest, self).__init__(method, uri, **method_kwargs)
Ejemplo n.º 41
0
def _make_url(*dirs, **params):
    """ dirs must already be url-encoded, params must not """
    url = '/'.join(dirs)
    if params:
        url += '?' + _urlencode(params)
    return url