Posts Tagged ‘bot

28
Apr
09

[Javascript/Php] Neopets Auction Bot Project (2)

A much faster script than my previous version. Instead of querying each item individually, it loops through the items in the list and uses the queryNeopetItem to send the data to another php page, which pareses the items and returns the prices of each of them. When the data is returned, it is then split and the table is formatted depending on what action should be taken (light green=no data, medium green=more expensive than data, grey = neofriend only, blue = make profit)

Neopets auction highlight


And below is the source for the greasemonkey script:

// ==UserScript==
// @name           AuctionBot test v2
// @namespace      Sim0n NeoPets
// @include        http://www.neopets.com/auctions.phtml
// ==/UserScript==

function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}

function queryNeopetItem(item, complete) {
	url = "myphpquery.php?items=" + item;
	GM_xmlhttpRequest({
		method: "POST",
		url: url,
		headers:{'Content-type':'application/x-www-form-urlencoded'},
		data:encodeURI(""),
		onload: function(xhr) { complete(xhr.responseText); }
	});
}
function comp(text) {
	var items = text.split("\n");
	for(var i = 0;i<20;i++) {
		if(items[i] == "") {
			table[0].rows[i+1].setAttribute("bgcolor","#ccffd0");
		} else {
			var item = items[i].split("\t");
			var cur_price = table[0].rows[i+1].cells[6].innerHTML.replace(/<b>(\d*)<\/b> NP/, "$1");
			if(Number(cur_price)<=Number(item[0])) {
				if(table[0].rows[i+1].cells[3].innerHTML.toLowerCase().indexOf("<b>[nf]</b>") != -1) {
					table[0].rows[i+1].setAttribute("bgcolor","#aaafa0");
				} else {
					alert(table[0].rows[i+1].cells[3].innerHTML)
					table[0].rows[i+1].setAttribute("bgcolor","#00ffff");
				}
			} else {
				table[0].rows[i+1].setAttribute("bgcolor","#aaffd0");
			}
		}
	}
}
var table = getElementsByAttribute(document.body, "table", "align", "center");
var queryString = "";
for(var i=1;i<table[0].rows.length - 1;i++) {
	queryString = queryString + table[0].rows[i].cells[2].innerHTML.replace(/<a href=\".*\">(.*)<\/a>/, "$1") + "|";
}
queryString = queryString + table[0].rows[table[0].rows.length-1].cells[2].innerHTML.replace(/<a href=\".*\">(.*)<\/a>/, "$1")
queryNeopetItem(queryString, comp);

And for the php query:

<?php
	$dbhost = 'a';
	$dbuser = 'b';
	$dbpass = 'd';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('0');
	$dbname = 'e';
	mysql_select_db($dbname);

	$items = explode("|", $_GET['items']);
	for($i=0;$i<count($items);$i++) {
		$sqlQuery = "ITEM_NAME = '$items[$i]'";
		$result = mysql_query("SELECT * FROM  Neopets_Items WHERE $sqlQuery");
		$any = false;
		while($row = mysql_fetch_array($result))
  		{
			$any = true;
  			echo $row['MAX_PRICE'] . "\t" . $row['SELL_PRICE'] . "\n";
  		}
		if($any==false) {
			echo "\n";
		}
	}
	mysql_close($conn);
?> 
28
Apr
09

[Javascript/Php] Neopets Auction Bot Project (1)

Ive been asked by somebody to create a bot for the game Neopets that will view the most recent auctions and bid upon items which on which money can be easily made. One of the constraints for this project, was that it must be made using Javascript + Greasemonkey, so no Perl or Python (or whatever it is people use to code bots on the net these days! Like I know -_-).
My initial attempt (below) uses the GM_xmlhttpRequest to query a database of items (that’s another project ^_^ – lots of faces today?), and it works very nicely, except for one small thing. Its ridiculously slow. It query the database for each of the items, and then either marks it with a green colour to indicate that it is loaded, but you will not make money off it, or will highlight the row brown to show that you will make money off the auction.
Remeber, its v. slow!
Still, here is the source code as it is a nice example of a recursive function and sending & retrieving data to other websites using Greasemonkey.

// ==UserScript==
// @name           Auc7i0nb0t t3s7
// @namespace      n30p375
// @include        http://www.neopets.com/auctions.phtml
// ==/UserScript==

//Some function from google - kinda overkill, but meh.
function getElementsByAttribute(oElm, strTagName, strAttributeName, strAttributeValue){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	var oAttributeValue = (typeof strAttributeValue != "undefined")? new RegExp("(^|\\s)" + strAttributeValue + "(\\s|$)") : null;
	var oCurrent;
	var oAttribute;
	for(var i=0; i<arrElements.length; i++){
		oCurrent = arrElements[i];
		oAttribute = oCurrent.getAttribute && oCurrent.getAttribute(strAttributeName);
		if(typeof oAttribute == "string" && oAttribute.length > 0){
			if(typeof strAttributeValue == "undefined" || (oAttributeValue && oAttributeValue.test(oAttribute))){
				arrReturnElements.push(oCurrent);
			}
		}
	}
	return arrReturnElements;
}

function queryNeopetItem(item, complete) {
	url = "url to my item query page.php?item=" + item;
	GM_xmlhttpRequest({
		method: "POST",
		url: url,
		headers:{'Content-type':'application/x-www-form-urlencoded'},
		data:encodeURI(""),
		onload: function(xhr) { complete(xhr.responseText); }
	});
}
var x = 1;
var table = getElementsByAttribute(document.body, "table", "align", "center");
var item_name = table[0].rows[x].cells[2].innerHTML.replace(/<a href=\".*\">(.*)<\/a>/, "$1");
function comp(text) {
	var ta = text.split("\n");
	max_price = ta[1];
	sell_price = ta[2];
	if(x<table[0].rows.length) {
		item_name = table[0].rows[x].cells[2].innerHTML.replace(/<a href=\".*\">(.*)<\/a>/, "$1");
		queryNeopetItem(item_name, comp)
		table[0].rows[x-1].setAttribute("bgcolor","#ccffd0");
		if(ta[1].indexOf("www.mywebhostsname.com") == -1) {
			var str = table[0].rows[x-1].cells[6].innerHTML.replace(/<b>(\d*)<\/b> NP/, "$1");
			if(Number(str)<=Number(max_price)) {
				table[0].rows[x-1].setAttribute("bgcolor","#dc9423");
			}
		}
		x++;
	} else {
		item_name = table[0].rows[x-1].cells[2].innerHTML.replace(/<a href=\".*\">(.*)<\/a>/, "$1");
		table[0].rows[x-1].setAttribute("bgcolor","#ccffd0");
		if(ta[1].indexOf("www.mywebhostsname.com") == -1) {
			var str = table[0].rows[x-1].cells[6].innerHTML.replace(/<b>(\d*)<\/b> NP/, "$1");
			if(str<=max_price) {
				table[0].rows[x-1].setAttribute("bgcolor","#dc9423");
			}
		}
		table[0].appendChild(document.createTextNode("Complete"));
	}
}
queryNeopetItem(item_name, comp);

url to my item query page.php?item is a page that querys the database and returns the items details (Max Price and Sell Price). The above function is recursive, as queryNeopetItem calls the function comp (which parses the text returned from the httpRequest) which, in turn calls queryNeopetItem again, until all the rows items have been queried.

<php
	$dbhost = 'x';
	$dbuser = 'y';
	$dbpass = 'z';
	$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('0');
	$dbname = 'a';
	mysql_select_db($dbname);

	$item = $_GET['item'];
	$max = $_GET['max'];
	$sell = $_GET['sell']; 

	$sqlQuery = "";

		if($item != "") {
			$sqlQuery = "ITEM_NAME = '$item'";
		}
		if($max != "") {
			if($sqlQuery != "") {
				$sqlQuery = $sqlQuery . " AND MAX_PRICE = '$max'";
			} else {
				$sqlQuery = "MAX_PRICE = '$max'";
		}
		}
		if($sell != "") {
			if($sqlQuery != "") {
				$sqlQuery = $sqlQuery . " AND SELL_PRICE = '$sell'";
			} else {
				$sqlQuery = "SELL_PRICE = '$sell'";
			}
		}

		$result = mysql_query("SELECT * FROM  Neopets_Items WHERE $sqlQuery");

		while($row = mysql_fetch_array($result))
  		{
  			echo $row['ITEM_NAME'] . "\n" . $row['MAX_PRICE'] . "\n" . $row['SELL_PRICE'];
  		}

	mysql_close($conn);
?> 

That code is kind of overkill for the query that the script makes, however it also allows you to query by a combination of the items properties which will be used later on in the project (probably)