Page 1 of 3 1 2 3 LastLast
Results 1 to 10 of 21

Thread: Visit_url()

  1. #1
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Visit_url()

    The following piece of code is actually just a snippet I wrote up real quick for an example. It logs the amount of 2 specific items in my store to a file.

    Code:
     mmbquant = shop_amount($item[meat maid body]);
     gaquant = shop_amount($item[Gnollish autoplunger]);
     cli_execute("mirror " + filename);
     print("Gnollish autoplunger=" + gaquant);
     print("meat maid body=" + mmbquant);
     cli_execute("mirror");
    Now each time this script would be run = 2 server hits. If it were that simple, I'd go with it, however I want to log several items instead of just 2. Fact is I don't care to log thos items, they were just a copy/paste. I do however want to log maybe a hundred or more other items, and that to me is way too many server hits.

    I would like to use visit_url() to obtain all the information in just 1 server hit, but the only information so far I can find that would relate is the following:

    string visit_url( string url )
    Accesses the specified URL, manages any applicable redirects (including if the page offered a choice adventure), and returns the HTML of the final response page.

    boolean contains_text( string source, string query )
    Reveals if the query string is a substring of the source string.
    Are there more functions for manipulating strings returned by visit_url()? If so, could someone point me towards information about them, and how to use them?

  2. #2
    Senior Member
    Join Date
    Mar 2006
    Location
    New Zealand
    Posts
    386

    Default Re: Visit_url()

    We also have a
    Code:
    string substring( string Text, int StartFrom)
    string substring( string Text, int StartFrom, int EndAt)
    You could just store the HTML returned by hitting your store manager in a string var and then reuse that to check for each item... (With the substring command it allows it quite well)

    Edit: Fixed some typo's...
    It's not like we hurt people or anything... Just steal from 'em... I'm not into hurting people, you know...

  3. #3
    Developer Veracity's Avatar
    Join Date
    Mar 2006
    Location
    The Unseelie Court
    Posts
    11,549

    Default Re: Visit_url()

    I'd suggest you also look at index_of(), which came in when substring() did.

    Code:
    int index_of( string source, string search ) 
    int index_of( string source, string search, int start ) 
    string substring( string source, int end ) 
    string substring( string source, int begin, int end )
    index_of() lets you find where your desired text starts in the string returned by visit_url() and substring() lets you trim down the string to focus in on that area of the text.
    Ph'nglui mglw'nafh Cthulhu
    R'lyeh wgah-nagl fhtagn.

  4. #4
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Visit_url()

    In addition to the functions Veracity mentioned, v8.9 adds the following functions to your list of abilities to locate strings (I'll add them to the wiki in the middle next week, if nobody else adds them):

    int last_index_of( string source, string search )
    int last_index_of( string source, string search, int stop )
    string replace_string( string source, string search, string replace )

    Also included are some regular expression type functions that I use a lot in developing KoLmafia's internal functions. The first splits a string, using the given regular expression as a delimiter. It uses Java's built in regular expression handler, not PERLs, so be careful when you're writing scripts.

    FS: string [int] split_string( string source, string regex )
    EX: string [int] test = split_string( "Hello4world", "\\d" );

    The above function call will split the string into two strings, "Hello" and "World" which will be returned in a mapping that closely resembles an array (actually, it is an array, which only exists as an internal data type, and may throw array-index-out-of-bounds exceptions, so be careful.

    Another useful one is grouping things by regular expressions. The function signature is as follows (oh, right, "FS" means "function signature" and "EX" means "example"):

    FS: string [int,int] group_string( string source, string regex )
    EX: string [int,int] test = group_string( "This is a test", "([a-z]+) " );

    If you understand regular expressions, then you'll be able to use this feature really easily. If not, then it'll be a little complicated because you need to learn regular expressions first. After understanding regular expressions, the above function generates a map with the following key-value pairs:

    test[0][0] => "his "
    test[0][1] => "his"
    test[1][0] => "is "
    test[1][1] => "is"
    test[2][0] => "a "
    test[2][1] => "a"

    That's more or less it as far as string parsing goes. There's a few other functions that were added along the way, including:

    float square_root( float value )
    monster [int] get_monsters( location l )

  5. #5
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Visit_url()

    Hey thanks everyone! Now I got some work to do!

    Hmm, this is the first sign I have seen of overloaded functions in kolmafia. Do I have the ability to write overloaded functions into a script also?

    Edit: In my studying and preparing to write, I came up with another small question. Is there a function available which returms the length of a string?

  6. #6
    Senior Member
    Join Date
    Mar 2006
    Location
    New Zealand
    Posts
    386

    Default Re: Visit_url()

    [quote author=efilnikufecin link=topic=451.msg2236#msg2236 date=1158929920]
    Edit: In my studying and preparing to write, I came up with another small question. Is there a function available which returns the length of a string?
    [/quote]
    I don't think there is. I was about to request it before but I found a workaround for what I was doing, (Searching for a item, save the index_of and then use another index_of for the HTML thats after the number by making it only start searching from the starting index_of, this gives you a "start" and "end" which you can substring and then usually further cut down to whatever you want, although this is from my KMail parsing so I'm not entirely sure if the same tactic would work for what your trying to do)

    But all of that aside, a len( string) (Heh yeah... the excel name for it) would be nice though if your passing it to search a item and you want for it to set the index to the end of the name, unless "last_index_of()" does that since I haven't actually messed around with that but I always just assumed it returned the last instance of a string rather then the first. (since the item name would have variating length you cant just use a simple +number after it.)
    It's not like we hurt people or anything... Just steal from 'em... I'm not into hurting people, you know...

  7. #7
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Visit_url()

    OK, can anyone figure out why I am getting no output at all from this script?
    Attached Files

  8. #8
    Developer
    Join Date
    Mar 2006
    Posts
    1,367

    Default Re: Visit_url()

    I think it's because of some code centralization that I did in order to make sure that KoLmafia never double-parsed things, which resulted in store management being on the list of restricted URLs.


    KoLmafiaASH.java

    Code:
    		public ScriptValue visit_url( ScriptVariable string )
    		{
    			String location = string.toStringValue().toString();
    			if ( KoLRequest.shouldIgnore( location ) )
    				return STRING_INIT;
    
    			boolean wasChoice = location.indexOf( "choice.php" ) != -1;
    
    			KoLRequest request = new KoLRequest( getClient(), location, true );
    			request.run();
    
    			if ( !wasChoice && request.getURLString().indexOf( "choice.php" ) != -1 && getBooleanProperty( "makeBrowserDecisions" ) )
    				request.handleChoiceResponse( request );
    
    			StaticEntity.externalUpdate( location, request.responseText );
    			return new ScriptValue( request.fullResponse == null ? "" : request.fullResponse );
    		}


    KoLRequest.java

    Code:
    	private boolean shouldIgnoreResults()
    	{	return shouldIgnore( formURLString ) || formURLString.startsWith( "messages.php" );
    	}
    
    	public static boolean shouldIgnore( String formURLString )
    	{
    		return formURLString.startsWith( "mall.php" ) || formURLString.startsWith( "searchmall.php" ) ||
    			formURLString.startsWith( "clan" ) || formURLString.startsWith( "manage" ) || formURLString.indexOf( "chat" ) != -1;
    	}

  9. #9
    Senior Member
    Join Date
    Mar 2006
    Posts
    729

    Default Re: Visit_url()

    I see now after some confusion that this has been changed.
    The following:

    Code:
    cli_execute("mirror test.txt");
    print(visit_url("managestore.php"));
    cli_execute("mirror");
    Makes quite a mess out of things in the cli, carriage returns seem to be inserted all over the place in the output file and so on, but over all I can manage to work it out with time...time to get busy!!!

  10. #10
    Senior Member
    Join Date
    Mar 2006
    Location
    New Zealand
    Posts
    386

    Default Re: Visit_url()

    It would probably be alot easier if you just opened up a "view source" window of "managestore.php" and work from there.
    PS. If you want any help feel free to ask, my K-Mail parser is pretty much been put on "hold" until I find a use for it and so a store manager price finder might be a interesting little project. Your just trying to find the prices of the items you already have in store right?
    It's not like we hurt people or anything... Just steal from 'em... I'm not into hurting people, you know...

Similar Threads

  1. visit_url help.. again
    By bordemstirs in forum Community Support
    Replies: 1
    Last Post: 08-18-2013, 05:37 PM
  2. help with visit_url
    By bordemstirs in forum Scripting Discussion
    Replies: 4
    Last Post: 05-13-2012, 07:13 AM
  3. visit_url()
    By bordemstirs in forum Community Support
    Replies: 1
    Last Post: 04-27-2011, 08:39 AM
  4. visit_url
    By relyk in forum Community Support
    Replies: 4
    Last Post: 01-01-2011, 10:27 AM
  5. visit_url outside of KOL
    By dawdawdo in forum Scripting Discussion
    Replies: 7
    Last Post: 05-24-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •