$Webget

By SReject on Sep 17, 2011

This snippet downloads the specified webpage and saves it into a specified bvar. then returns either the bvar or an error message.

Syntax: $WebGet(,<&bvar>)

Example:

alias wgexmp {
  if (&?* iswm $webget(http://www.hawkee.com/snippet/8993/,&page)) {
    var %x = 1
    while ($bvar(&page,%x,4000).text) {
      echo -a $v1
      inc %x 4000
    }
  }
  else {
    echo 04 -a * /wgexmp: $v2
  }
}
alias webget {
  if (!$isid) return
  if ($0 != 2) { echo -ac info * $!webget: Invalid parameters specified | halt }
  if (!$len($1) || $regex($1,[" ])) { echo -ac info * $!webget: Invalid URL Specified | halt }
  if (&?* !iswm $2) { echo -ac info * $!webget: Specified output must be a binvary variable | halt }

  var %err, %f, %com = $ticks 
  while ($com(webget- $+ %com)) inc %com
  var %com = webget- $+ %com, %f = write $qt($mircdir $+ %com $+ .vbs)

  %f on error resume next
  %f set F = CreateObject("Scripting.FileSystemObject").CreateTextFile(" $+ $Mircdir $+ %com $+ .tmp")
  %f if (err.number <> 0) then
  %f   err.clear
  %f   WScript.Quit
  %f end if
  %f set C = CreateObject("msxml2.xmlhttp.6.0")
  %f if (err.number <> 0) then
  %f   err.clear
  %f   set C = CreateObject("msxml2.xmlhttp.4.0")
  %f   if (err.number <> 0) then
  %f     err.clear
  %f     set C = CreateObject("msxml2.xmlhttp.3.0")
  %f     if (err.number <> 0) then
  %f       err.clear
  %f       set C = CreateObject("msxml2.xmlhttp.2.6")
  %f       if (err.number <> 0) then
  %f         F.WriteLine("ERR 1")
  %f         F.Close
  %f         err.clear
  %f         wscript.quit
  %f       end if
  %f     end if
  %f   end if
  %f end if
  %f C.Open "GET", $qt( $iif(:// !isin $1,http://) $+ $1) $+ , false
  %f C.Send
  %f if (Hex(err.Number) = "800C0005") then
  %f   F.Write("ERR 2")
  %f elseif (Err.Number <> 0) then
  %f   F.Write("ERR 3")
  %f elseif (C.Status <> 200) then
  %f   F.Write("ERR 4")
  %f else
  %f   F.Write("OK" & vbcrlf & C.ResponseText)
  %f end if
  %f F.Close
  %f err.clear
  %f wscript.Quit
  %f = $qt($mircdir $+ %com $+ .tmp)

  .comopen %com WScript.Shell
  if ($comerr || !$com(%com)) {
    %err = Unable to open WScript.Shell
  }
  elseif (!$com(%com,Run,1,bstr," $+ $mircdir $+ %com $+ .vbs",uint,0,bool,true)) %ERR = An error occured while trying to run $qt($mircdir $+ %com $+ .vbs")
  elseif (!$isfile(%f)) %ERR = Unable to create a tmp file to store the request content.
  elseif ($read(%f,n,1) == OK) {
    if ($bvar($2,0)) bunset $2
    bread %f 4 $calc($file(%f).size -5) $2
  }   
  elseif ($v1 == ERR 1) %ERR = Unable to create an httprequest object
  elseif ($v1 == ERR 2) %ERR = Unable to access the httprequest object
  elseif ($v1 == ERR 3) %ERR = Unable to connect to $qt($1)
  elseif ($v1 == ERR 4) %ERR = Unable to access $qt($1)

  :error
  if ($error) { %ERR = $iif(%ERR,$v1,$error) | reseterror }
  if ($com(%com)) .comclose $v1
  if ($isfile($mircdir $+ %com $+ .vbs)) .remove $qt($mircdir $+ %com $+ .vbs)
  if ($isfile($mircdir $+ %com $+ .tmp)) . remove $qt($mircdir $+ %com $+ .tmp)
  return $iif(%ERR,$v1,$2)
}

Comments

Sign in to comment.
Frenetic   -  Sep 17, 2011

Aye, nice.

 Respond  
SReject   -  Sep 17, 2011

Yea. mIRC sockets, though not difficult to understand, can be confusing depending on the protocol, not to mention they are async, so you have to save data until the required data from the socket is retrieved. This way it's all done inline, and mIRC won't freeze while waiting for the socket

 Respond  
Frenetic   -  Sep 17, 2011

So it works kind-of the same way as, the webget in C. Interesting, nice snippet. :)

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.