Skip to content

Instantly share code, notes, and snippets.

@mikebobadilla
Forked from Terrance/SteamMarket.py
Last active August 29, 2015 14:23
Show Gist options
  • Save mikebobadilla/dc2ebdda38278b734628 to your computer and use it in GitHub Desktop.
Save mikebobadilla/dc2ebdda38278b734628 to your computer and use it in GitHub Desktop.
import json, re, shlex, urllib.request
watch = []
def getPrice(item):
listings = json.loads(urllib.request.urlopen(makeUrl(item)).read().decode("utf-8"))["listinginfo"]
lowest = None
for id, obj in list(listings.items()):
try:
price = obj["converted_price"]
except KeyError:
price = obj["price"]
if not lowest or price < lowest:
lowest = price
return "£{:0,.2f}".format(lowest / 100)
def getPrices():
for item in watch:
print(item["name"] + ":", end=" ")
print(getPrice(item))
def processUrl(url):
match = re.search("([0-9]+)/([0-9]+)-([^\/?#]+)", url)
if not match:
return
groups = match.groups()
return {
"app": groups[0],
"set": groups[1],
"name": urllib.request.unquote(groups[2])
}
def makeUrl(item):
return "http://steamcommunity.com/market/listings/" + item["app"] + "/" + item["set"] + "-" + urllib.request.quote(item["name"]) + "/render/?query=&start=0&count=100"
def addWatch(url):
newItem = processUrl(url)
if not newItem:
return print("Couldn't decode the URL. Paste in a Market link to watch.")
for item in watch:
match = True
for prop in ["app", "set", "name"]:
match &= item[prop] == newItem[prop]
if match:
return print("Already watching " + newItem["name"] + ".")
watch.append(newItem)
print("Added " + newItem["name"] + ".")
if __name__ == "__main__":
while True:
cmd = shlex.split(input("~> "))
if len(cmd):
if cmd[0] in ["watch", "add", "w", "a"]:
for url in cmd[1:]:
addWatch(cmd[1])
if len(cmd) == 1:
print("Paste in a Market link to watch.")
elif cmd[0] in ["clear", "empty", "e"]:
watch = []
elif cmd[0] in ["check", "get", "c", "g"]:
getPrices()
elif cmd[0] in ["quit", "exit", "q", "x"]:
break
else:
print("Unknown command " + cmd[0] + ". Available: watch, clear, get, quit.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment