Created
September 15, 2013 14:08
-
-
Save Terrance/6571053 to your computer and use it in GitHub Desktop.
A small shell for checking Steam Market prices. Commands are `watch <url>` (add item to watch), `clear` (remove all watches), `get` (get prices), `quit`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
how do you ensure that the price is always in GBP?