Skip to content

Instantly share code, notes, and snippets.

Created September 30, 2011 08:47
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 anonymous/1253138 to your computer and use it in GitHub Desktop.
Save anonymous/1253138 to your computer and use it in GitHub Desktop.
Neopets Account Pricer
import re, urllib2, cookielib, webbrowser, sys
from time import sleep
from random import randint
from math import ceil
price_data = [e.split('|') for e in open('prices.txt').read().splitlines()]
prices = {}
for i in price_data:
prices[i[0]] = int(i[1])
cj = cookielib.CookieJar()
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))
opener.addheaders = [('User-Agent', 'Mozilla/5.0')]
un = raw_input('Username: ')
pw = raw_input('Password: ')
login = opener.open('http://www.neopets.com/login.phtml', 'username=%s&password=%s' % (un, pw)).read()
if 'forgotpassword.phtml' in login:
print 'Error! Incorrect password.'
sys.exit()
output = ''
np_worth, inv_worth, shop_worth, sdb_worth = 0, 0, 0, 0
print 'Getting inventory...'
inventory = opener.open('http://www.neopets.com/objects.phtml?type=inventory').read()
print 'Getting bank...'
bank = opener.open('http://www.neopets.com/bank.phtml').read()
print 'Getting shop till...'
till = opener.open('http://www.neopets.com/market.phtml?type=till').read()
print 'Collecting NP data...'
on_hand = int(re.findall('=inventory">(.+?)</a>', inventory)[0].replace(',', ''))
print 'You have %d NP on hand.' % on_hand
banked = int(re.findall('align="center" style="font-weight: bold;">(.+?)<', bank)[0].replace(',', '').replace(' NP', ''))
print 'You have %d NP in the bank.' % banked
in_till = int(re.findall('You currently have <b>(.+?)</b>', till)[0].replace(',', '').replace(' NP', ''))
print 'You have %d NP in your shop till.' % in_till
np_worth = on_hand + banked + in_till
print 'Total NP: %d\n\n' % np_worth
output += '<h2>Neopoints</h2>On hand: %d<br />Bank: %d<br />Till: %d<h3>Total NP: %d</h3>' % (on_hand, banked, in_till, np_worth)
print 'Pricing inventory...'
inv_items = {}
items = re.findall('neopointItem"></A><BR>(.+?)<', inventory)
output += '<h2>Inventory</h2>'
for i in items:
try:
inv_worth += prices[i]
inv_items[i] = int(prices[i])
except KeyError:
inv_worth += 0
inv_items[items[i]] = 0
print 'Your inventory is worth about %d NP.\n\n' % inv_worth
inv_items = sorted(inv_items.iteritems(), key = lambda (k, v): v, reverse = True)
for i in inv_items:
output += '%s: %d<br />' % (i)
output += '<h3>Estimated worth: %d</h3>' % inv_worth
print 'Getting shop...'
shop = opener.open('http://www.neopets.com/market.phtml?type=your').read()
shop_items = {}
pages = int(ceil(int(re.findall('Stocked : <b>(\d+)</b>', shop)[0]) / 30.0))
for p in range(pages):
page = opener.open('http://www.neopets.com/market.phtml?order_by=id&type=your&lim=%d' % ((p + 1) * 30)).read()
print 'Pricing page #%d...' % (p + 1)
items = re.findall('width=60 bgcolor=\'#ffffcc\'><b>(.+?)</b>', page)
qty = re.findall('align=center><b>(\d+)</b>', page)
page_worth = 0
for i in range(len(items)):
try:
page_worth += prices[items[i]] * int(qty[i])
shop_items[items[i]] = int(prices[items[i]])
except KeyError:
page_worth += 0
shop_items[items[i]] = 0
shop_worth += page_worth
print 'Page %d is worth about %d NP.' % (p + 1, page_worth)
if p < pages - 1:
print 'Waiting a few seconds for safety...'
sleep(randint(1000, 3000) / 1000.0)
print 'Your shop is worth about %d NP.\n\n' % shop_worth
output += '<h2>Shop</h2>'
shop_items = sorted(shop_items.iteritems(), key = lambda (k, v): v, reverse = True)
for i in shop_items:
output += '%s: %d<br />' % (i)
output += '<h3>Estimated worth: %d</h3>' % shop_worth
print 'Getting SDB...'
sdb = opener.open('http://www.neopets.com/safetydeposit.phtml?offset=0&obj_name=&category=0').read()
sdb_items = {}
pages = int(ceil(int(re.findall('Items:</b> (\d+) ', sdb)[0]) / 30.0))
for p in range(pages):
page = opener.open('http://www.neopets.com/safetydeposit.phtml?offset=%d&obj_name=&category=0' % (p * 30)).read()
print 'Pricing page #%d...' % (p + 1)
items = re.findall('<td align="left"><b>(.+?)<br><span class', page)
qty = re.findall('<td align="center"><b>(\d+)</b>', page)
page_worth = 0
for i in range(len(items)):
try:
page_worth += prices[items[i]] * int(qty[i])
sdb_items[items[i]] = int(prices[items[i]])
except KeyError:
page_worth += 0
sdb_items[items[i]] = 0
sdb_worth += page_worth
print 'Page %d is worth about %d NP.' % (p + 1, page_worth)
if p < pages - 1:
print 'Waiting a few seconds for safety...'
sleep(randint(1000, 3000) / 1000.0)
print 'Your SDB is worth about %d NP.\n\n' % sdb_worth
output += '<h2>Safety Deposit Box</h2>'
sdb_items = sorted(sdb_items.iteritems(), key = lambda (k, v): v, reverse = True)
for i in sdb_items:
output += '%s: %d<br />' % (i)
output += '<h3>Estimated worth: %d</h3>' % sdb_worth
total_worth = np_worth + inv_worth + shop_worth + sdb_worth
print 'Total account worth: %d' % total_worth
output += 'Your account\'s assets total roughly: <h1>%d NP</h1>' % total_worth
open('results.html', 'w').write(output)
webbrowser.open('results.html')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment