Created
September 30, 2011 18:09
-
-
Save anonymous/1254533 to your computer and use it in GitHub Desktop.
Neopets Account Pricer (Python 3 only)
This file contains 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 re, urllib.request, urllib.error, urllib.parse, http.cookiejar, 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]) | |
class prevent_redirect(urllib.request.HTTPRedirectHandler): | |
def http_error_302(self, req, fp, code, msg, headers): | |
target = urllib.response.addinfourl(fp, headers, req.get_full_url()) | |
target.status = code | |
target.code = code | |
return target | |
cj = http.cookiejar.CookieJar() | |
opener = urllib.request.build_opener(prevent_redirect, urllib.request.HTTPCookieProcessor(cj)) | |
opener.addheaders = [('User-Agent', 'Mozilla/5.0')] | |
un = input('Username: ') | |
pw = input('Password: ') | |
login = opener.open('http://www.neopets.com/login.phtml', ('username=%s&password=%s' % (un, pw)).encode('ascii')).read() | |
if b'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().decode('utf-8') | |
print('Getting bank...') | |
bank = opener.open('http://www.neopets.com/bank.phtml').read().decode('utf-8') | |
print('Getting shop till...') | |
till = opener.open('http://www.neopets.com/market.phtml?type=till').read().decode('utf-8') | |
print('Getting stock portfolio...') | |
portfolio = opener.open('http://www.neopets.com/stockmarket.phtml?type=portfolio').read().decode('utf-8') | |
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) | |
if 'don\'t own shares' in portfolio: | |
stocks = 0 | |
else: | |
stocks = int(re.findall('<td align="center"><b>(.+?)</b>', portfolio)[-2].replace(',', '')) | |
print('You have %d NP in stocks.' % stocks) | |
np_worth = on_hand + banked + in_till + stocks | |
print('Total NP: %d\n\n' % np_worth) | |
output += '<h2>Neopoints</h2>On hand: %d<br />Bank: %d<br />Till: %d<br />Stocks: %d<h3>Total NP: %d</h3>' % (on_hand, banked, in_till, stocks, 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[i] = 0 | |
print('Your inventory is worth about %d NP.\n\n' % inv_worth) | |
inv_items = sorted(iter(inv_items.items()), key = lambda k_v: k_v[1], 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().decode('utf-8') | |
shop_items = {} | |
pages = int(ceil(int(re.findall('Stocked : <b>(.+?)</b>', shop)[0].replace(',', '')) / 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().decode('utf-8') | |
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(iter(shop_items.items()), key = lambda k_v1: k_v1[1], 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().decode('utf-8') | |
sdb_items = {} | |
pages = int(ceil(int(re.findall('Items:</b> (.+?) ', sdb)[0].replace(',', '')) / 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().decode('utf-8') | |
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(iter(sdb_items.items()), key = lambda k_v2: k_v2[1], 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