Пример #1
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._coaching:
         self.apicoach.wait()
     if not start:
         start = time() - self.HOUR
     if not end:
         end = time()
     try:
         ret = _get(
             'https://poloniex.com/public?' + _urlencode({
                 'command': 'returnTradeHistory',
                 'currencyPair': str(pair).upper(),
                 'start': str(start),
                 'end': str(end)
             }),
             timeout=self.timeout)
     except Exception as e:
         raise e
     try:
         return _loads(ret.text, parse_float=unicode)
     except NameError:
         return _loads(ret.text, parse_float=str)
Пример #2
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
     try:
         ret = _get('https://poloniex.com/public?' + _urlencode(args),
                    timeout=self.timeout)
         self.logger.debug(ret.url)
     except Exception as e:
         raise e
     if not self.jsonNums:
         try:
             return _loads(ret.text, parse_float=unicode)
         except NameError:
             return _loads(ret.text, parse_float=str)
     return _loads(ret.text,
                   parse_float=self.jsonNums,
                   parse_int=self.jsonNums)
Пример #3
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
Пример #4
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
Пример #5
0
    def _handleReturned(self, data):
        """ Handles returned data from poloniex"""
        try:
            if not self.jsonNums:
                out = _loads(data, parse_float=str)
            else:
                out = _loads(data,
                             parse_float=self.jsonNums,
                             parse_int=self.jsonNums)
        except:
            self.logger.error(data)
            raise PoloniexError('Invalid json response returned')

        # check if poloniex returned an error
        if 'error' in out:

            # update nonce if we fell behind
            if "Nonce must be greater" in out['error']:
                self._nonce = int(
                    out['error'].split('.')[0].split()[-1])
                # raise RequestException so we try again
                raise RequestException('PoloniexError ' + out['error'])

            # conncetion timeout from poloniex
            if "please try again" in out['error'].lower():
                # raise RequestException so we try again
                raise RequestException('PoloniexError ' + out['error'])

            # raise other poloniex errors, ending retry loop
            else:
                raise PoloniexError(out['error'])
        return out
Пример #6
0
 def parseJson(self, data):
     self.logger.debug(data)
     if not self.jsonNums:
         jsonout = _loads(data, parse_float=str)
     else:
         jsonout = _loads(data,
                          parse_float=self.jsonNums,
                          parse_int=self.jsonNums)
     # check if poloniex returned an error
     if 'error' in jsonout:
         raise PoloniexError(jsonout['error'])
     return jsonout
Пример #7
0
def get_launcher(out_dir=".", quiet=False):
    """
    Download latest release of Minecraft Java launcher ("launcher.jar").

    Parameters
    ----------
    out_dir: str
        "server.jar" destination directory.
    quiet: bool
        If True, does not print output.
    """
    out_file = _join(out_dir, "launcher.jar")

    from lzma import decompress

    with _urlopen(_LAUNCHER_JSON_URL) as launcher_json:
        launcher_java = _loads(launcher_json.read())["java"]

    checksum = launcher_java["sha1"]
    if _already_exists(out_file, checksum, quiet):
        return

    with _urlopen(launcher_java["lzma"]["url"]) as lzma_file:
        launcher_bytes = decompress(lzma_file.read())

    _verify_and_save(out_file, launcher_bytes, checksum, quiet)
Пример #8
0
def loads(s):
    try:
        return _loads(s)
    except Exception as e:
        # Turn the decoding exception into something with more useful
        # information.
        raise Exception("Failed to decode response %r: %s" % (s, e))
Пример #9
0
def _cookie_user2Obj(obj, cookie_name):
	tmp = obj.get_secure_cookie(cookie_name)
	try:
		tmp = _loads(tmp)
	except TypeError:
		tmp = eval(tmp) if tmp else None
	return _dict2Obj("User", **tmp) if tmp else None
Пример #10
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)
Пример #11
0
    def _on_message(self, ws, message):
        message = _loads(message)
        if 'error' in message:
            return logger.error(message['error'])

        if message[0] == 1002:
            if message[1] == 1:
                return logger.info('Subscribed to ticker')

            if message[1] == 0:
                return logger.info('Unsubscribed to ticker')

            data = message[2]
            data = [float(dat) for dat in data]
            self._tick[data[0]] = {
                'id': data[0],
                'last': data[1],
                'lowestAsk': data[2],
                'highestBid': data[3],
                'percentChange': data[4],
                'baseVolume': data[5],
                'quoteVolume': data[6],
                'isFrozen': data[7],
                'high24hr': data[8],
                'low24hr': data[9]
            }
Пример #12
0
def get_kraken_coin():
    '''
    :return: dict of coins in meta_kraken.json
    '''
    with open(f'meta_kraken.json','r') as file:
        content = file.read()
        return _loads(content)
Пример #13
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
Пример #14
0
def get_meta():
    '''
    :return: available coins and meta_info
    '''
    url = 'https://www.cryptocompare.com/api/data/coinlist/'
    rawdata = _requests.get(url).content.decode()
    parseddata = _loads(rawdata)
    return parseddata['Data']
Пример #15
0
def get_price(fromSymbols, toSymbols):
    '''
    :return: current price of fromSymbols given in toSymbols. You can concat. for example: fromSymbols='ETH,ETC', toSymbols='USD,EUR'
    '''
    url = f"{_URL_CRYPTOCOMPARE}/pricemulti?fsyms={fromSymbols}&tsyms={toSymbols}"
    rawdata = _requests.get(url).content.decode()
    parseddata = _loads(rawdata)
    return parseddata
Пример #16
0
def loads(s):
    try:
        return _loads(s)
    except Exception as e:
        # Turn the decoding exception into something with more useful
        # information.
        raise Exception(
            "Failed to decode response %r: %s" % (s, e))
Пример #17
0
def asyncClient(link, data):
    response = yield _Task(
        _AsyncHTTPClient().fetch,
        **dict(request=link,
               method='POST',
               body=data,
               expect_100_continue=True,
               request_timeout=120.0))
    raise _Return(_loads(response.body)) if response.body else _Return(None)
Пример #18
0
 def on_message(self, data):
     if not self.jsonNums:
         message = _loads(data, parse_float=str)
     else:
         message = _loads(data,
                          parse_float=self.jsonNums,
                          parse_int=self.jsonNums)
     # catch errors
     if 'error' in message:
         return self.logger.error(message['error'])
     # handle sub/unsub
     chan = self._handle_sub(message)
     if chan:
         # activate chan callback
         # heartbeats are funky
         if not chan == '1010':
             message = message[2]
         self.socket._callback(self.channels[chan]['callback'], message)
Пример #19
0
    def document_decode(cls, serialized, codec_options=None, *args, **kwargs):
        from json import loads as _loads

        opts = codec_options or cls.DEFAULT_CODEC_OPTIONS
        dcls = opts.document_class
        return _loads(
            serialized,
            object_pairs_hook=lambda pairs: cls.object_hook(dcls(pairs), opts),
        )
Пример #20
0
    def parse(cls, string, errors=False):
        try:
            rarg = _loads(str(string))
            return cls.from_collection(rarg)

        except ValueError as error:
            if errors:
                raise error
            else:
                return string
Пример #21
0
def loads(data):
    """
    Load a JSON object from a byte string.

    Raise an exception including ``data`` if the parse fails.
    """
    try:
        return _loads(data)
    except ValueError as e:
        raise ValueError("{!r}: {!r}".format(e, data))
Пример #22
0
def check_connection():
    '''Tests an internet connection. If it can connect, it records the 
    comic number of the most recent comic for later use.
    '''
    global CONNECTED, MOST_RECENT
    try:
        response, content = HTTP.request(BASE_URL)
        entry             = _loads(content.decode('utf-8'))
        MOST_RECENT       = entry['num']
    except httplib2.ServerNotFoundError:
        CONNECTED = False
Пример #23
0
 def get_extra_instructions(self):
     instructions = self.project.per_task_instructions
     properties = {}
     if self.x:
         properties['x'] = str(self.x)
     if self.y:
         properties['y'] = str(self.y)
     if self.zoom:
         properties['z'] = str(self.zoom)
     if self.extra_properties:
         properties.update(_loads(self.extra_properties))
     return instructions.format(**properties)
Пример #24
0
 def get_extra_instructions(self):
     instructions = self.project.per_task_instructions
     properties = {}
     if self.x:
         properties['x'] = str(self.x)
     if self.y:
         properties['y'] = str(self.y)
     if self.zoom:
         properties['z'] = str(self.zoom)
     if self.extra_properties:
         properties.update(_loads(self.extra_properties))
     return instructions.format(**properties)
Пример #25
0
    def _handleReturned(self, data):
        """ Handles returned data from poloniex raising exceptions if needed """
        # Cloudfare reported a bad gateway or gateway timeout error.
        if hasattr(data, 'status_code') and (
                data.status_code == 502 or data.status_code == 504
                or data.status_code in range(520, 527, 1)):
            # log and try again
            self.logger.error(data.text)
            raise RequestException('Poloniex Network Error %s' %
                                   str(data.status_code))
        try:
            if not self.jsonNums:
                out = _loads(data.text, parse_float=str)
            else:
                out = _loads(data.text,
                             parse_float=self.jsonNums,
                             parse_int=self.jsonNums)
        except:
            self.logger.error(data.text)
            raise PoloniexError('Invalid json response returned')

        # check if poloniex returned an error
        if 'error' in out:
            self.logger.error(out['error'])
            # update nonce if we fell behind
            if "Nonce must be greater" in out['error']:
                self._nonce = int(out['error'].split('.')[0].split()[-1])
                # raise RequestException so we try again
                raise RequestException('PoloniexError ' + out['error'])

            # connection timeout from poloniex
            if "please try again" in out['error'].lower():
                # raise RequestException so we try again
                raise RequestException('PoloniexError ' + out['error'])

            # raise other poloniex errors, ending retry loop
            else:
                raise PoloniexError(out['error'])
        return out
Пример #26
0
def read_json(filename):
    '''
    Reads json files

    :param filename:
        The full path to the json file
    :returns:
        Loaded json content as represented data structure
    '''

    j = read_file(filename)
    if j:
        return _loads(j)
Пример #27
0
def dl_comic(url):
    '''Downloads the comic file.'''
    global updated
    os.chdir(BASE_PATH)
    response, content = HTTP.request(url)
    entry             = _loads(content.decode('utf-8'))
    image_data        = get_image(entry['img'])
    filename          = get_filename(entry['title'], entry['num'])
    if image_data:
        os.chdir(COMIC_DIR)
        save_mouseover(entry['alt'], filename)
        write_image(image_data, filename)
        updated = True  # List displaying functions need this.
    return filename
Пример #28
0
 def on_message(self, data):
     if not self.jsonNums:
         message = _loads(data, parse_float=str)
     else:
         message = _loads(data,
                          parse_float=self.jsonNums,
                          parse_int=self.jsonNums)
     # catch errors
     if 'error' in message:
         return self.logger.error(message['error'])
     chan = self._getChannelName(str(message[0]))
     # handle sub/unsub
     # skip heartbeats
     if not chan == 'heartbeat':
         # Subscribed
         if message[1] == 1:
             self.logger.debug('Subscribed to %s', chan)
             # return False so no callback trigger
             return False
         # Unsubscribed
         if message[1] == 0:
             self.logger.debug('Unsubscribed to %s', chan)
             # return False so no callback trigger
             return False
     if 'callback' in self.channels[chan]:
         # activate chan callback
         if chan == 'heartbeat':
             # show whole heartbeat
             self.socket._callback(self.channels[chan]['callback'], message)
         elif chan in ['ticker', '24hvolume']:
             # ticker and 24hvolume dont need seq id
             message = message[2:]
         else:
             # show seq id for everything else
             message = message[1:]
         self.socket._callback(self.channels[chan]['callback'], message)
Пример #29
0
def get_server(out_dir=".", quiet=False):
    """
    Download latest release of Minecraft server ("server.jar").

    Parameters
    ----------
    out_dir: str
        "server.jar" destination directory.
    quiet: bool
        If True, does not print output.
    """
    out_file = _join(out_dir, "server.jar")

    with _urlopen(_MANIFEST_URL) as manifest_json:
        manifest = _loads(manifest_json.read())

    latest = manifest["latest"]["release"]

    for version in manifest["versions"]:
        if version["id"] == latest:
            version_json_url = version["url"]
            break
    else:
        raise RuntimeError(f"Server version {latest} not found in versions list.")

    with _urlopen(version_json_url) as version_json:
        server = _loads(version_json.read())["downloads"]["server"]

    checksum = server["sha1"]
    if _already_exists(out_file, checksum, quiet):
        return

    with _urlopen(server["url"]) as server_file:
        server_bytes = server_file.read()

    _verify_and_save(out_file, server_bytes, checksum, quiet)
Пример #30
0
 def marketTradeHist(self, pair, start=False, end=time()):
     """
     Returns public trade history for <pair>
     starting at <start> and ending at [end=time()]
     """
     if self._coaching:
         self.apicoach.wait()
     if not start:
         start = time()-self.HOUR
     try:
         ret = _get(
                 'https://poloniex.com/public?'+_urlencode({
                     'command': 'returnTradeHistory',
                     'currencyPair': str(pair).upper(),
                     'start': str(start),
                     'end': str(end)
                     }),
                 timeout=self.timeout)
     except Exception as e:
         raise e
     try:
         return _loads(ret.text, parse_float=unicode)
     except NameError:
         return _loads(ret.text, parse_float=str)
Пример #31
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)
Пример #32
0
    async def _handleReturned(self, data):
        ''' Handles returned data from musicbrainz
        data must be valid json'''

        try:
            out = _loads(data)
        except Exception as e:
            self.logger.error('in func: %s \nException: %s' % (__name__, e))
            raise
        else:
            if 'error' in out:
                err = RequestException('MusicbrainzError %s' % (out['error']))
                self.logger.error('in func: %s \nException: %s' %
                                  (__name__, err))
            return out

        return None
Пример #33
0
def get_historical_price(fromSymbol, toSymbol=_CURR, frequency='d', limit=2000):
    '''
    :param frequency: 'd', 'h' or 'm'
    :param limit: number of bars to return. I think max is 2000
    :return: get the last bars of fromSymbol in unit of fromSymbol
    '''
    url = f"{_URL_CRYPTOCOMPARE}/{FREQUENCY[frequency]}?fsym={fromSymbol}&tsym={toSymbol}&limit={limit}"  #&e={exchange}"
    rawdata = _requests.get(url).content.decode()
    parseddata = _loads(rawdata)
    try:
        df = _DataFrame(parseddata['Data'])
        if not df.empty:
            df = df.set_index('time')
            df.index = _to_datetime(df.index, unit='s')
    except Exception as inst:
        print(f"{type(inst)}: {inst}")    # the exception instance
        df = _DataFrame()
    return df
Пример #34
0
    def action(self, command=None, body=None, year=None):
        if not self.user or not self.passwd:
            raise FikenError("Username and Password needed!")
        if command in RELS:
            url = self.base_rel + self.company_Slug + '/' + command

            if year:
                url = url + '/' + year

            if self.debug_endpoint:
                print("Current url is: {}".format(url))
                print("Current body is:")
                print(body)

            if body:
                header = {
                    "Content-Type": "application/json",
                    "Accept": "application/hal+json"
                }

                ret = _post(url=url,
                            data=body,
                            headers=header,
                            auth=HTTPBasicAuth(self.user, self.passwd),
                            timeout=self.timeout)
            else:
                ret = _get(url,
                           auth=HTTPBasicAuth(self.user, self.passwd),
                           timeout=self.timeout)

            if ret.status_code != 201:
                print(ret.content)
                raise FikenError(ret.status_code)

            if body:
                headers = ret.headers
                return headers
            else:
                self.json_out = _loads(ret.text,
                                       parse_float=self.parse_float,
                                       parse_int=self.parse_int)
                return self.json_out
        else:
            raise FikenError("Invalid command")
Пример #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)
Пример #36
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)
Пример #37
0
    def get_extra_instructions(self):

        instructions = self.project.per_task_instructions

        def replace_colon(matchobj):
            return matchobj.group(0).replace(':', '_')

        instructions = re.sub('\{([^}]*)\}', replace_colon, instructions)

        properties = {}
        if self.x:
            properties['x'] = str(self.x)
        if self.y:
            properties['y'] = str(self.y)
        if self.zoom:
            properties['z'] = str(self.zoom)
        if self.extra_properties:
            extra_properties = _loads(self.extra_properties)
            for key in extra_properties:
                properties.update(
                    {key.replace(':', '_'): extra_properties[key]})
        return instructions.format(**properties)
Пример #38
0
    def get_extra_instructions(self):

        instructions = self.project.per_task_instructions

        def replace_colon(matchobj):
            return matchobj.group(0).replace(':', '_')

        instructions = re.sub('\{([^}]*)\}', replace_colon, instructions)

        properties = {}
        if self.x:
            properties['x'] = str(self.x)
        if self.y:
            properties['y'] = str(self.y)
        if self.zoom:
            properties['z'] = str(self.zoom)
        if self.extra_properties:
            extra_properties = _loads(self.extra_properties)
            for key in extra_properties:
                properties.update({
                    key.replace(':', '_'): extra_properties[key]
                })
        return instructions.format(**properties)
Пример #39
0
def asyncClient(link, data):
    response = yield _Task(
        _AsyncHTTPClient().fetch,
        **dict(request=link, method="POST", body=data, expect_100_continue=True, request_timeout=120.0)
    )
    raise _Return(_loads(response.body)) if response.body else _Return(None)
Пример #40
0
def loads(data, **kwargs):
    format = kwargs.pop(DATETIME_ARGUMENT, None) or DEFAULT_DATETIME_FORMAT
    d = _loads(data, cls=JsonPlusDecoder)
    return iteritems(d, format)
Пример #41
0
    def __call__(self, command, args={}):
        """
        Main Api Function
        - encodes and sends <command> with optional [args] to Poloniex api
        - raises 'ValueError' if an api key or secret is missing
            (and the command is 'private'), or if the <command> is not valid
        - returns decoded json api message
        """
        global PUBLIC_COMMANDS, PRIVATE_COMMANDS

        # check in with the coach
        if self._coaching:
            self.apicoach.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 ValueError("A Key and Secret needed!")
            # set nonce
            args['nonce'] = self.nonce

            try:
                # 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)
            except Exception as e:
                raise e
            finally:
                # increment nonce(no matter what)
                self.nonce += 1
            # return decoded json
            try:
                return _loads(ret.text, parse_float=unicode)
            except NameError:
                return _loads(ret.text, parse_float=str)

        # public?
        elif command in PUBLIC_COMMANDS:
            try:
                ret = _get(
                        'https://poloniex.com/public?' + _urlencode(args),
                        timeout=self.timeout)
            except Exception as e:
                raise e
            try:
                return _loads(ret.text, parse_float=unicode)
            except NameError:
                return _loads(ret.text, parse_float=str)
        else:
            raise ValueError("Invalid Command!")
Пример #42
0
def loads(s):
    return _loads(decompress(s))