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 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
Ejemplo n.º 3
0
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
Ejemplo n.º 5
0
 def test_update_and_save_cookie_to_disk_if_values_changed(self):
     original_cookie = dict(steamLoginSecure='a very secured string',
                            sessionid='my current session')
     dict_with_new_values = dict(sessionid='my new session')
     cookie = personal_info.update_and_save_cookie_to_disk_if_values_changed(
         original_cookie,
         dict_with_new_values,
         file_name_with_personal_info='temp.txt',
         verbose=True)
     self.assertTrue(
         all(original_cookie[field] == cookie[field]
             for field in original_cookie.keys()
             if field not in dict_with_new_values.keys()))
     self.assertTrue(
         all(dict_with_new_values[field] == cookie[field]
             for field in dict_with_new_values.keys()))
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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
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
Ejemplo n.º 9
0
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
Ejemplo n.º 10
0
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
Ejemplo n.º 11
0
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