Skip to content

Instantly share code, notes, and snippets.

@lelandbatey
Created May 19, 2013 23:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lelandbatey/5609473 to your computer and use it in GitHub Desktop.
Save lelandbatey/5609473 to your computer and use it in GitHub Desktop.
Generates a json file with the item-ids of all tradable items as the keys, and the corresponding item names as the values. Very "hacky" in it's approach to getting the right data that it needs, but it gets the job done. Might break in the future.
from __future__ import print_function
from pprint import pprint
import json
# What do I need?
# This requires two files: tf2ItemsFile and tf2PricesJson.
# tf2ItemsFile
# A copy of the "current client item schema" for tf2. This can either be found
# in your <instance>/tf/scripts/items/items_game.txt or you can download it
# online here: http://git.optf2.com/schema-tracking/plain/Team%20Fortress%202%20Client%20Schema?h=teamfortress2
# tf2PricesJson
# We need a copy of the price-listing from backpack.tf, but not because we
# need the prices. Really, all we need is the item Id's of all the tradable
# items in TF2, and this script is built to get them out of the json that is
# provided by backpack.tf.
# How do you get that? Well, you can get it by going to
# http://backpack.tf/api/ and registering for an api key, then downloading the
# page:
# http://backpack.tf/api/IGetPrices/v3/?format=json&key=YOUR_API_KEY_GOES_HERE
# Don't screw up the downloading of that, because you can only download that
# data once every 5 minutes. So don't lose it!
# What does this thing do?
# This short python script will create a json file named whatever you set in
# OUTPUT_JSON_FILE (itemNameAndIdJson.json by default) that will be a
# dictionary of all the item id's as keys, and all the item names as the
# corresponding values. Both the values AND the keys are stored as strings.
tf2ItemsFile = 'tf2ItemsInfo.json'
tf2PricesJson = 'may18Prices.json'
OUTPUT_JSON_FILE = 'itemNameAndIdJson.json'
itemInfoOutFile = open(OUTPUT_JSON_FILE,'w')
tf2ItemsInfo = open(tf2ItemsFile,'r').read().split('\n')
def get_item_name_from_index(index):
index = str(index)
toReturn = ""
index = '"'+index+'"'
for line in tf2ItemsInfo:
if index == line.strip():
dexNum = tf2ItemsInfo.index(line)
result = ""
try:
result = tf2ItemsInfo[dexNum+2].strip().split('"')[3]
except:
print('\tERROR!!:\n\t\t'+str(tf2ItemsInfo[dexNum+2].strip().split('"')))
pass
if toReturn != result:
toReturn += result
return toReturn
def getItemIndexes():
rawJsonData = json.loads(open(tf2PricesJson,'r').read())
prices = rawJsonData["response"]['prices']
itemNums = prices.keys()
itemDict = {}
for num in itemNums:
itemDict[str(num)] = get_item_name_from_index(num)
print(json.dumps(itemDict, sort_keys=True, indent=4, separators=(',', ': ')),file=itemInfoOutFile)
getItemIndexes()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment