def req_itemordershistogram(self, hash_name=None, item_nameid=None, **req_options): try: if not item_nameid: if not hash_name: raise NameError item_nameid = self.get_item_nameid(hash_name, **req_options).data()['item_nameid'] if item_nameid == 'NotFound': raise NotImplementedError url = 'https://steamcommunity.com/market/itemordershistogram' params = { 'country': 'CN', 'language': 'schinese', 'currency': 23, 'norender': 1, 'item_nameid': item_nameid, 'two_factor': 0, } response = requests.get(url, params, **req_options) if response.status_code == 200: data = response.json() if data: data['hash_name'] = hash_name data['item_nameid'] = item_nameid return DataPatch(data) return DataPatch(status_code=0) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: print(response.url) self._log(1, f':bad response:{response.status_code}') self.shadowsocks.shuffle_server() return DataPatch(status_code=response.status_code)
def normalized_price(self, price_text): """ simple currency convert toolkit """ try: currency = { '$': 'USD', 'USD': 'USD', '¥': 'CNY', '¥': 'CNY', 'CNY': 'CNY' } price_text = price_text.replace(' ', '').replace('\'', '').replace(',', '') data_type = self.currency_type for prefix in currency: if prefix in price_text: data_type = currency[prefix] price_text = price_text.replace(prefix, '') price = float(price_text) / self.currency_cache[data_type] return price except Exception as e: handle_error(e)
def bref_info(self, hash_name, proxy, timeout): try: req_data = { 'appid': '570', 'keyw': hash_name, 'Sockets': '0', 'start': '0', 'fuzz': 'true', 'AssetsType': '0', 'OrderBy': 'Default', 'safeonly': 'false' } base = 'http://www.dotasell.com/xhr/json_search.ashx' response = requests.get(base, params=req_data, proxies=proxy, timeout=timeout) if response.status_code == 200: json = response.json() items = json.get('MerchandiseList') for item in items: hash_n = item['MarketHashName'] if hash_n == hash_name: return DataPatch({'hash_name': hash_name, 's_price': item['LocalPrice'], 's_quantity': json['TotalCount']}) self._log(6, ':DSApi.bref_info:item not found') return DataPatch(status_code=0) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: self._log(6, f':DSApi.bref_info:{response.status_code}') return DataPatch(status_code=response.status_code)
def _read(self, file_path): load_func = self.indiv('load_func') with open(file_path, 'r') as f: for line in f.readlines(): try: data = json.loads(line) data = load_func(data) except IllegalJsonException: handle_error(IllegalJsonException) else: self.output.put(data)
def fetch_data(self, **options): """ replace this func with custom method. """ self._log(2, f':working on{str(options)}') try: patch = DataPatch(options) except Exception as e: handle_error(e) patch = DataPatch(status_code=450) self._log(2, f':data fetched:{patch}') return patch
def get_item_nameid(self, hash_name, **req_options): try: url = 'https://steamcommunity.com/market/listings/570/' + hash_name time.sleep(1) response = requests.get(url, **req_options) if response.status_code == 200: html = response.text tar = re.search(r'Market_LoadOrderSpread\( (.*?) \)', html) tar = tar.group(1) return DataPatch({'hash_name': hash_name, 'item_nameid': tar}) except Exception as e: if 'AttributeError' in repr(e): print(f'\033[0;31m:{hash_name} item_nameid not found.\033[0m') return DataPatch({'hash_name': hash_name, 'item_nameid': 'NotFound'}) else: handle_error(e) return DataPatch(status_code=1) else: self._log(1, f':bad response:{response.status_code}') self.shadowsocks.shuffle_server() return DataPatch(status_code=response.status_code)
def req_priceoverview(self, hash_name, appid=570, **req_options): try: url = 'https://steamcommunity.com/market/priceoverview/' params = { 'country': 'JP', 'currency': 1, 'appid': appid, 'market_hash_name': hash_name, } response = requests.get(url, params, **req_options) if response.status_code == 200: data = response.json() if len(data) == 0: return DataPatch(status_code=0) data['hash_name'] = hash_name return DataPatch(data) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: self._log(1, f':bad response:{response.status_code}') return DataPatch(status_code=response.status_code)
def req_pricehistory(self, hash_name, **req_options): try: url = 'http://steamcommunity.com/market/pricehistory/' params = { 'country': 'JP', 'currency': 1, 'appid': 570, 'market_hash_name': hash_name, } response = requests.get(url, params, cookies=self.login_auth, **req_options) if response.status_code == 200: data = response.json() if len(data) == 0: return DataPatch(status_code=0) data = {'hash_name': hash_name, 'data': data} return DataPatch(data) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: self._log(1, f':bad response:{response.status_code}') return DataPatch(status_code=response.status_code)
def sales_info(hash_name, proxy, timeout): try: base_sales = 'http://www.dotasell.com/xhr/json_ItemsInSale.ashx?appid=570' req_data = { 'GemNeedle': ['-1'], 'HashName': hash_name, 'onlytradable': 'True', 'start': '0', 'StyleNeedle': '-1' } response = requests.post(base_sales, json=req_data, proxies=proxy, timeout=timeout) if response: data = response.json() sale_info = data.get('SaleInfo') if not sale_info: return DataPatch(status_code=0) return DataPatch({'hash_name': hash_name, 'sales': [x['PriceCNYString'] for x in sale_info]}) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: return DataPatch(status_code=response.status_code)
def req_search(self, start=0, size=100, sort_column='name', proxies=None, timeout=10, **req_options): try: url = 'https://steamcommunity.com/market/search/render/' params = { 'appid': 570, 'norender': 1, 'sort_column': sort_column, 'start': start, 'count': size, } response = requests.get(url, params, proxies=proxies, timeout=timeout) if response.status_code == 200: data = response.json() data = data.get('results') if data: return DataPatch(data) return DataPatch(status_code=0) except Exception as e: handle_error(e) return DataPatch(status_code=1) else: self._log(1, f':bad response:{response.status_code}') self.shadowsocks.shuffle_server() return DataPatch(status_code=response.status_code)