Difference between revisions of "Visit url"

From Kolmafia
Jump to navigation Jump to search
imported>Fredg1
m (adding run_choice() in the seeAlso)
Line 1: Line 1:
{{
+
<onlyinclude>{{{{{format|Function2}}}
#vardefine:name|visit_url}}{{
+
|name=User:Philmasterplus/Sandbox
#vardefine:return_type|buffer}}{{
+
|function1.return_type=buffer
 
+
|function1.description=Returns the original HTML source of the page being overridden inside a relay override script.
FunctionPage|
+
|function2.return_type=buffer
name={{#var:name}}|
+
|function2.description=Visits a web page and returns its HTML source.
 
+
|function2.param1=url
function1={{Function|
+
|function2.param1.type=string
name={{#var:name}}|
+
|function2.param1.description=URL of the page to visit. If a relative URL is provided, it is treated as an in-game page.
aggregate={{#var:aggregate}}|
+
|function2.param2=use_POST
return_type={{#var:return_type}}|
+
|function2.param2.type=boolean
return_also={{#var:return_also}}|
+
|function2.param2.optional=yes
}}|
+
|function2.param2.default=true
 
+
|function2.param2.description=If <code>true</code>, makes an HTTP POST request when visiting the URL. Otherwise, makes a GET request.
function2={{Function|
+
|function2.param3=encoded
name={{#var:name}}|
+
|function2.param3.type=boolean
aggregate={{#var:aggregate}}|
+
|function2.param3.optional=yes
return_type={{#var:return_type}}|
+
|function2.param3.default=false
return_also={{#var:return_also}}|
+
|function2.param3.description=If <code>true</code>, KoLmafia assumes that <code>url</code> has already been [[wikipedia:Percent-encoding|URL-encoded]] and will not encode it.
parameter1={{Param|string|page}}|
+
|description=
}}|
+
<p>Returns the HTML from the visited page (not just what is displayed when visiting the page, but all markup) by performing a POST request if {{pspan|use_POST}} is omitted or true, or a GET request if false. Note that for addresses inside of KoL, it is only necessary to supply the page name for {{pspan|page}}, and KoLmafia will populate the rest of the url. However, pages outside of KoL require the full url to be supplied for {{pspan|page}}.</p>
 
 
function3={{Function|
 
name={{#var:name}}|
 
aggregate={{#var:aggregate}}|
 
return_type={{#var:return_type}}|
 
return_also={{#var:return_also}}|
 
parameter1={{Param|string|page}}|
 
parameter2={{Param|boolean|use_POST}}|
 
}}|
 
 
 
function4={{Function|
 
name={{#var:name}}|
 
aggregate={{#var:aggregate}}|
 
return_type={{#var:return_type}}|
 
return_also={{#var:return_also}}|
 
parameter1={{Param|string|page}}|
 
parameter2={{Param|boolean|use_POST}}|
 
parameter3={{Param|boolean|encoded}}|
 
p1desc={{Pspan|page}} is the page to visit|
 
p2desc={{Pspan|use_POST}} is true for a POST request and false for a GET request|
 
p3desc={{Pspan|encoded}} is false if the parameter is not used. If set to true, then the url is assumed to be pre-encoded by the user.|
 
}}|
 
 
 
function_description=Returns the HTML from the visited page (not just what is displayed when visiting the page, but all markup) by performing a POST request if {{pspan|use_POST}} is omitted or true, or a GET request if false. Note that for addresses inside of KoL, it is only necessary to supply the page name for {{pspan|page}}, and KoLmafia will populate the rest of the url. However, pages outside of KoL require the full url to be supplied for {{pspan|page}}.</p>
 
  
 
<p>The version with no parameters is only meaningful in a relay override script; it retrieves the server page that your script is overriding.  All four versions behave slightly differently in a relay script; they retrieve a version of the page with any KoLmafia decorations added, rather than the raw page from the server.</p>
 
<p>The version with no parameters is only meaningful in a relay override script; it retrieves the server page that your script is overriding.  All four versions behave slightly differently in a relay script; they retrieve a version of the page with any KoLmafia decorations added, rather than the raw page from the server.</p>
  
<p>In a POST request, if &pwd is part of the {{pspan|page}} string, it will automatically be replaced by &pwd=###### where ###### is the password hash for the current session. For a GET request the password hash will not be added so you'll want to use {{f|my_hash}}.|
+
<p>In a POST request, if &pwd is part of the {{pspan|page}} string, it will automatically be replaced by &pwd=###### where ###### is the password hash for the current session. For a GET request the password hash will not be added so you'll want to use {{f|my_hash}}.</p>
 
+
|code1={{CodeSample
code1={{CodeSample|
+
  |title=Code Samples
title=Code Samples|
+
  |description=Visits your private character sheet.
description=Visits your private character sheet.|
+
  |code=
code=
+
<syntaxhighlight lang="java">
<syntaxhighlight>
 
 
visit_url( "charsheet.php" );
 
visit_url( "charsheet.php" );
</syntaxhighlight>}}|
+
</syntaxhighlight>}}
 
+
|code2={{CodeSample
code2={{CodeSample|
+
  |description=Visits Google.
description=Visits Google.|
+
  |code=
code=
+
<syntaxhighlight lang="java">
<syntaxhighlight>
 
 
visit_url( "http://www.google.com/");
 
visit_url( "http://www.google.com/");
 
</syntaxhighlight>}}
 
</syntaxhighlight>}}
{{CodeSample|
+
|code3={{CodeSample
description=Given a player ID, parse a player's profile for his name.|
+
  |description=Given a player ID, parse a player's profile for his name.
code=
+
  |code=
<syntaxhighlight>
+
<syntaxhighlight lang="java">
string getPlayerName( int playerID ) {
+
string [int] getPlayerName( int playerID ) {
 
   string playerName;
 
   string playerName;
  
Line 88: Line 62:
 
   return playerName;
 
   return playerName;
 
}
 
}
</syntaxhighlight>}}|
+
</syntaxhighlight>}}
see_also={{SeeAlso|make_url|run_choice}}|
+
|see_also={{SeeAlso|make_url|run_choice}}
cli_equiv=The CLI commands "text", or "http:" will also visit specified urls.|
+
|cli_equiv=The CLI commands "text", or "http:" will also visit specified urls.
more_info=Note that this function returns an empty string if the page does not exist or if the request times out.|
+
|more_info=Note that this function returns an empty string if the page does not exist or if the request times out.
special=Some people claim that it is better to [[append|append]] the result of this function to a buffer instead of saving it as a string when dealing with an especially large page to avoid time-out. This claim appears to be purely circumstantial and unsupported by the mafia's code.|
+
|special=Some people claim that it is better to [[append|append]] the result of this function to a buffer instead of saving it as a string when dealing with an especially large page to avoid time-out. This claim appears to be purely circumstantial and unsupported by the mafia's code.
}}
+
|{{{1|}}}
 
+
}}</onlyinclude>
 
[[Category:Adventuring]]
 
[[Category:Adventuring]]

Revision as of 11:56, 20 December 2020

Function Syntax

Nothing to see here, move along. Nothing to see here, move along.( Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along. )

Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.

Nothing to see here, move along. Nothing to see here, move along.( Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along., Nothing to see here, move along.? Nothing to see here, move along. = Nothing to see here, move along. )

Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.
  • Nothing to see here, move along.: Nothing to see here, move along.

Returns the HTML from the visited page (not just what is displayed when visiting the page, but all markup) by performing a POST request if use_POST is omitted or true, or a GET request if false. Note that for addresses inside of KoL, it is only necessary to supply the page name for page, and KoLmafia will populate the rest of the url. However, pages outside of KoL require the full url to be supplied for page.

The version with no parameters is only meaningful in a relay override script; it retrieves the server page that your script is overriding. All four versions behave slightly differently in a relay script; they retrieve a version of the page with any KoLmafia decorations added, rather than the raw page from the server.

In a POST request, if &pwd is part of the page string, it will automatically be replaced by &pwd=###### where ###### is the password hash for the current session. For a GET request the password hash will not be added so you'll want to use my_hash().

Code Samples

Visits your private character sheet.

visit_url( "charsheet.php" );

Visits Google.

visit_url( "http://www.google.com/");

Given a player ID, parse a player's profile for his name.

string [int] getPlayerName( int playerID ) {
   string playerName;

   // use visit_url() to store the player profile's HTML in a string
   string playerProfile = visit_url("showplayer.php?who=" + playerID);
   if ( contains_text(playerProfile, "<td>Sorry, this player could not be found.</td>") ) {
      print("Player " + playerID + "does not exist.","red");
      return "";
   }
   // find player name in the string returned by visit_url()
   matcher match_name = create_matcher("<b>([^<]+)</b> \\(#" + playerID + "\\)<br>", playerProfile);
   if ( match_name.find() ) {
      playerName = match_name.group(1);
   }
   else {
      print("Problem occurred while parsing for player name","red");
      return "";
   }

   return playerName;
}

CLI Equivalent

The CLI commands "text", or "http:" will also visit specified urls.

See Also

make_url() | run_choice()

More Information

Note that this function returns an empty string if the page does not exist or if the request times out.

Special

Some people claim that it is better to append the result of this function to a buffer instead of saving it as a string when dealing with an especially large page to avoid time-out. This claim appears to be purely circumstantial and unsupported by the mafia's code.