4

I am coding an IRC bot in Python in order for my friends and I to buy items from the steam market. It is mostly for fun and to advance my programming skills. I have been able to get the lowest price for an item, but I am having trouble POSTing the buy request. I emitted the session id and steamLogin cookies, and the url can be anything like http://steamcommunity.com/market/listings/730/Winter%20Offensive%20Weapon%20Case. Here is the code for just the steam part:

import mechanize, re, datetime, urllib
from cookielib import Cookie
from cookielib import LWPCookieJar

url = 'http://steamcommunity.com/market/listings/730/blahblah'
sessionid = ''
steamLogin = ''
maximum = int(raw_input('How much are you willing to pay? '))

def makeCookie(name, value):
    return Cookie(
        version=0, 
        name=name, 
        value=value,
        port=None, 
        port_specified=False,
        domain="steamcommunity.com", 
        domain_specified=True, 
        domain_initial_dot=False,
        path="/", 
        path_specified=True,
        secure=False,
        expires=None,
        discard=False,
        comment=None,
        comment_url=None,
        rest=None
    )

br = mechanize.Browser()

cj = LWPCookieJar()
br.set_cookiejar(cj)
cj.set_cookie(makeCookie('sessionid', sessionid))
cj.set_cookie(makeCookie('steamLogin', steamLogin))
br.addheaders = [('User-agent', 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.7) Gecko/20100713 Firefox/3.6.7')]

id = ''
response = br.open(url)
elements = response.read().splitlines()
line = (elements[elements.index('\tvar g_rgCurrency = [];') + 1])[23:]
match = re.search(r'("listingid":")(\d+)(.+?)(converted_price":)(\d+)(,"converted_fee":)(\d+)', line)
if not match: continue
if match.group(2) == id: continue
id = match.group(2)
subtotal = int(match.group(5))
fee = int(match.group(7))
total = subtotal + fee
print "Total at", str(datetime.datetime.now()) + ' for listing id (' + id + '):', '${:,.2f}'.format(float(total)/100)
if total <= maximum:
    data = urllib.urlencode({'sessionid': sessionid,
    'currency': '1',
    'subtotal': subtotal,
    'fee': fee,
    'total': total})
    print data
    try:
        post = br.open('https://steamcommunity.com/market/buylisting/' + id, data)
    except Exception as e:
        print e
Ekansh Vinaik
  • 141
  • 2
  • 4
  • Your formatting of `if not match: continue` is weird, shall go to two lines to be more readable. Your use of `continue` in your code is useless - `continue` breaks out of a loop, but in your code there is no loop. – Jan Vlcinsky Apr 21 '14 at 20:47
  • 1
    You shall try the activity you are trying to do by Python code in web browser and using Firebug in FF or DevTools in Chrome to track exactly, what is sent to the server. Then, compare it and you shall find the problem. As you are getting 400, I would assume there is some problem in authorization. You might try some simpler action which requires authorization, it could limit number of details to care about. Often, you shall be able to drill down to one or two `curl` calls. While curl requires a bit of learning, it pays back as very simple form of expressing http requests (incl. cookies) – Jan Vlcinsky Apr 21 '14 at 20:51
  • 1
    I briefly looked at the steam site and had to enable JavaScript. Mechanize doesn't handle that at all, and I suspect you are running into trouble because of that (JavaScript is often used on sites to prevent double submits of forms, even for something that looks like a normal postt). I have solved driving these kind of sites ( e.g. checking the review queues on [so] for new items, by using firefox/chrome and selenium, that is as close to browser behaviour from python as you can get. – Anthon Apr 10 '15 at 15:28
  • @ekansh, if you want to redact the code, just delete the question. Leaving the questing with its contents edited out is not helpful. – erik258 Aug 11 '18 at 22:24

0 Answers0