This wrapper uses my generic Java HTTP wrapper found here: viewtopic.php?f=15&t=18332 It provides additional functionality to the basic HTTP wrapper, including login, bank withdraw/deposit, withdraw till, price checking, etc. Code (Text): import java.io.*; import java.util.*; public class Account implements Serializable { static final long serialVersionUID = 1l; // class versioning system String user, password, pin; CookieManager cookies; Connection conn; public Account( String user, String password, String pin ) { this.user = user; this.password = password; this.pin = pin; cookies = new CookieManager(); conn = new Connection( "http://www.neopets.com", cookies ); updateSession(); } public String updateSession() { conn.post( "/hi.phtml", new String[][]{ { "destination", "username" }, { "/", user } } ); String page = conn.post( "/login.phtml", new String[][]{ { "username", "password", "destination" }, { user, password, "/" } } ); if( page == null ) updateSession(); // check for errors here (invalid info, frozen, etc) // invalid info look-up is !cookies.hasCookie( "neologin" ) // frozen look-up is <blockquote>TEXT<blockquote> return cookies.toString(); } public int getNP() { String page = conn.get( "/bank.phtml" ); try { return Integer.parseInt( StringUtil.textBetween( page, "inventory\">", "<" ).replaceAll( ",", "" ) ); } catch( Exception e1 ) { return -1; } } public int getBankNP() { String page = conn.get( "/bank.phtml" ); try { return Integer.parseInt( StringUtil.textBetween( page, "Current Balance</b> : ", " " ).replaceAll( ",", "" ) ); } catch( Exception e1 ) { return -1; } } public void depositNP( String a ) { conn.get( "/bank.phtml" ); conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount" }, { "deposit", a } } ); } public void withdrawNP( String amount ) { conn.get( "/bank.phtml" ); if( pin == "" ) conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount" }, { "withdraw", amount } } ); else conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount", "pin" }, { "withdraw", amount, pin } } ); } public void withdrawTill() { String page = conn.get( "/market.phtml?type=till" ); if( page == null ) return; String amount = StringUtil.textBetween( page, "You currently have <b>", " " ).replaceAll( ",", "" ); if( amount.equals( "0" ) ) return; conn.post( "/process_market.phtml", new String[][]{ { "type", "amount" }, { "withdraw", amount } } ); } public void stockItem( String name ) { String page = conn.get( "/objects.phtml?type=inventory" ); String objectID = StringUtil.textBetween( page, "(", ")", page.lastIndexOf( "openwin(", page.indexOf( name ) ) ); if( objectID != null ) { conn.get( "/iteminfo.phtml?obj_id=" + objectID ); page = conn.post( "/useobject.phtml", new String[][]{ { "obj_id", "action" }, { objectID, "stockshop" } } ); } } public void depositItem( String name ) { String page = conn.get( "/objects.phtml?type=inventory" ); String objectID = StringUtil.textBetween( page, "(", ")", page.lastIndexOf( "openwin(", page.indexOf( name ) ) ); if( objectID != null ) { conn.get( "/iteminfo.phtml?obj_id=" + objectID ); page = conn.post( "/useobject.phtml", new String[][]{ { "obj_id", "action" }, { objectID, "safetydeposit" } } ); } } public int buyItem( String name, int maxPrice, int numSearches ) throws ShopWizardBanException { conn.get( "/market.phtml?type=wizard" ); int minPrice = Integer.MAX_VALUE; int price = -1; String page, url = null; for( int i = 0; i < numSearches; i++ ) { page = conn.post( "/market.phtml", new String[][]{ { "type", "feedset", "shopwizard", "table", "criteria", "min_price", "max_price" }, { "process_wizard", "0", name, "shop", "exact", "0", Integer.toString( maxPrice ) } } ); if( page.indexOf( "I did not find anything" ) != -1 ) continue; if( page.indexOf( "Whoa there" ) != -1 ) { Console.writeln( "SHOP WIZARD BANNED!" ); throw new ShopWizardBanException( Long.parseLong( StringUtil.textBetween( page, "about <b>", "<" ) )*60l*1000l ); } price = Integer.parseInt( StringUtil.textBetween( page, "buy_cost_neopoints=", "\"" ) ); if( price < minPrice ) { minPrice = price; url = StringUtil.textBetween( page, "bgcolor=\"#F6F6F6\"><a href=\"", "\"" ); } } if( url != null ) { page = conn.get( url ); conn.get( "/" + StringUtil.textBetween( page, "valign=top><a href='", "'" ) ); Console.writeln( "Bought: " + name + " (" + minPrice + " NP)" ); return minPrice; } return -1; } public int buyItem( String name, int maxPrice ) throws ShopWizardBanException { return buyItem( name, maxPrice, 3 ); } public int buyItem( String name ) throws ShopWizardBanException { return buyItem( name, 99999 ); } public int priceCheck( String name, int numSearches ) throws ShopWizardBanException { conn.get( "/market.phtml?type=wizard" ); int min = 100000, price; String page; for( int i = 0; i < numSearches; i++ ) { page = conn.post( "/market.phtml", new String[][]{ { "type", "feedset", "shopwizard", "table", "criteria", "min_price", "max_price" }, { "process_wizard", "0", name, "shop", "exact", "0", "99999" } } ); if( page == null ) { i--; continue; } if( page.indexOf( "I did not find anything" ) != -1 ) continue; if( page.indexOf( "Whoa there" ) != -1 ) throw new ShopWizardBanException( Long.parseLong( StringUtil.textBetween( page, "about <b>", "<" ) )*60l*1000l ); price = Integer.parseInt( StringUtil.textBetween( page, "buy_cost_neopoints=", "\"" ) ); if( price < min ) min = price; } return min; } public int priceCheck( String name ) throws ShopWizardBanException { return priceCheck( name, 5 ); } public void priceItem( String name, String id, String oldPrice, int newPrice ) { System.out.println( name + "\t" + id + "\t" + oldPrice + "\t" + newPrice ); conn.post( "/market.phtml", new String[][]{ { "order_by", "type", "lim", "obj_name" }, { "id", "your", "30", name } } ); conn.post( "/process_market.phtml", new String[][]{ { "type", "order_by", "view", "obj_id_1", "oldcost_1", "cost_1", "back_to_inv[" + id +"]", "lim", "obj_name" }, { "update_prices", "id", "", id, oldPrice, Integer.toString( newPrice ), "0", "30", name } } ); conn.get( "/market.phtml?type=your&view=&obj_name= " + name + "&lim=30&order_by=id" ); } public void priceItem( String name, int newPrice ) { String page = conn.post( "/market.phtml", new String[][]{ { "order_by", "type", "lim", "obj_name" }, { "id", "your", "30", name } } ); String id = StringUtil.textBetween( page, "obj_id_1' value='", "'" ); String oldPrice = StringUtil.textBetween( page, "maxlength=5 value='", "'" ); } // simple password encryption, I didn't bother to make a better one or look up one (I suggest you replace it because this code is open-source) public final String getEncryptedPassword() { char[] arr = password.toCharArray(); for( int i = 0; i < arr.length; i++ ) arr[i]--; return new String( arr ); } // simple password decryption, should be inverse function of getEncryptPassword() public final String decryptPassword( String s ) { char[] arr = s.toCharArray(); for( int i = 0; i < arr.length; i++ ) arr[i]++; return new String( arr ); } public final String getEncryptedPin() { return Integer.toString( Integer.parseInt( pin )*13 ); } public static final String decryptPin( String s ) { return Integer.toString( Integer.parseInt( s )/13 ); } public Connection getConnection() { return conn; } public CookieManager getCookies() { return cookies; } public String getPin() { return pin; } public String getUser() { return user; } public String toString() { return "Username: " + user; } } Code (Text): public class ShopWizardBanException extends Exception { static final long serialVersionUID = 1l; long time; public ShopWizardBanException() { time = 1800000l; } public ShopWizardBanException( long time ) { this.time = time; } public long getTime() { return time; } } Code (Text): // code taken from org.apache.commons.lang.StringUtils since I could not find mine public final class StringUtils { public static String textBetween( String str, String start, String end ) { if( str == null || start == null || end == null ) return null; int start = str.indexOf( start ); if( start != -1 ) { int end = str.indexOf( end, start + start.length() ); if( end != -1 ) return str.substring( start + start.length(), end ); } return null; } } Most of it is pretty self-explanatory IMO if you know Java. To create a new Account: Code (Text): Account account = new Account( username, password, pin ); The pin should be "" (empty String, not null) if there is no pin. Also, a ShopWizardBanException is thrown in any method that requires the use of the SW. The exception comes with a variable that holds the duration of the ban in milliseconds, so handling the ShopWizardBanException should be: Code (Text): try { // code that invokes a SW -using method } catch( ShopWizardBanException e1 ) { Thread.sleep( e1.getTime() ); }
Great idea to put bank operations included, when I think about it we use that often. GJ ^^. I may copy you and put it in my wrapper (don't worry it's not in the same language )
thanks, I tried to include all the common operations and of course I don't mind, otherwise I wouldn't have released it In fact, I hereby waive all copyright to my publicly released code (I release the code into the public domain, feel free to copy, plagiarize, etc.
If you would like your code to be of greatest usage to the general public, especially this community, it might be advisible that you use the GNU General Public License for future programs and autobuyers. Doing this grants people to modify, re-distribute, and freely use your applications either for gratis or not. I think having all of the applications on Neofriends.net run entirely on free (or libre to distinguish freedom as in "liberty", and not necessarily freeware) software would be of great benefit to the community.
? You're not being very coherent today That just gets the id of the item which is then used to specify actions for that item (stock in shop, deposit to sdb, etc). Or are you wondering how that piece of code works?
>___<;; yeah i was just wondering what it does since it didn't seem to be compatible with your StringUtils class