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)
def login(self, username: str, password: str) -> bool: ''' Logs into Brickset as a user, returning a userhash, which can be used in other methods. The userhash is stored inside the client. :param str username: Your Brickset username. :param str password: Your Brickset password. :returns: A boolean value of whether or not the login request was done properly. :rtype: `bool` :raises brickfront.errors.InvalidRequest: If the site doesn't like the sent request. ''' values = { 'apiKey': self._apiKey, 'username': username, 'password': password } returned = _get(self._getURL('login', values)) # Make sure all is well self._isOkayRequest(returned) root = _ET.fromstring(returned.text) if root.text.startswith('ERROR'): raise _InvalidLogin(root.text[7:]) return False # SHould be unnecessary but just in case self._userHash = root.text return True
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
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)
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
def get_ticker(): resp = _get( environ.get('POLONIEX_URL') + environ.get('POLONIEX_PUBLIC') + 'returnTicker') return Response(json.loads(resp.text), resp.status_code, mimetype='application/json')
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)
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
def get_project_databases(self, project_id, folder=".", scenario_id=None): """ Download project databases. Databases will be downloaded into folder/project_id.zip For larger projects it is recommended to defined the scenario_id to be downloaded. Otherwise the download might fail :param token: access token :param project_id: project id :param folder: folder :param scenario_id: scenario_id :type token: str :type project_id: int :type folder: str :type scenario_id: int """ if scenario_id: r = self._get( f"{self.api_url}/projects/{project_id}/data?scenario={scenario_id}" ) else: r = requests._get(f"{self.api_url}/projects/{project_id}/data") if r.status_code == 200: if scenario_id: open(f"{folder}/{project_id}-{scenario_id}.zip", 'wb').write(r.content) else: open(f"{folder}/{project_id}.zip", 'wb').write(r.content) return raise Exception( f"Something went wrong while downloading the folder {r.status_code} {r.json()}" )
def get(*args, **kwargs) -> Union[MockedResponse, Response]: # type: ignore try: response = _get(*args, **kwargs) if response.status_code == 200: return response else: return MockedResponse() except (gaierror, IOError): return MockedResponse()
def grab_image(product_image_url): response = _get(product_image_url) filename = '{}.jpg'.format(generate_random_string()) if response.status_code == 200: with open(filename, 'wb') as file: file.write(response.content) file.close() return filename else: return None
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!")
def down(e): with pools: downFilename = key + code[e] if judgeFile(downFilename, nameList) == -1: return 0 try: downloadUrl = processor(code[e]) extensions.append(downloadUrl[len(downloadUrl) - 4:len(downloadUrl)]) with open('temp\\{}.temp'.format(downFilename), 'wb') as f: f.write(_get(downloadUrl, verify=False).content) except: pass
def getFirstHref(url): oral = _get(url) oral.encoding = oral.apparent_encoding capitalList = _Bs(oral.text, 'html.parser').find_all('ul', id="chapterList")[0] capitalList = _Bs(str(capitalList), 'html.parser').find_all('a') nameList = [each.text for each in capitalList if each] FirstHref = capitalList[0].get('href') bookName = _Bs(oral.text, 'html.parser').find_all( 'meta', property="og:novel:book_name")[0].get('content') + '.txt' print('Get book information success!') return nameList, FirstHref, bookName
def baixar_views() -> str: """ faz o download das views do arquivo vindo do github. """ local_filename = os.path.join(NOME_PASTA, NOME_ARQUIVO) r = _get(url=URL, headers=HEADER) if r.status_code == 200: with open(local_filename, 'wb') as f: for chunk in r.iter_content(chunk_size=1024): if chunk: f.write(chunk) # f.flush() return local_filename raise Exception("Erro ao conectar com o github !")
async def __call__(self, category, query='', fmt=FMT_JSON, kwargs={}): '''set up and send the http call ''' payload = {} # Flaten option dict into string opts = '%20'.join(['%s:%s' % (e, kwargs[e]) for e in kwargs]) self._sanity_check(category, kwargs) payload['url'] = '%s%s/?query=%s%s%s' % ( self.API_URL, category, query, opts, self._fmt) ret = _get(**payload) self.logger.debug('%s will be called' % ret.url) return await self._handleReturned(ret.text)
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)
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)
def processor(code): headers = { 'User-Agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36", 'Host': 'www.ypppt.com' } downloadHref = BeautifulSoup( _get('http://www.ypppt.com/p/d.php?aid={}'.format(code), { "aid": code }, verify=False, headers=headers).text, 'html.parser').find_all('a')[0].get('href') if downloadHref.find('pan.baidu') != -1: pass elif downloadHref.find('uploads') != -1: downloadHref = 'http://www.ypppt.com' + downloadHref return downloadHref
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)
def getSets(self, **kwargs) -> list: ''' A way to get different sets from a query. All parameters are optional, but you should *probably* use some. :param str query: The thing you're searching for. :param str theme: The theme of the set. :param str subtheme: The subtheme of the set. :param str setNumber: The LEGO set number. :param str year: The year in which the set came out. :param str owned: Whether or not you own the set. Only works when logged in with :meth:`login`. Set to `1` to make true. :param str wanted: Whether or not you want the set. Only works when logged in with :meth:`login`. Set to `1` to make true. :param str orderBy: How you want the set ordered. Accepts 'Number', 'YearFrom', 'Pieces', 'Minifigs', 'Rating', 'UKRetailPrice', 'USRetailPrice', 'CARetailPrice', 'EURetailPrice', 'Theme', 'Subtheme', 'Name', 'Random'. Add 'DESC' to the end to sort descending, e.g. NameDESC. Case insensitive. Defaults to 'Number'. :param str pageSize: How many results are on a page. Defaults to 20. :param str pageNumber: The number of the page you're looking at. Defaults to 1. :param str userName: The name of a user whose sets you want to search. :returns: A list of LEGO sets. :rtype: List[:class:`brickfront.build.Build`] :raises brickfront.errors.InvalidRequest: If the site doesn't like the sent request. ''' # Generate a dictionary to post values = { 'apiKey': self._apiKey, 'userHash': self._userHash, 'query': kwargs.get('query', ''), 'theme': kwargs.get('theme', ''), 'subtheme': kwargs.get('subtheme', ''), 'setNumber': kwargs.get('setNumber', ''), 'year': kwargs.get('year', ''), 'owned': kwargs.get('owned', ''), 'wanted': kwargs.get('wanted', ''), 'orderBy': kwargs.get('orderBy', 'Number'), 'pageSize': kwargs.get('pageSize', '20'), 'pageNumber': kwargs.get('pageNumber', '1'), 'userName': kwargs.get('userName', '') } # Send the GET request. returned = _get(self._getURL('getSets', values)) # Make sure all is well self._isOkayRequest(returned) root = _ET.fromstring(returned.text) return [_Build(i, self._userHash) for i in root]
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")
def checkKey(self) -> bool: ''' Checks that an API key is valid. :returns: Whether the key is valid or not. :rtype: `bool` :raises brickfront.errors.InvalidRequest: If the site doesn't like the sent request. ''' # Get site returned = _get(self._getURL('checkKey', 'apiKey={}'.format(self._apiKey))) # Make sure all is well self._isOkayRequest(returned) # Parse XML root = _ET.fromstring(returned.text) # Return bool return root.text == 'OK'
def call(self, url=None, headers=None, payload=None, data=None): if payload is not None: ret = _get(url, headers=headers, timeout=self.timeout, params=payload) if ret.status_code != 200: raise BtcTaxError("Status Code: {}".format(ret.status_code)) else: return ret elif data is not None: s = session() ret = s.post(url, data=data) if ret.status_code != 200: raise BtcTaxError("Status Code: %s" % ret.status_code) else: capital_gains_csv_url = 'https://bitcoin.tax/gains/' \ 'download?reporttype=allocations&format=csv&ignorezero=false&rounded=false' r = s.get(capital_gains_csv_url) encoding = r.headers['Content-Type'].split(";")[1].split("=")[1] return r.content.decode(str(encoding))
def get_page(url:str, user_agent='Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'): """ Attempts to get the content at `url` by making an HTTP GET request. If the content-type of response is some kind of HTML/XML, return the text content as bytes, otherwise return None. """ headers = { 'User-Agent': user_agent } try: with _closing(_get(url, stream=True, headers=headers)) as resp: if _is_good_response(resp): return resp.content else: return None except _RequestException as e: _log_error('Error during requests to {0} : {1}'.format(url, str(e))) return None
def downloader(FirstHref, bookName): oral = _get('https://www.x23qb.com' + FirstHref) oral.encoding = oral.apparent_encoding content = _Bs(oral.text, 'html.parser').find_all('div', id="TextContent")[0].text for delete in [ 'style_tp();', 'style_bm();', 'chapter();', '铅笔小说', '(www.x23qb.com)' ]: content = content.replace(delete, '') content = '\n\n'.join( each.replace(' ', '').replace('\xa0', ' ') for each in content.split('\n') if each.replace(' ', '').replace(' ', '')) with open(bookName, 'a', encoding='utf-8') as f: f.writelines(content) msg = str( _Bs(oral.text, 'html.parser').find_all('p', class_="mlfy_page")[0]) msg = _Bs(msg, 'html.parser').find_all('a')[4].get('href') if msg.find('_') != -1: msg = downloader(msg, bookName) return msg
def getSet(self, setID: str) -> _Build: ''' Gets the information of one build, using its Brickset set ID :param str setID: The ID of the build from Brickset. :returns: A single LEGO set in a list. Will return an empty list if no sets are found. :rtype: List[:class:`brickfront.build.Build`] :raises brickfront.errors.InvalidRequest: If the site doesn't like the sent request. ''' values = { 'apiKey': self._apiKey, 'userHash': self._userHash, 'setID': setID } # Send the GET request. returned = _get(self._getURL('getSet', values)) # Make sure all is well self._isOkayRequest(returned) root = _ET.fromstring(returned.text) return [_Build(i, self._userHash) for i in root]
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)
def get(*args, **kwargs): print(">> get({}, {})".format(repr(args), repr(kwargs))) return _get(*args, **kwargs)
def requests_get(url, **kwargs): return _get(url, **kwargs)
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!")
def searcher(Type): sys = system('cls') while 1: name = input('请输入你想下载的歌曲:') # name = '梅香如故' # 调试 nameCode = quote(name) url = 'https://www.socarchina.com/vipmusic/' headers = {'X-Requested-With': 'XMLHttpRequest'} data = {'input': name, 'filter': 'name', 'type': Type, 'page': '1'} fileName = urlS = 0 print('搜索中。。。请稍后') try: res = post(url, data=data, headers=headers).text res = loads(res)['data'] except: sys = system('cls') print('TimeOutError\n网络异常,请检查网络,稍后重试。') while 1: pass title, downUrl, author = [i['title'] for i in res ], [i['url'] for i in res ], [i['author'] for i in res] for each in range(len(author)): if author[each] == '': author[each] = '未知歌手' sys = system('cls') if len(title) == 0: print('没有找到匹配结果。可以尝试减少关键字。') continue for each in range(len(title)): print('{}. {} - {}'.format(each, author[each], title[each])) code = input('10.重新搜索\n11.更换源站\n请输入要下载的歌曲编号:') while 1: try: code = int(code) if code == 10 or code == 11: break fileName, urlS = '{} - {}'.format(author[code], title[code]), downUrl[code] if urlS[len(urlS) - 4:len(urlS) - 3] != '.': sys = system('cls') for each in range(len(title)): print('{}. {} - {}'.format(each, author[each], title[each])) code = input('10.重新搜索\n11.更换源站\n这个源出了点小问题。。。请选择其他源或其他歌手:') continue if path.exists('music\\' + fileName + urlS[len(urlS) - 4:len(urlS)]): sys = system('cls') for each in range(len(title)): print('{}. {} - {}'.format(each, author[each], title[each])) code = input( '10.重新搜索\n11.更换源站\n{}已存在。\n请选择:'.format(fileName)) continue try: print('加载中。。。') if Type == 'netease': headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36' } try: urlS = _get( urlS, headers=headers, allow_redirects=False).headers['location'] video = _get(urlS) writeFile( video, 'music\\' + fileName + urlS[len(urlS) - 4:len(urlS)]) sys = system('cls') except: sys = system('cls') for each in range(len(title)): print('{}. {} - {}'.format( each, author[each], title[each])) code = input( '10.重新搜索\n11.更换源站\n这个源出了点小问题。。。请选择其他源或其他歌手:') continue else: video = _get(urlS) writeFile( video, 'music\\' + fileName + urlS[len(urlS) - 4:len(urlS)]) sys = system('cls') except: sys = system('cls') print('TimeOutError\n网络异常,请检查网络,稍后重试。') while 1: pass for each in range(len(title)): print('{}. {} - {}'.format(each, author[each], title[each])) code = input( '10.重新搜索\n11.更换源站\n{}下载成功!!!\n请选择:'.format(fileName)) except: sys = system('cls') for each in range(len(title)): print('{}. {} - {}'.format(each, author[each], title[each])) code = input('10.重新搜索\n11.更换源站\n请正确输入编号:') continue if code == 10: sys = system('cls') continue if code == 11: Type = init() sys = system('cls') continue return Type
def get(url, slug=None, related_objects=None, **kwargs): return _get(url, **kwargs)