import urllib2
import json
import codecs
profile = raw_input('Enter id/customURL or profiles/SteamID64: ')
url = "http://steamcommunity.com/"+profile+"/inventory/json/753/3/"
cookie = "Steam_Language=english" # yazyk ne menyat'
req = urllib2.Request(url)
req.add_header("Cookie", cookie + ";")
f = urllib2.urlopen(req)
data = json.loads(f.read())
data_inv = data['rgInventory']
coupons = []
for item in data_inv:
coupons.append({'cid':data_inv[item]['classid'], 'inid':data_inv[item]['instanceid']})
newcoupons = []
while (len(coupons)>0):
x = coupons[0]
c = coupons.count(x)
for i in range(c):
coupons.remove(x)
x['count']=c
newcoupons.append(x)
coupons = newcoupons
newcoupons = []
for item in coupons:
name = data['rgDescriptions'][item['cid']+'_'+item['inid']]['name']
name = name.split(" ",2)
item['name'] = name[2]
item['off'] = name[0]
newcoupons.append(item)
outstr = u''
for x in newcoupons:
outstr += "%s;%s;%s\n" % (x['count'], x['off'], unicode(x['name']))
f = codecs.open('coupons.csv', 'w', 'utf-8')
f.writelines(outstr)
f.close()