Java Neopets Wrapper

Discussion in 'Code Snippets and Tutorials' started by Zer0, Aug 30, 2008.

  1. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    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):
    1.  
    2. import java.io.*;
    3. import java.util.*;
    4.  
    5. public class Account implements Serializable {
    6.     static final long serialVersionUID = 1l; // class versioning system
    7.     String user, password, pin;
    8.     CookieManager cookies;
    9.     Connection conn;
    10.    
    11.     public Account( String user, String password, String pin ) {
    12.         this.user = user;
    13.         this.password = password;
    14.         this.pin = pin;
    15.         cookies = new CookieManager();
    16.         conn = new Connection( "http://www.neopets.com", cookies );
    17.         updateSession();
    18.     }
    19.    
    20.     public String updateSession() {
    21.         conn.post( "/hi.phtml", new String[][]{ { "destination", "username" }, { "/", user } } );
    22.         String page = conn.post( "/login.phtml", new String[][]{ { "username", "password", "destination" }, { user, password, "/" } } );
    23.        
    24.         if( page == null )
    25.             updateSession();
    26.        
    27.         // check for errors here (invalid info, frozen, etc)
    28.         // invalid info look-up is !cookies.hasCookie( "neologin" )
    29.         // frozen look-up is <blockquote>TEXT<blockquote>
    30.        
    31.         return cookies.toString();
    32.     }
    33.    
    34.     public int getNP() {
    35.         String page = conn.get( "/bank.phtml" );
    36.        
    37.         try {
    38.             return Integer.parseInt( StringUtil.textBetween( page, "inventory\">", "<" ).replaceAll( ",", "" ) );
    39.         } catch( Exception e1 ) {
    40.             return -1;
    41.         }
    42.     }
    43.    
    44.     public int getBankNP() {
    45.         String page = conn.get( "/bank.phtml" );
    46.        
    47.         try {
    48.             return Integer.parseInt( StringUtil.textBetween( page, "Current Balance</b> : ", " " ).replaceAll( ",", "" ) );
    49.         } catch( Exception e1 ) {
    50.             return -1;
    51.         }
    52.     }
    53.    
    54.     public void depositNP( String a ) {
    55.         conn.get( "/bank.phtml" );
    56.         conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount" }, { "deposit", a } } );
    57.     }
    58.    
    59.     public void withdrawNP( String amount ) {
    60.         conn.get( "/bank.phtml" );
    61.        
    62.         if( pin == "" )
    63.             conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount" }, { "withdraw", amount } } );
    64.         else
    65.             conn.post( "/process_bank.phtml", new String[][]{ { "type", "amount", "pin" }, { "withdraw", amount, pin } } );
    66.     }
    67.    
    68.     public void withdrawTill() {
    69.         String page = conn.get( "/market.phtml?type=till" );
    70.        
    71.         if( page == null )
    72.             return;
    73.        
    74.         String amount = StringUtil.textBetween( page, "You currently have <b>", " " ).replaceAll( ",", "" );
    75.        
    76.         if( amount.equals( "0" ) )
    77.             return;
    78.        
    79.         conn.post( "/process_market.phtml", new String[][]{ { "type", "amount" }, { "withdraw", amount } } );
    80.     }
    81.    
    82.     public void stockItem( String name ) {
    83.         String page = conn.get( "/objects.phtml?type=inventory" );
    84.         String objectID = StringUtil.textBetween( page, "(", ")", page.lastIndexOf( "openwin(", page.indexOf( name ) ) );
    85.        
    86.         if( objectID != null ) {
    87.             conn.get( "/iteminfo.phtml?obj_id=" + objectID );
    88.             page = conn.post( "/useobject.phtml", new String[][]{ { "obj_id", "action" }, { objectID, "stockshop"  } } );
    89.            
    90.         }
    91.     }
    92.    
    93.     public void depositItem( String name ) {
    94.         String page = conn.get( "/objects.phtml?type=inventory" );
    95.         String objectID = StringUtil.textBetween( page, "(", ")", page.lastIndexOf( "openwin(", page.indexOf( name ) ) );
    96.        
    97.         if( objectID != null ) {
    98.             conn.get( "/iteminfo.phtml?obj_id=" + objectID );
    99.             page = conn.post( "/useobject.phtml", new String[][]{ { "obj_id", "action" }, { objectID, "safetydeposit"  } } );
    100.            
    101.         }
    102.     }
    103.    
    104.     public int buyItem( String name, int maxPrice, int numSearches ) throws ShopWizardBanException {
    105.         conn.get( "/market.phtml?type=wizard" );
    106.        
    107.         int minPrice = Integer.MAX_VALUE;
    108.         int price = -1;
    109.         String page, url = null;
    110.        
    111.         for( int i = 0; i < numSearches; i++ ) {
    112.             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 ) } } );
    113.            
    114.             if( page.indexOf( "I did not find anything" ) != -1 )
    115.                 continue;
    116.            
    117.             if( page.indexOf( "Whoa there" ) != -1 ) {
    118.                 Console.writeln( "SHOP WIZARD BANNED!" );
    119.                 throw new ShopWizardBanException( Long.parseLong( StringUtil.textBetween( page, "about <b>", "<" ) )*60l*1000l );
    120.             }
    121.            
    122.             price = Integer.parseInt( StringUtil.textBetween( page, "buy_cost_neopoints=", "\"" ) );
    123.            
    124.             if( price < minPrice ) {
    125.                 minPrice = price;
    126.                 url = StringUtil.textBetween( page, "bgcolor=\"#F6F6F6\"><a href=\"", "\"" );
    127.             }
    128.         }
    129.        
    130.         if( url != null ) {
    131.             page = conn.get( url );
    132.             conn.get( "/" + StringUtil.textBetween( page, "valign=top><a href='", "'" ) );
    133.             Console.writeln( "Bought: " + name + " (" + minPrice + " NP)" );
    134.             return minPrice;
    135.         }
    136.        
    137.         return -1;
    138.     }
    139.    
    140.     public int buyItem( String name, int maxPrice ) throws ShopWizardBanException {
    141.         return buyItem( name, maxPrice, 3 );
    142.     }
    143.    
    144.     public int buyItem( String name ) throws ShopWizardBanException {
    145.         return buyItem( name, 99999 );
    146.     }
    147.    
    148.     public int priceCheck( String name, int numSearches ) throws ShopWizardBanException {
    149.         conn.get( "/market.phtml?type=wizard" );
    150.         int min = 100000, price;
    151.         String page;
    152.        
    153.         for( int i = 0; i < numSearches; i++ ) {
    154.             page = conn.post( "/market.phtml", new String[][]{ { "type", "feedset", "shopwizard", "table", "criteria", "min_price", "max_price" }, { "process_wizard", "0", name, "shop", "exact", "0", "99999" } } );
    155.            
    156.             if( page == null ) {
    157.                 i--;
    158.                 continue;
    159.             }
    160.            
    161.             if( page.indexOf( "I did not find anything" ) != -1 )
    162.                 continue;
    163.            
    164.             if( page.indexOf( "Whoa there" ) != -1 )
    165.                 throw new ShopWizardBanException( Long.parseLong( StringUtil.textBetween( page, "about <b>", "<" ) )*60l*1000l );
    166.            
    167.             price = Integer.parseInt( StringUtil.textBetween( page, "buy_cost_neopoints=", "\"" ) );
    168.            
    169.             if( price < min )
    170.                 min = price;
    171.         }
    172.        
    173.         return min;
    174.     }
    175.    
    176.     public int priceCheck( String name ) throws ShopWizardBanException {
    177.         return priceCheck( name, 5 );
    178.     }
    179.    
    180.     public void priceItem( String name, String id, String oldPrice, int newPrice ) {
    181.         System.out.println( name + "\t" + id + "\t" + oldPrice + "\t" + newPrice );
    182.         conn.post( "/market.phtml", new String[][]{ { "order_by", "type", "lim", "obj_name" }, { "id", "your", "30", name } } );
    183.         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 } } );
    184.         conn.get( "/market.phtml?type=your&view=&obj_name= " + name + "&lim=30&order_by=id" );
    185.     }
    186.    
    187.     public void priceItem( String name, int newPrice ) {
    188.         String page = conn.post( "/market.phtml", new String[][]{ { "order_by", "type", "lim", "obj_name" }, { "id", "your", "30", name } } );
    189.         String id = StringUtil.textBetween( page, "obj_id_1' value='", "'" );
    190.         String oldPrice = StringUtil.textBetween( page, "maxlength=5 value='", "'" );
    191.     }
    192.    
    193.     // 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)
    194.     public final String getEncryptedPassword() {
    195.         char[] arr = password.toCharArray();
    196.        
    197.         for( int i = 0; i < arr.length; i++ )
    198.             arr[i]--;
    199.        
    200.         return new String( arr );
    201.     }
    202.    
    203.     // simple password decryption, should be inverse function of getEncryptPassword()
    204.     public final String decryptPassword( String s ) {
    205.         char[] arr = s.toCharArray();
    206.        
    207.         for( int i = 0; i < arr.length; i++ )
    208.             arr[i]++;
    209.        
    210.         return new String( arr );
    211.     }
    212.    
    213.     public final String getEncryptedPin() {
    214.         return Integer.toString( Integer.parseInt( pin )*13 );
    215.     }
    216.    
    217.     public static final String decryptPin( String s ) {
    218.         return Integer.toString( Integer.parseInt( s )/13 );
    219.     }
    220.    
    221.     public Connection getConnection() {
    222.         return conn;
    223.     }
    224.  
    225.     public CookieManager getCookies() {
    226.         return cookies;
    227.     }
    228.  
    229.     public String getPin() {
    230.         return pin;
    231.     }
    232.  
    233.     public String getUser() {
    234.         return user;
    235.     }
    236.    
    237.     public String toString() {
    238.         return "Username: " + user;
    239.     }
    240. }
    Code (Text):
    1. public class ShopWizardBanException extends Exception {
    2.     static final long serialVersionUID = 1l;
    3.    
    4.     long time;
    5.    
    6.     public ShopWizardBanException() {
    7.         time = 1800000l;
    8.     }
    9.    
    10.     public ShopWizardBanException( long time ) {
    11.         this.time = time;
    12.     }
    13.    
    14.     public long getTime() {
    15.         return time;
    16.     }
    17. }
    18.  
    Code (Text):
    1.  
    2. // code taken from org.apache.commons.lang.StringUtils since I could not find mine
    3. public final class StringUtils {
    4.     public static String textBetween( String str, String start, String end ) {
    5.         if( str == null || start == null || end == null )
    6.             return null;
    7.        
    8.         int start = str.indexOf( start );
    9.        
    10.         if( start != -1 ) {
    11.             int end = str.indexOf( end, start + start.length() );
    12.            
    13.             if( end != -1 )
    14.                 return str.substring( start + start.length(), end );
    15.  
    16.         }
    17.        
    18.         return null;
    19.     }
    20. }
    Most of it is pretty self-explanatory IMO if you know Java.

    To create a new Account:
    Code (Text):
    1. 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):
    1. try {
    2.     // code that invokes a SW -using method
    3. } catch( ShopWizardBanException e1 ) {
    4.     Thread.sleep( e1.getTime() );
    5. }
     
    Heya_old likes this.
  2. zav75

    zav75 Level I

    Joined:
    Feb 28, 2007
    Messages:
    76
    Likes Received:
    6
    Location:
    Canada, Province of Québec
    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 :D (don't worry it's not in the same language :p )
     
  3. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    :D thanks, I tried to include all the common operations
    and of course I don't mind, otherwise I wouldn't have released it :p

    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.
     
    xZel likes this.
  4. Tom Kearns

    Tom Kearns Newbie

    Joined:
    Apr 20, 2008
    Messages:
    6
    Likes Received:
    0
    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.
     
  5. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    Please look at my post above ;)

     
  6. lazypando

    lazypando Level IV

    Joined:
    Nov 16, 2006
    Messages:
    3,326
    Likes Received:
    94
    oomph GRAVEDIG :)

    o_O whaa...?
     
  7. Zer0

    Zer0 Level IV

    Joined:
    Mar 2, 2008
    Messages:
    3,037
    Likes Received:
    180
    Location:
    Home sweet home
    ? You're not being very coherent today :p
    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?
     
  8. lazypando

    lazypando Level IV

    Joined:
    Nov 16, 2006
    Messages:
    3,326
    Likes Received:
    94
    >___<;;
    yeah i was just wondering what it does since it didn't seem to be compatible with your StringUtils class