PHP Code:
#Import Needed Classes:
import urllib2, re
import os #Used for normalizing pyton strings like folders wich use "/" in folder instead of "\" when running on windows debug server
import ConfigParser # Config parser class (.cfg files)
import time #Time module , used for sleeping
import random #used to randomise numbers and get a random number between a range
#import sqllink # Sql link used to connect to the sql database
from sqllink import sqllink
from InventoryManager import InventoryManager
#Setup Default vars / classes
confighandler = ConfigParser.RawConfigParser() #Used for .cfg files
#import Image
from PIL import Image #Pyamf class used for amf request (slightly edited)
class autobuyer:
#Initialize this class
def __init__(self,acc,newsql,theuserid,debugmode):
self.acc = acc
self.newsql = newsql
self.debugmode = debugmode
self.theuserid = theuserid
self.shopid = str(self.newsql.getsettingrecode("abshopid"))
self.ablist = []
self.nextabtime = self.newsql.getsettingrecode("nextabtime")
def downloadimage(self,imgurl):
print "dl image"
cookie_handler = urllib2.HTTPCookieProcessor(self.acc.cj)
if self.debugmode ==0:
#outputfolder = 'C:\xampp\htdocs\logs\'C:\xampp\htdocs\neo\logs
outputfolder = '/root/bin/cache/'
else:
outputfolder='C:/Users/rdd/Desktop/amf new/cache/'
f = open(outputfolder + self.acc.user + '.jpg','wb') #w+ makes file if not exist
thedata = self.acc.get(imgurl)
print "test"
#thedata = thedata.replace("\n", "");
f.write(thedata)
f.close()
def loglogic(self,html,theitemname):
#do logic for logging based on html
#Bought item
if html.find("added to your inventory") > 0:
filename = "Abbuy" + str(time.time())
html = "Bought Item : " + theitemname
sql = "INSERT INTO dailylogs(id,logname, logresult , userid , username) VALUES (NULL,'Autobuyer','%s','%s','%s')" % (html,str(self.theuserid),self.acc.user)
self.newsql.insertsql(sql)
print sql
#self.writestringtofile( filename,html)
print "Bought Item " + theitemname
def checkitemeexist(self,itemname):
foundone=0
for x in self.ablist:
if x == itemname:
foundone=1
return foundone
return foundone
def dotick2(self):
stockhtml = self.acc.get("http://www.neopets.com/objects.phtml?type=shop&obj_type=" + self.shopid,"http://www.neopets.com/space/index.phtml")
self.itemdblogic(stockhtml)
def docaptcha(self):
if self.debugmode ==0:
#outputfolder = 'C:\xampp\htdocs\logs\'C:\xampp\htdocs\neo\logs
outputfolder = '/root/bin/cache/'
else:
outputfolder='C:/Users/rdd/Desktop/amf new/cache/'
filename = outputfolder + self.acc.user + '.jpg'
im = Image.open(filename)
#colors = {}
#for color in im.getdata():
# colors[color] = colors.get(color, 0) + 1
#print colors[x]
###############
width, height = im.size
pixel_list = list(im.getdata())
lowest_brite=9999
for y in range(0, height,10):
for x in range(0, width, 10):
#print "x=" + str(x) + "y=" + str(y)
pixelRGB = im.getpixel((x,y))
R,G,B = pixelRGB
rgb = pixel_list[x:y]
brightness = sum([R,G,B])/3 ##0 is dark (black) and 255 is bright (white)
if brightness < lowest_brite:
lowest_brite=brightness
lowestx=x
lowesty=y
print lowest_brite
return [lowestx,lowesty]
# print width
def dotick(self):
print "abtick"
if self.ablist == []:
self.ablist = self.loadabfile()
#print self.ablist
#return 1
stockhtml = self.acc.get("http://www.neopets.com/objects.phtml?type=shop&obj_type=" + self.shopid,"http://www.neopets.com/space/index.phtml")
#for x in self.ablist:
#print x
endpos1=0
html = stockhtml
while not html.find("haggle.phtml?obj_info_id=",endpos1)== -1:
#Start url
startpos1 = html.find("haggle.phtml?obj_info_id=",endpos1)
endpos1 = html.find('"',startpos1) #Item id end
haggleurl="http://www.neopets.com/" + html[startpos1:endpos1]
#print "Haggleurl=" + haggleurl
endpos1 = endpos1 + 4
#Start item name
startpos2=html.find("wish to purchase ",endpos1) + 17
endpos2=html.find(" at ",startpos2)
theitemname=html[startpos2:endpos2]
print theitemname
#Start ITEM PRICE
startpos3=html.find(" at ",startpos2)+ 4
endpos3=html.find(" NP",startpos3)
theitemprice = html[startpos3:endpos3]
theitemprice=theitemprice.replace(",","")
#print "theitemprice=" + theitemprice
if self.checkitemeexist(theitemname) == 1:
print "Buying item " + theitemname
html=self.acc.get(haggleurl ,"http://www.neopets.com/objects.phtml?type=shop&obj_type=" + self.shopid)
#print html
if html.find("captcha_show.phtml?_x_pwned=") > 1: #img dosent exist so item expired
cappos1 = html.find("captcha_show.phtml?_x_pwned=")+ 28
capendpos1=html.find('"',cappos1)
capurl= html[cappos1:capendpos1]
print "http://www.neopets.com/captcha_show.phtml?_x_pwned=" + capurl
self.downloadimage("http://www.neopets.com/captcha_show.phtml?_x_pwned=" + capurl)
captchapoints = self.docaptcha()
capx= captchapoints[0]
capy= captchapoints[1]
print "captcha x" + str(capx)
startprice=html.find("less than ") + 11
endprice=html.find(" Neopoints",startprice)
print "price=" + theitemprice
postdata = {'current_offer': theitemprice, "x": str(capx), "y": str(capy)}
html=self.acc.post("http://www.neopets.com/haggle.phtml" , postdata , "http://www.neopets.com/haggle.phtml")
self.loglogic(html,theitemname)
self.nextabtime = time.time() + 300 #Next ab time = + 5 mins
self.newsql.setsetting("nextabtime",self.nextabtime) #Save Settings
#self.itemdblogic(stockhtml) #Itemdblogic
#Find captcha img url
def writestringtofile(self,file_name, str_to_be_written):
# print self.outputfolder + file_name + ".htm"
if self.debugmode ==0:
#outputfolder = 'C:\xampp\htdocs\logs\'C:\xampp\htdocs\neo\logs
outputfolder = '/root/bin/cache/'
else:
outputfolder='C:/Users/rdd/Desktop/amf new/cache/'
file_writer = open(outputfolder + file_name + ".htm", 'w+')
file_writer.write(str(str_to_be_written))
#print ("String written successfully.")
file_writer.close()
def loadabfile(self):
print "tik2"
outputfolder='/var/www/html/uploads'
sql = """SELECT itemlist FROM ablist WHERE neoowner='%s'"""% (self.theuserid)
row = self.newsql.execdbwithret(sql)
print str(row[0])
f = open(outputfolder + row[0])
lines = f.readlines()
f.close()
lines2=[]
for x in lines:
lines2.append(x.replace("\n",""))
return lines2
def itemdblogic(self,html):
print "item db logic"
#print html
endpos1=0
while not html.find("haggle.phtml?obj_info_id=",endpos1)== -1:
startpos1 = html.find("haggle.phtml?obj_info_id=",endpos1) + 25
endpos1 = html.find('&',startpos1) #Item id end
print "itemid=" + html[startpos1:endpos1]
theitemid=html[startpos1:endpos1]
endpos1 = endpos1 + 4
#Start item name
startpos2=html.find("wish to purchase ",endpos1) + 17
endpos2=html.find(" at ",startpos2)
theitemname=html[startpos2:endpos2]
#Start ITEM PRICE
startpos3=html.find(" at ",startpos2)+ 4
endpos3=html.find(" NP",startpos3)
theitemprice = html[startpos3:endpos3]
theitemprice=theitemprice.replace(",","")
print "theitemprice=" + theitemprice
#Start image url
startpos4=html.find('src="',endpos3)+ 5
endpos4=html.find('"',startpos4)
imgurl = html[startpos4:endpos4]
print "imageurl=" + html[startpos4:endpos4]
#Start alt text
startpos5=html.find('alt="',endpos4)+ 5
endpos5=html.find('"',startpos5)
print "alttext=" + html[startpos5:endpos5]
itemtext=html[startpos5:endpos5]
#Check if item already exist in db via name if so we need to execute update if not we insert
row=0
sql = """SELECT id from fquestjobs WHERE accid='%s'"""% (theitemname)
row = self.newsql.execdbwithret(sql)
if row == None: #Item does not exist in db so insert it
print "New Item found , inserting..."
sql = """INSERT INTO itemdb(id,itemname,itemid,itemtext,imageurl,lastshopprice,shopid) VALUES (NULL,'%s','%s','%s' ,'%s','%s','%s')""" % (theitemname,theitemid,itemtext,imgurl,theitemprice,self.shopid)
self.newsql.insertsql(sql)
else:
print "old item found , updating..."
#sql = "UPDATE bagalogs SET count=count + 1 WHERE playid='%s'" % (1)
sql = """UPDATE itemdb SET itemname='%s' ,itemid='%s', itemtext='%s', imageurl='%s', lastshopprice='%s', shopid='%s' WHERE itemname='%s'""" % (theitemname,theitemid,itemtext,imgurl,theitemprice,self.shopid,theitemname)
self.newsql.insertsql(sql)