Skip to main content
edited title
Link

Gets a user's csgo CSGO inventory and gets pricesprice python code

Source Link

Gets a user's csgo inventory and gets prices

#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib2
import json
import datetime
import time

global file_name
file_name = "skins 2017-05-05 23-15-16.txt"

wear_list = ["Factory New", "Minimal Wear", "Field-Tested", "Well-Worn", "Battle-Scarred"]
wear_val = {"Factory New": 1, "Minimal Wear": 2, "Field-Tested": 3, "Well-Worn": 4, "Battle-Scarred": 5}
items = []
item_prices = {}

def getInventory(steamid):
    try:
        data = urllib2.urlopen('http://steamcommunity.com/profiles/'+steamid+'/inventory/json/730/2')
    except:
        print("Overloaded the server...")
        print("Waiting...")
        time.sleep(60)
        data = urllib2.urlopen('http://steamcommunity.com/profiles/'+steamid+'/inventory/json/730/2')
    json_data = json.loads(data.read())
    descriptions = json_data['rgDescriptions']
    now = datetime.datetime.now()
    date = now.strftime("%Y-%m-%d %H-%M-%S")
    global file_name
    file_name = "skins " + str(date) + ".txt"
    txt = open(file_name, "w+")
    for v in descriptions:
        name = str([descriptions[v]['market_name']])
        name = name[3:]
        name = name[:-2]
        if name.endswith("Flip Knife | Rust Coat (Battle-Scarred)"):
            name = name[7:]
        if name.startswith("StatTrak"):
            name = name[15:]
            name = 'StatTrak ' + name
        if name.endswith("(Dragon King) (Minimal Wear)"):
            name = "M4A4 | Dragon King (Minimal Wear"
        txt.write(name)
        txt.write('\n')
        #txt.write(str(descriptions[v]))
        #txt.write('\n')
        print(name)
    txt.close()
    print('Done!')
    return

def getPrice():
    x = 1
    gun_name_wear = 0
    txt = open(file_name, "r+")
    for line in txt:
        stattrak = 0
        wear = line[line.find("(")+1:line.find(")")]
        if wear in wear_list:
            print(wear)
            wear = wear.replace(" ","%20")
            gun = line.split(' |', 1)[0].replace('.', '')
            print(gun)
            if "StatTrak" in gun:
                stattrak = 1
                gun = gun.replace("StatTrak","")
            if gun.startswith(" "):
                gun = gun[1:]
            gun = gun.replace(" ", "%20")
            name = line[line.find("| ")+1:line.find(" (")]
            if name.startswith(" "):
                name = name[1:]
            print(name)
            name = name.replace(" ", "%20")
            if stattrak == 1:
                try:
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=StatTrak%E2%84%A2%20" + gun + "%20|%20" + name + "%20(" + wear + ")")
                except:
                    print("Overloaded the server...")
                    print("Waiting...")
                    time.sleep(60)
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=StatTrak%E2%84%A2%20" + gun + "%20|%20" + name + "%20(" + wear + ")")
                json_data = json.loads(data.read())
                print(json_data)
                try:
                    price = json_data['lowest_price']
                except KeyError:
                    price = json_data['median_price']
                price = price[-4:]
                print(price)
                gun = gun.replace("%20", " ")
                name = name.replace("%20", " ")
                wear = wear.replace("%20", " ")
                item_prices[str("StatTrak " + gun + " " + name + " " + wear)] = price
                continue
            if "knife" in line.lower():
                try:
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=★%20" + gun + "%20|%20" + name + "%20(" + wear + ")")
                except:
                    print("Overloaded the server...")
                    print("Waiting...")
                    time.sleep(60)
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=★%20" + gun + "%20|%20" + name + "%20(" + wear + ")")
                json_data = json.loads(data.read())
                print(json_data)
                try:
                    price = json_data['lowest_price']
                except KeyError:
                    price = json_data['median_price']
                price = price[-4:]
                print(price)
                gun = gun.replace("%20", " ")
                name = name.replace("%20", " ")
                wear = wear.replace("%20", " ")
                item_prices[str(gun + " " + name + " " + wear)] = price
                continue
            else:
                try:
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=" + gun + "%20|%20" + name + "%20(" + wear + ")")
                except:
                    print("Overloaded the server...")
                    print("Waiting...")
                    time.sleep(60)
                    data = urllib2.urlopen("http://steamcommunity.com/market/priceoverview/?appid=730&currency=2&market_hash_name=" + gun + "%20|%20" + name + "%20(" + wear + ")")
                json_data = json.loads(data.read())
                print(json_data)
                try:
                    price = json_data['lowest_price']
                except KeyError:
                    price = json_data['median_price']
                price = price[-4:]
                print(price)
                gun = gun.replace("%20", " ")
                name = name.replace("%20", " ")
                wear = wear.replace("%20", " ")
                gun_name_wear = str(gun + " " + name + " " + wear)
                price = price.replace 
                item_prices[str(gun + " " + name + " " + wear)] = price
            time.sleep(5)
        else:
            continue
    print(item_prices)
    print(item_prices[gun_name_wear])

getInventory('76561198216481185')
getPrice()

my code gets a players inventory, writes it to a text file, then works out the prices of said inventory.

I know I repeat myself when I get the JSON data for each of the different types of weapons (knife/statrak/normal) but i will most likely write function at a later date.

Anything would help, thank you.

Edit: Please ignore the "global file_name" and the line underneath that. I used that when tweaking only small parts of the script and to avoid mass requests to the server.