mIRC Home    About    Download    Register    News    Help

Print Thread
socket script #178065 04/06/07 10:05 PM
Joined: May 2005
Posts: 449
bwr30060 Offline OP
Fjord artisan
OP Offline
Fjord artisan
Joined: May 2005
Posts: 449
I'm trying to make this script that will look up each callsign in my log on www.qrz.com and get what country they're from. I know that the qrz site was having problems today, but it's up now, and the script still doesn't work. I noticed that %temptext was equal to something like "HTTP 200 OK" at one point, which I don't think is a good thing. The file that this is reading is a simple text file with each callsign on its own line. Here's the code:
Code:
on 1:sockopen:qrz: {
  set %url   /detail/ $+ $read("C:\Documents and Settings\Owner\Desktop\hams.txt",%ln)
  sockwrite -n $sockname GET %url HTTP/1.1
  sockwrite -n $sockname Host: www.qrz.com $+ $crlf $+ $crlf 
}
on 1:sockread:qrz:{  
  if ($sockerr) {    
    echo -a Error.    
    halt  
  }  
  else {    
    sockread %temptext 
    if ($mid(%temptext,41,7) == Country) {
      var %head = <tr bgcolor="#FFFFFF"><td align="right">
      var %mid = </td><td><b>
      var %end = </b></td></tr>
      %text = %text $+ $chr(32) $+ $remove(%temptext,%head,%mid,%end)
      write "C:\Documents and Settings\Owner\Desktop\hams2.txt" %text
      unset %text
    }
    elseif ($left(%temptext,14) == <title>QRZ.COM) {
      var %head = <title>QRZ.COM
      var %mid = </td><td><b><a href="/callsign/N4BWR">
      var %end = </title>
      set %text $remove(%temptext,%head,%end)
    }
  }
  %ln = %ln + 1  
  sockclose qrz
  if (%ln >= $lines("C:\Documents and Settings\Owner\Desktop\hams.txt")) {
    $qrz
  }
}
alias qrz {
  sockopen qrz www.qrz.com 80
}


If you want one or two callsigns to test it out, you can try NA1SS (the space station) and W1AW (ARRL headquarters). Thanks.

Re: socket script [Re: bwr30060] #178075 05/06/07 12:27 AM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
1. It sort of worked for me by removing the /sockclose command.

2. Maybe use this custom identifier to strip the HTML tags, to make life a little easier.

Code:
alias -l html {
  var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x,&nbsp;)
  return %x
}

Re: socket script [Re: bwr30060] #178077 05/06/07 12:28 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
Remove your sockclose line and you'll have no problems. Or, adjust it so that it doesn't close after the first line is received. Btw, it appears as though you're going to run into * /set: line too long errors.


Invision Support
#Invision on irc.irchighway.net
Re: socket script [Re: Riamus2] #178079 05/06/07 12:38 AM
Joined: May 2005
Posts: 449
bwr30060 Offline OP
Fjord artisan
OP Offline
Fjord artisan
Joined: May 2005
Posts: 449
When I took out /sockclose, I got errors because the socket was already open, and I've only been able to get this to successfully write the first callsign and country.

Re: socket script [Re: bwr30060] #178081 05/06/07 12:58 AM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
Code:
on 1:sockopen:qrz: {
  set %url /detail/ $+ $read("C:\Documents and Settings\Owner\Desktop\hams.txt",%ln)
  sockwrite -n $sockname GET %url HTTP/1.1
  sockwrite -n $sockname Host: www.qrz.com $+ $crlf $+ $crlf 
}

on 1:sockread:qrz:{  
  if ($sockerr) {    
    echo -a Error.    
    halt  
  }  
  else {    
    sockread %temptext 
    if (Country isin %temptext) {
      write "C:\Documents and Settings\Owner\Desktop\hams2.txt" $html(%temptext)
      %ln = %ln + 1
      if (%ln <= $lines("C:\Documents and Settings\Owner\Desktop\hams.txt"){ 
        set %url $read("C:\Documents and Settings\Owner\Desktop\hams.txt",%ln)
        qrz
      }
    }
  }
}

alias qrz {
  if (!%ln) set %ln 1
  sockclose qrz  
  sockopen qrz www.qrz.com 80
}

alias -l html {
  var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x,&nbsp;)
  return %x
}


I think it may need working on but try that.

Re: socket script [Re: SladeKraven] #178083 05/06/07 01:28 AM
Joined: May 2005
Posts: 449
bwr30060 Offline OP
Fjord artisan
OP Offline
Fjord artisan
Joined: May 2005
Posts: 449
Thanks. I got this figured out before your latest reply, Andy. Here's a shortened version of the output. These are some of the ham radio contacts I've made lately:
Quote:

PY2BN Country:BRAZIL
TI2JJP Country:COSTA RICA
LU6FOV Country:ARGENTINA
KI4MJO Country:USA
YV4DYJ Country:VENEZUELA
NP4A Country:USA
ZP6DYA Country:BRAZIL
YO9HP Country:ROMANIA
KB0RGS Country:USA
YU1XA Country:SERBIA
CT3MD Country:PORTUGAL
W1AW Country:USA
KI4MJO Country:USA
GB6MD Country:England - U.K.
WW2SUB Country:USA
NI4BK Country:USA
HB9Z Country:SWITZERLAND
F5BBD Country:FRANCE


I still need to work with it a bit to add the part about stopping when it has read the last line of the file, but this is what I have:
Code:
on 1:sockread:qrz:{  
  if ($sockerr) {    
    echo -a Error.    
    halt  
  }  
  else {    
    sockread %temptext 

    if ($mid(%temptext,41,8) == Country:) {
      var %head = <tr bgcolor="#FFFFFF"><td align="right">
      var %mid = </td><td><b>
      var %end = </b></td></tr>
      %text = %text $+ $chr(32) $+ $remove(%temptext,%head,%mid,%end)
      write "C:\Documents and Settings\Owner\Desktop\hams2.txt" %text
      unset %text
    }
    elseif ($left(%temptext,14) == <title>QRZ.COM) {
      var %head = <title>QRZ.COM
      var %mid = </td><td><b><a href="/callsign/ $+ %call $+ ">
      var %end = </title>
      set %text $remove(%temptext,%head,%end)
    }

  }
  if (%temptext = </html>) {
    %ln = %ln + 1
    set %url   /detail/ $+ $read("C:\Documents and Settings\Owner\Desktop\hams.txt",%ln)
    set %call   $read("C:\Documents and Settings\Owner\Desktop\hams.txt",%ln)
    sockwrite -n $sockname GET %url HTTP/1.1
    sockwrite -n $sockname Host: www.qrz.com $+ $crlf $+ $crlf
  }
}

Re: socket script [Re: bwr30060] #178085 05/06/07 01:31 AM
Joined: Oct 2004
Posts: 8,330
Riamus2 Offline
Hoopy frood
Offline
Hoopy frood
Joined: Oct 2004
Posts: 8,330
You can include the sockclose in multiple ways. One way is to just do it before opening a new one as shown. You can also do something like:

Code:
if (</html> isin %temptext) { sockclose qrz }


... or something similar. Note that you can adjust that so that it closes after the last line of data that you *need*. In other words, if you only needed the first 10 lines of text, you could make it close after that first 10 lines so that you don't waste bandwidth downloading a lot of extra html that isn't needed.


Invision Support
#Invision on irc.irchighway.net
Re: socket script [Re: Riamus2] #178089 05/06/07 01:41 AM
Joined: Dec 2002
Posts: 3,547
S
SladeKraven Offline
Hoopy frood
Offline
Hoopy frood
S
Joined: Dec 2002
Posts: 3,547
In my above code add a simple else that should do the trick.

Code:
on 1:sockread:qrz:{  
  if ($sockerr) {    
    echo -a Error.    
    halt  
  }  
  else {    
    sockread %temptext 
    if (Country isin %temptext) {
      echo -a $read(hams.txt,%ln) is $html(%temptext)
      %ln = %ln + 1
      if (%ln <= $lines(hams.txt)) { 
        set %url $read(hams.txt,%ln)
        qrz
      }
     else { unset %ln }
    }
  }
}


It unsets the line number when it's finished.

PY2BN Country:BRAZIL
TI2JJP Country:COSTA RICA
LU6FOV Country:ARGENTINA
KI4MJO Country:USA
YV4DYJ Country:VENEZUELA
NP4A Country:USA
ZP6DYA Country:BRAZIL
YO9HP Country:ROMANIA
KB0RGS Country:USA
YU1XA Country:SERBIA
CT3MD Country:PORTUGAL
W1AW Country:USA
KI4MJO Country:USA
GB6MD Country:England - U.K.
WW2SUB Country:USA
NI4BK Country:USA
HB9Z Country:SWITZERLAND
F5BBD Country:FRANCE

If it still doesn't work for you, there are others here available to help. It's my bed time now. grin