コード例 #1
0
 def iter_me(self):
     if self._data is None:
         raise RuntimeError('No data')
     try:
         for chunk in self._data(chunk_size=self._buff_size):
             yield chunk
     except REQUEST_ERRORS as e:
         raise RuntimeErrorTrace(e)
コード例 #2
0
 def _request(self, proxy_key):
     try:
         self._rq = requests.get(self._url,
                                 params=self._params,
                                 stream=True,
                                 timeout=30,
                                 proxies=proxies(proxy_key))
     except REQUEST_ERRORS as e:
         raise RuntimeErrorTrace(e)
     self._data = self._rq.iter_content
コード例 #3
0
 def _request(self):
     try:
         response = requests.post(self.URL,
                                  json=self.__params,
                                  proxies=proxies('snowboy_training'))
     except REQUEST_ERRORS as e:
         raise RuntimeErrorTrace(e)
     if not response.ok:
         raise RuntimeError('Server error {}: {} ({})'.format(
             response.status_code, response.reason,
             pretty_errors(response.text)))
     self._data = response.iter_content()
コード例 #4
0
 def _send(self, proxy_key):
     try:
         self._rq = requests.post(
             self._url,
             data=self._chunks(),
             params=self._params,
             headers=self._headers,
             stream=True,
             timeout=60,
             proxies=proxies(proxy_key),
             **self._requests_kwargs()
         )
     except REQUEST_ERRORS as e:
         raise RuntimeErrorTrace(e)
コード例 #5
0
 def _send(self, proxy_key):
     try:
         self._rq = create_connection(
             self._url,
             timeout=60,
             **proxies(proxy_key, ws_format=True),
         )
         self._rq.send(json.dumps({'config': {'sample_rate': self.rate}}))
         for chunk in self._chunks():
             self._rq.send(chunk, opcode=ABNF.OPCODE_BINARY)
         self._rq.send('{"eof" : 1}')
     except REQUEST_ERRORS as e:
         self._close()
         raise RuntimeErrorTrace(e)
コード例 #6
0
    def _request(self, proxy_key):
        self.session.update()

        self._params['message'] = self._params.pop('text')
        try:
            self._rq = requests.post(
                self._url,
                json=self._params,
                stream=True,
                timeout=30,
                proxies=proxies(proxy_key),
                **self.session.requests_options,
            )
        except REQUEST_ERRORS as e:
            raise RuntimeErrorTrace(e)
        self._data = self._rq.iter_content
コード例 #7
0
ファイル: keys_utils.py プロジェクト: xjhsxjh/mdmTerminal2
def _yandex_get_api_key_v1():
    url = 'https://translate.yandex.com'
    target = 'SPEECHKIT_KEY:'

    try:
        response = requests.get(url, proxies=proxies('key_yandex'))
    except REQUEST_ERRORS as e:
        raise RuntimeErrorTrace(e)
    line = response.text
    if line.find('<title>Oops!</title>') > -1:
        raise RuntimeError('Yandex blocked automated requests')
    end = 0
    start = line.find(target)
    if start:
        start += len(target)
        end = line.find(',', start)
    if start and end and start < end:
        return line[start:end].strip(' \'')
    else:
        raise RuntimeError('API Key not extracted. Yandex change page?')
コード例 #8
0
ファイル: keys_utils.py プロジェクト: xjhsxjh/mdmTerminal2
def requests_post(url, key: str, **kwargs):
    try:
        reply = requests.post(url, **kwargs)
    except REQUEST_ERRORS as e:
        raise RuntimeErrorTrace(e)
    try:
        data = reply.json()
    except ValueError as e:
        if reply.ok:
            raise RuntimeError('Error json parsing: {}'.format(e))
        data = {}
    if 'error_code' in data:
        raise RuntimeError('[{}]{}: {}'.format(reply.status_code,
                                               data.get('error_code'),
                                               data.get('error_message')))
    if not reply.ok:
        raise RuntimeError('{}: {}'.format(reply.status_code, reply.reason))
    if key not in data:
        raise RuntimeError('Key \'{}\' not in reply'.format(mask_off(key)))
    return data[key]
コード例 #9
0
ファイル: keys_utils.py プロジェクト: xjhsxjh/mdmTerminal2
def _azure_token_from_oauth(key, region):
    # https://docs.microsoft.com/en-us/azure/cognitive-services/Speech-Service/rest-apis#authentication
    url = AZURE_ACCESS_ENDPOINT.format(region)
    headers = {
        'Ocp-Apim-Subscription-Key': key,
        'Content-type': 'application/x-www-form-urlencoded',
        'Content-Length': '0'
    }
    try:
        response = requests.post(url,
                                 headers=headers,
                                 proxies=proxies('token_azure'))
    except REQUEST_ERRORS as e:
        raise RuntimeErrorTrace(e)
    if not response.ok:
        raise RuntimeError('{}: {}'.format(response.status_code,
                                           response.reason))
    token = response.text
    if not token:
        raise RuntimeError('Azure send empty token')
    return token
コード例 #10
0
 def _reply_check(self):
     try:
         while True:
             try:
                 data = json.loads(self._rq.recv())
             except (json.JSONDecodeError, TypeError, BlockingIOError):
                 continue
             except Exception as e:
                 if not self._text:
                     raise RuntimeErrorTrace(e)
                 break
             if isinstance(data, dict) and 'type' in data:
                 if data['type'] == 'data':
                     try:
                         self._text = data['data']['chunks'][0]['alternatives'][0]['text']
                     except (KeyError, TypeError, IndexError):
                         pass
                 elif data['type'] in ('end', 'error'):
                     if not self._text:
                         raise RuntimeError('{}: {}'.format(data['type'].upper(), data.get('data')))
                     break
     finally:
         self.close()
コード例 #11
0
 def _send(self, proxy_key):
     time_stamp = 0
     try:
         self._rq = create_connection(
             self.URL,
             timeout=20,
             origin=self.ORIGIN,
             **proxies(proxy_key, ws_format=True),
         )
         self._rq.send(json.dumps(self._params))
         self._rq.recv()  # wait {'type': 'connect', 'data': 'Done'}
         for chunk in self._chunks():
             if chunk:
                 if not time_stamp:
                     time_stamp = time.time()
                 self._rq.send_binary(chunk)
         wait = self.MIN_SEND_TIME - (time.time() - time_stamp)
         if wait > 0:
             time.sleep(wait)
         self._rq.send_binary(b'')
     except Exception as e:
         self.close()
         raise RuntimeErrorTrace(e)