def update_all_listings_for_items_other_than_cards(tag_drop_rate_str=None, rarity=None): # Profile Backgrounds update_all_listings_for_profile_backgrounds( tag_drop_rate_str=tag_drop_rate_str, rarity=rarity) # Forced cooldown cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) rate_limits = get_steam_api_rate_limits_for_market_search( has_secured_cookie) cooldown_duration = rate_limits['cooldown'] print( 'Forced cooldown between profile backgrounds and emoticons. Cooldown: {} seconds' .format(cooldown_duration)) time.sleep(cooldown_duration) # Emoticons update_all_listings_for_emoticons(tag_drop_rate_str=tag_drop_rate_str, rarity=rarity) return
def get_session_id(cookie=None): if cookie is None: cookie = get_cookie_dict() session_id = cookie['sessionid'] return session_id
def download_sack_of_gems_price(sack_of_gems_listing_file_name=None): if sack_of_gems_listing_file_name is None: sack_of_gems_listing_file_name = get_sack_of_gems_listing_file_name() cookie = get_cookie_dict() listing_hash = get_listing_hash_for_gems() listing_details, status_code = get_listing_details( listing_hash=listing_hash, cookie=cookie) if status_code == 200: item_nameid = listing_details[listing_hash]['item_nameid'] bid_price, ask_price, bid_volume, ask_volume = download_market_order_data( listing_hash, item_nameid) listing_details[listing_hash]['bid'] = bid_price listing_details[listing_hash]['ask'] = ask_price listing_details[listing_hash]['bid_volume'] = bid_volume listing_details[listing_hash]['ask_volume'] = ask_volume sack_of_gems_price = ask_price with open(sack_of_gems_listing_file_name, 'w', encoding='utf-8') as f: json.dump(listing_details, f) else: raise AssertionError() return sack_of_gems_price
def query_goo_value(app_id, item_type, verbose=True): cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) url = get_steam_goo_value_url() req_data = get_steam_goo_value_parameters(app_id=app_id, item_type=item_type) if has_secured_cookie: resp_data = requests.get(url, params=req_data, cookies=cookie) else: resp_data = requests.get(url, params=req_data) status_code = resp_data.status_code if status_code == 200: result = resp_data.json() if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed( cookie, jar) goo_value = int(result['goo_value']) if verbose: if goo_value > 0: print('AppID: {} ; Item type: {} ; Goo value: {} gems'.format( app_id, item_type, goo_value)) else: goo_value = None return goo_value
def get_listing_details_batch(listing_hashes, all_listing_details=None, save_to_disk=True, listing_details_output_file_name=None): if listing_details_output_file_name is None: listing_details_output_file_name = get_listing_details_output_file_name( ) cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) rate_limits = get_steam_api_rate_limits_for_market_listing( has_secured_cookie) if all_listing_details is None: all_listing_details = dict() num_listings = len(listing_hashes) query_count = 0 for count, listing_hash in enumerate(listing_hashes): if count + 1 % 100 == 0: print('[{}/{}]'.format(count + 1, num_listings)) listing_details, status_code = get_listing_details( listing_hash=listing_hash, cookie=cookie) query_count += 1 if status_code != 200: print('Wrong status code ({}) for {} after {} queries.'.format( status_code, listing_hash, query_count)) break if query_count >= rate_limits['max_num_queries']: if save_to_disk: with open(listing_details_output_file_name, 'w', encoding='utf-8') as f: json.dump(all_listing_details, f) cooldown_duration = rate_limits['cooldown'] print('Number of queries {} reached. Cooldown: {} seconds'.format( query_count, cooldown_duration)) time.sleep(cooldown_duration) query_count = 0 all_listing_details.update(listing_details) if save_to_disk: with open(listing_details_output_file_name, 'w', encoding='utf-8') as f: json.dump(all_listing_details, f) return all_listing_details
def sell_booster_pack( asset_id, price_in_cents, # this is the money which you, as the seller, will receive verbose=True): cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) if not has_secured_cookie: raise AssertionError() session_id = get_session_id(cookie=cookie) # Format the price (in cents) as an integer before sending a request to Steam API # Otherwise, a price like 18.0 would still work, but a price like 14.000000000000002 would return status code 400. price_in_cents = round(price_in_cents) url = get_steam_market_sell_url() req_data = get_market_sell_parameters(asset_id=asset_id, price_in_cents=price_in_cents, session_id=session_id) resp_data = requests.post(url, headers=get_request_headers(), data=req_data, cookies=cookie) status_code = resp_data.status_code if status_code == 200: # Expected result: # {"success":true,"requires_confirmation":0} result = resp_data.json() jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar) if result['success']: print('Booster pack {} successfully sold for {} cents.'.format( asset_id, price_in_cents)) else: print( 'Booster pack {} not sold for {} cents, despite OK status code.' .format(asset_id, price_in_cents)) else: # NB: 400 means "Bad Request". print( 'Booster pack {} not sold for {} cents. Status code {} was returned.' .format(asset_id, price_in_cents, status_code)) result = None if verbose: print(result) return result
def download_user_data(): cookie = get_cookie_dict() resp_data = requests.get(get_user_data_url(), cookies=cookie) if resp_data.status_code == 200: result = resp_data.json() jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar) else: result = None return result
def download_steam_inventory(profile_id=None, save_to_disk=True, start_asset_id=None): if profile_id is None: profile_id = get_my_steam_profile_id() cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) url = get_steam_inventory_url(profile_id=profile_id) req_data = dict( l='english', # noqa: E741 count=5000, ) # TODO if start_asset_id is not None: req_data['start_assetid'] = start_asset_id # TODO if has_secured_cookie: resp_data = requests.get(url, params=req_data, # TODO cookies=cookie) else: resp_data = requests.get(url, params=req_data) # TODO status_code = resp_data.status_code if status_code == 200: steam_inventory = resp_data.json() if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar) if save_to_disk: with open(get_steam_inventory_file_name(profile_id), 'w') as f: json.dump(steam_inventory, f) else: print('Inventory for profile {} could not be loaded. Status code {} was returned.'.format(profile_id, status_code)) steam_inventory = None return steam_inventory
def download_recommender_inputs(): url = get_recommender_inputs_url() params = get_recommender_inputs_request_params() cookies = get_cookie_dict() response = requests.get(url, params=params, cookies=cookies) if response.status_code == 200: result = response.json() jar = dict(response.cookies) update_and_save_cookie_to_disk_if_values_changed(cookies, jar) else: print('Download of inputs failed with status code {}.'.format( response.status_code)) result = None return result
def get_listing_details(listing_hash=None, cookie=None, render_as_json=False): listing_details = dict() url = get_steam_market_listing_url(listing_hash=listing_hash, render_as_json=render_as_json) req_data = get_listing_parameters() if cookie is None: cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) if has_secured_cookie: resp_data = requests.get(url, params=req_data, cookies=cookie) else: resp_data = requests.get(url, params=req_data) status_code = resp_data.status_code if status_code == 200: html_doc = resp_data.text if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed( cookie, jar) item_nameid, is_marketable, item_type_no = parse_item_name_id(html_doc) if item_nameid is None: print('Item name ID not found for {}'.format(listing_hash)) if is_marketable is None: print('Marketable status not found for {}'.format(listing_hash)) if item_type_no is None: print('Item type not found for {}'.format(listing_hash)) listing_details[listing_hash] = dict() listing_details[listing_hash]['item_nameid'] = item_nameid listing_details[listing_hash]['is_marketable'] = is_marketable listing_details[listing_hash]['item_type_no'] = item_type_no return listing_details, status_code
def create_booster_pack(app_id, is_marketable=True, verbose=True): cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) if not has_secured_cookie: raise AssertionError() session_id = get_session_id(cookie=cookie) url = get_steam_booster_pack_creation_url() req_data = get_booster_pack_creation_parameters( app_id=app_id, session_id=session_id, is_marketable=is_marketable) resp_data = requests.post(url, data=req_data, cookies=cookie) status_code = resp_data.status_code if status_code == 200: # Expected result: # {"purchase_result":{"communityitemid":"XXX","appid":685400,"item_type":36, "purchaseid":"XXX", # "success":1,"rwgrsn":-2}, "goo_amount":"22793","tradable_goo_amount":"22793","untradable_goo_amount":0} print( '\n[appID = {}] Booster pack successfully created.'.format(app_id)) result = resp_data.json() jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar) else: # NB: 401 means "Unauthorized", which must have something to do with wrong/outdated credentials in the cookie. if status_code == 500: print( '\n[appID = {}] Booster pack not created, because a pack was created less than 24h ago.' .format(app_id)) else: print( '\n[appID = {}] Booster pack not created, because of status code {}.' .format(app_id, status_code)) result = None if verbose: print(result) return result
def download_steam_inventory(profile_id=None, save_to_disk=True): if profile_id is None: profile_id = get_my_steam_profile_id() cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) url = get_steam_inventory_url(profile_id=profile_id) if has_secured_cookie: resp_data = requests.get(url, cookies=cookie) else: resp_data = requests.get(url) status_code = resp_data.status_code if status_code == 200: steam_inventory = resp_data.json() if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed( cookie, jar) if save_to_disk: with open(get_steam_inventory_file_name(profile_id), 'w', encoding='utf-8') as f: json.dump(steam_inventory, f) else: print( 'Inventory for profile {} could not be loaded. Status code {} was returned.' .format(profile_id, status_code)) steam_inventory = None return steam_inventory
def test_get_cookie_dict(self): cookie = personal_info.get_cookie_dict(verbose=True) self.assertIn(len(cookie), [0, 2])
def get_all_listings(all_listings=None, url=None, tag_item_class_no=None, tag_drop_rate_str=None, rarity=None): if url is None: url = get_steam_market_search_url() cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) rate_limits = get_steam_api_rate_limits_for_market_search(has_secured_cookie) if all_listings is None: all_listings = dict() num_listings = None query_count = 0 start_index = 0 delta_index = 100 while (num_listings is None) or (start_index < num_listings): if num_listings is not None: print('[{}/{}]'.format(start_index, num_listings)) req_data = get_search_parameters(start_index=start_index, delta_index=delta_index, tag_item_class_no=tag_item_class_no, tag_drop_rate_str=tag_drop_rate_str, rarity=rarity) if query_count >= rate_limits['max_num_queries']: cooldown_duration = rate_limits['cooldown'] print('Number of queries {} reached. Cooldown: {} seconds'.format(query_count, cooldown_duration)) time.sleep(cooldown_duration) query_count = 0 try: if has_secured_cookie: resp_data = requests.get(url, params=req_data, cookies=cookie) else: resp_data = requests.get(url, params=req_data) except requests.exceptions.ConnectionError: resp_data = None try: status_code = resp_data.status_code except AttributeError: status_code = None start_index += delta_index query_count += 1 if status_code == 200: result = resp_data.json() if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed(cookie, jar) num_listings_based_on_latest_query = result['total_count'] if num_listings is not None: num_listings = max(num_listings, num_listings_based_on_latest_query) else: num_listings = num_listings_based_on_latest_query listings = dict() for listing in result['results']: listing_hash = listing['hash_name'] listings[listing_hash] = dict() listings[listing_hash]['sell_listings'] = listing['sell_listings'] listings[listing_hash]['sell_price'] = listing['sell_price'] listings[listing_hash]['sell_price_text'] = listing['sell_price_text'] else: print('Wrong status code ({}) for start_index = {} after {} queries.'.format(status_code, start_index, query_count, )) if status_code is None: continue else: break all_listings.update(listings) return all_listings
def download_market_order_data_batch(badge_data, market_order_dict=None, verbose=False, save_to_disk=True, market_order_output_file_name=None, listing_details_output_file_name=None): if market_order_output_file_name is None: market_order_output_file_name = get_market_order_file_name() # Pre-retrieval of item name ids listing_hashes = [ badge_data[app_id]['listing_hash'] for app_id in badge_data.keys() ] item_nameids = get_item_nameid_batch( listing_hashes, listing_details_output_file_name=listing_details_output_file_name) # Retrieval of market orders (bid, ask) cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) rate_limits = get_steam_api_rate_limits_for_market_order( has_secured_cookie) if market_order_dict is None: market_order_dict = dict() query_count = 0 for app_id in badge_data.keys(): listing_hash = badge_data[app_id]['listing_hash'] bid_price, ask_price, bid_volume, ask_volume = download_market_order_data( listing_hash, verbose=verbose, listing_details_output_file_name=listing_details_output_file_name) market_order_dict[listing_hash] = dict() market_order_dict[listing_hash]['bid'] = bid_price market_order_dict[listing_hash]['ask'] = ask_price market_order_dict[listing_hash]['bid_volume'] = bid_volume market_order_dict[listing_hash]['ask_volume'] = ask_volume market_order_dict[listing_hash]['is_marketable'] = item_nameids[ listing_hash]['is_marketable'] if query_count >= rate_limits['max_num_queries']: if save_to_disk: with open(market_order_output_file_name, 'w', encoding='utf-8') as f: json.dump(market_order_dict, f) cooldown_duration = rate_limits['cooldown'] print('Number of queries {} reached. Cooldown: {} seconds'.format( query_count, cooldown_duration)) time.sleep(cooldown_duration) query_count = 0 query_count += 1 if save_to_disk: with open(market_order_output_file_name, 'w', encoding='utf-8') as f: json.dump(market_order_dict, f) return market_order_dict
def get_session_id(): cookies = get_cookie_dict() session_id = cookies['sessionid'] return session_id
def download_market_order_data(listing_hash, item_nameid=None, verbose=False, listing_details_output_file_name=None): cookie = get_cookie_dict() has_secured_cookie = bool(len(cookie) > 0) if item_nameid is None: item_nameid = get_item_nameid( listing_hash, listing_details_output_file_name=listing_details_output_file_name) if item_nameid is not None: url = get_steam_market_order_url() req_data = get_market_order_parameters(item_nameid=item_nameid) try: if has_secured_cookie: resp_data = requests.get(url, params=req_data, cookies=cookie) else: resp_data = requests.get(url, params=req_data) except requests.exceptions.ConnectionError: resp_data = None try: status_code = resp_data.status_code except AttributeError: status_code = None else: print( 'No query to download market orders for {}, because item name ID is unknown.' .format(listing_hash)) resp_data = None status_code = -1 if status_code == 200: result = resp_data.json() if has_secured_cookie: jar = dict(resp_data.cookies) cookie = update_and_save_cookie_to_disk_if_values_changed( cookie, jar) try: buy_order_graph = result['buy_order_graph'] try: # highest_buy_order bid_info = buy_order_graph[0] bid_price = bid_info[0] bid_volume = bid_info[1] except IndexError: bid_price = -1 bid_volume = -1 except KeyError: bid_price = -1 bid_volume = -1 try: sell_order_graph = result['sell_order_graph'] try: # lowest_sell_order ask_info = sell_order_graph[0] ask_price = ask_info[0] ask_volume = ask_info[1] except IndexError: ask_price = -1 ask_volume = -1 except KeyError: ask_price = -1 ask_volume = -1 else: bid_price = -1 bid_volume = -1 ask_price = -1 ask_volume = -1 if verbose: print( 'Listing: {} ; item id: {} ; ask: {:.2f}€ ({}) ; bid: {:.2f}€ ({})' .format(listing_hash, item_nameid, ask_price, ask_volume, bid_price, bid_volume)) return bid_price, ask_price, bid_volume, ask_volume