mIRC Homepage

Socket problems

Posted By: RusselB

Socket problems - 06/12/05 03:48 AM

Code:
 alias find.a.person { 
  if (!$isid) { 
    set %lastname $1 
    set %firstname $2 
    set %street.num $gettok($did(Observer,11),1,32)
    set %street.name $gettok($did(Observer,11),2-,32)
    set %apt $did(Observer,13)
    sockopen findap findaperson.canada411.ca 80 
  } 
} 
on *:sockopen:findap:{ 
  unset %match
  unset %page*
  sockwrite -n $sockname GET $+(/10649/search/Find_Person?firstname_begins_with=1&firstname=,%firstname,&name_begins_with=1&name=,%lastname,&city_zip=Sarnia&state_id=ON) HTTP/1.1 
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
} 
on *:sockread:findap:{ 
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc 
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      did -ra Observer 15 %nc
      set %match found
    }
    elseif (*search_id* iswm %nc) && !%search_id {
      set %search_id $gettok(%nc,2,34)
      set %search_id $gettok(%search_id,-1,61)
    }
    elseif ($+(*,%lastname,*,%firstname,*,Total*) iswm %nc) {
      var %nc = $gettok(%nc,-2,32)
      var %nc = $gettok(%nc,-1,62)
      set %pages $calc($int($calc(%nc / 10)) + 1)
      set %pages $calc(%pages - 1)
    }
    elseif (</html* iswm %nc) || %match {
      sockclose findap
      /*
      if !%match && !%pages {
        inc %page
        sockopen findap $+ %page findaperson.canada411.ca 80
      }
      */
    }
  } 
}
/*
on *:sockopen:findap $+ %page:{
  set %limit $calc(%page * 10 + 1)
  sockwrite -n $sockname GET $+(/10649/search/Alpha_Limit?search_id=,%search_id,&old_search_type=Find_Person&sort=&alpha_limit=&lower=,%limit,) HTTP/1.1
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
}
on *:sockread:findap $+ %page:{
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc 
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      did -ra Observer 15 %nc
      set %match found
    }
  }
  elseif (</html* iswm %nc) || %match {
    sockclose findap
    if !%match && !%pages {
      inc %page
      sockopen findap $+ %page findaperson.canada411.ca 80
    }
  }
} 
*/
 

That code works fine, even with the remarked areas as is, if the information that I want is on the first page from the website, however, there's a chance that it might not be, in which case I want to check all other pages until it comes across a match (presuming that there is a match). The remarked code is what I came up with, but it doesn't seem to work, as I've tested it with data that would be on the second of two pages, and the match isn't coming up. I'm not getting any error messages either.

Sample data to the site Smith, B in Sarnia, ON

Specific address that I was using for test purposes: 1423 Windemere Cres

Hopefully someone can correct my code so that the information is read correctly, no matter what page the site puts it on (site only puts 10 listings per page).

I would also appreciate an explanation of the changes that are made.

I know that part of the problem is getting the value for %search_id correct. It appears the location that I'm referencing is incorrect.

Edited: %search_id now returning correct value
Posted By: RusselB

Re: Socket problems - 10/12/05 09:54 AM

Any ideas, or better yet, solutions?
Posted By: genius_at_work

Re: Socket problems - 11/12/05 12:17 AM

Code:
alias find.a.person { 
  if (!$isid) { 
[color:green]    set %lastname smith
    set %firstname b
    set %street.num 1423
    set %street.name Windemere Cres
    set %apt[/color] 
    sockopen findap findaperson.canada411.ca 80 
  } 
} 
on *:sockopen:findap:{ 
  unset %match
  unset %page*
  sockwrite -n $sockname GET $+(/10649/search/Find_Person?firstname_begins_with=1&firstname=,%firstname,&name_begins_with=1&name=,%lastname,&city_zip=Sarnia&state_id=ON) HTTP/1.1 
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
} 
on *:sockread:findap:{ 
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      [color:green]echo -s did -ra Observer 15 %nc[/color]
      set %match found
    }
    elseif (*search_id* iswm %nc) && !%search_id {
      set %search_id $gettok(%nc,2,34)
      set %search_id $gettok(%search_id,-1,61)
    }
    elseif ($+(*,%lastname,*,%firstname,*,Total*) iswm %nc) {
      var %nc = $gettok(%nc,-2,32)
      var %nc = $gettok(%nc,-1,62)
      set %pages $calc($int($calc(%nc / 10)) + 1)
      set %pages $calc(%pages - 1)
    }
    elseif [color:red]([/color](</html* iswm %nc) || [color:red]([/color]%match[color:red]))[/color] {
      sockclose findap
     if [color:red](([/color]!%match[color:red])[/color] && [color:red]([/color][color:blue]%pages[/color][color:red]))[/color] {
        inc %page
        sockopen findap $+ %page findaperson.canada411.ca 80
      }
    }
  } 
}

on *:sockopen:[color:blue]$([/color]findap $+ %page[color:blue])[/color]:{
  set %limit $calc(%page * 10 + 1)
  sockwrite -n $sockname GET $+(/10649/search/Alpha_Limit?search_id=,%search_id,&old_search_type=Find_Person&sort=&alpha_limit=&lower=,%limit,) HTTP/1.1
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
}

on *:sockread:[color:blue]$([/color]findap $+ %page[color:blue])[/color]:{
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      echo -a did -ra Observer 15 %nc
      set %match found
      sockclose findap
    }
  }
  elseif [color:red]([/color](</html* iswm %nc) || [color:red]([/color]%match[color:red]))[/color] {
    sockclose findap
    if [color:red](([/color]!%match[color:red])[/color] && [color:red]([/color][color:blue]%pages[/color][color:red]))[/color] {
      inc %page
      sockopen findap $+ %page findaperson.canada411.ca 80
    }
  }
} 


Above,

GREEN = changes I made for testing purposes only. The above code will work without the dialog. Type /find.a.person and it will echo the result in the status window.

RED = Added brackets. These missing backets may or may not have affected the operation, but they are in good coding style.

BLUE = The main problems with the code. Explanation of the problems:

I changed this code: findap $+ %page to this: $(findap $+ %page) in the second SOCKOPEN and SOCKREAD events. You need the $() to evaluate the $+ and %page in an event.

And I changed this code: if !%match && !%pages to this: if ((!%match) && (%pages)) in the last elseif of each SOCKREAD event. The problem was the !%pages part. That line basically said, IF <there is no match> AND <there are no other pages> search the next page.

I hope that helps you. wink

-genius_at_work
Posted By: RusselB

Re: Socket problems - 11/12/05 06:14 PM

Thanks for the assistance, and it almost works perfectly..however, when I run it with the rest of my dialog, rather than as a stand alone, I get the following in my status window
Quote:
(519) 542-0612
0Windemere%20C


I changed your
echo -s did -ra Observer 15 %nc to
echo -s %nc
did -ra Observer 15 %nc


In theory, I think, it should've stopped as soon as it had the first match (the phone number), but obviously it didn't.
Posted By: genius_at_work

Re: Socket problems - 11/12/05 07:02 PM

I don't have your dialog, so I can't seem to duplicate your error. However, I did notice an error that I had missed in the code. In the second SOCKREAD event, there is this command twice:

/sockclose findap

Since that SOCKREAD event is watching for findap# socket events, the socket needs to be closed one of these ways:

/sockclose findap*
or
/sockclose findap $+ %page

Try that change and see if it helps.

-genius_at_work
Posted By: RusselB

Re: Socket problems - 11/12/05 07:09 PM

No difference. As to my dialog, here's the relevant portions
Code:
 alias obs { 
  dialog -md Observer Observer 
} 
dialog Observer {
  title "Observer"
  size -1 -1 340 175
  option dbu
  button "Cancel", 1, 10 40 20 10, cancel
  button "OK", 2, 40 40 20 10, ok
  button Store New Info, 3, 10 60 50 10, default
  text "Rte", 4, 10 10 20 10
  combo 5, 30 10 30 10, edit drop
  text "Account", 6, 10 20 20 10
  combo 7, 30 20 60 10, edit drop
  text "Name", 8, 100 12 25 8
  edit "", 9, 130 10 95 10, autohs
  text "Address", 10, 100 22 25 8
  edit "", 11, 130 20 50 10, autohs
  text "Apt.", 12, 185 22 15 8
  edit "", 13, 205 20 20 10, autohs
  text "Phone", 14, 100 32 25 8
  edit "", 15, 130 30 50 10, autohs
  combo 16, 130 40 50 10, edit drop
  check "Active", 17, 185 32 25 8, left
  text "Billing", 19, 115 105 50 10, center
  text "Delivery", 20, 100 42 25 8
  text "Expires", 21, 185 40 50 8, center
  edit "", 22, 185 50 50 10, autohs right
  text "Start", 23, 5 105 50 10, center
  text "Stop", 24, 60 105 50 10, center
  edit "", 34, 130 60 50 10, right
  text "Balance", 28, 100 62 25 8
  text "Pay Date", 29, 225 105 50 10, center
  text "Billing", 30, 100 52 25 8
  list 18, 5 115 50 50, size extsel
  text "Delivery", 36, 170 105 50 10, center
  list 37, 60 115 50 50, size extsel
  list 38, 115 115 50 50, size extsel
  list 39, 225 115 50 50, size extsel
  text "Payment", 41, 100 72 25 8
  edit "", 42, 130 70 50 10, autohs right
  list 43, 170 115 50 50, size extsel
  text "Pay Amount", 44, 280 105 50 10, center
  combo 27, 130 50 50 10, edit drop
  list 45, 280 115 50 50, size extsel
  edit "", 25, 5 90 50 10
  edit "", 26, 60 90 50 10
  edit "", 31, 115 90 50 10
  edit "", 32, 170 90 50 10
  edit "", 33, 225 90 50 10
  edit "", 35, 280 90 50 10
}
on *:DIALOG:Observer:INIT:*: { 
  if !$hget($dname) { .hmake $dname 100 } 
  if $exists($+($dname,.hsh)) { .hload $dname $+($dname,.hsh) } 
  if !$hget($dname,Rtes) {        echo -a 8,1ALERT!!! No routes established    } 
  else {     didtok $dname 5 44 $hget($dname,Rtes)  }  
  ;  
  didtok $dname 16 44 Daily,Sat. Only,MP/LL 
  ;  
  didtok $dname 27 44 Office,Monthly,Bi-wkly 
  ;  
} 

on *:DIALOG:Observer:sclick:5: {  
  set %rte $iif($did($dname,$did).seltext,$v1,$did($dname,$did)) 
  set %accts $hfind($dname,$+(%rte,.,*,.Name),0,w) 
  while %accts { 
    did -a $+ $iif(%accts = 1,r) $dname 7 $gettok($hfind($dname,$+(%rte,.,*,.Name),%accts,w),2,46) 
    dec %accts 
  } 
} 
on *:DIALOG:Observer:sclick:7: {  
  set %acct $iif($did($dname,$did).seltext,$v1,$did($dname,$did)) 
  did -ra $dname 9 $hget($dname,$+(%rte,.,%acct,.Name))  
  did -ra $dname 11 $hget($dname,$+(%rte,.,%acct,.Address))  
  did -ra $dname 13 $hget($dname,$+(%rte,.,%acct,.Apt)) 
  did -ra $dname 15 $hget($dname,$+(%rte,.,%acct,.Phone)) 
  if !$did($dname,15) {
    find.a.person $gettok($did($dname,9),1,44) $iif($gettok($did($dname,9),2-,44),$v1)
  }
  did $iif($hget($dname,$+(%rte,.,%acct,.Active)),-c,-u) $dname 17 
  did -ra $dname 22 $asctime($hget($dname,$+(%rte,.,%acct,.Expiry)),mmm dd yyyy) 
  var %x = 1 
  while (%x &lt;= $hfind($dname,$+(%rte,.,%acct,.data.*),0,w)) { 
    var %data = $hget($dname,$+(%rte,.,%acct,.data.,%x)) 
    did -a $dname 18 $iif($gettok(%data,1,44) != -, $asctime($v1,mmm dd yyyy),-) 
    did -a $dname 37 $iif($gettok(%data,2,44) != -, $asctime($v1,mmm dd yyyy),-) 
    did -a $dname 38 $gettok(%data,3,44) 
    did -a $dname 43 $gettok(%data,4,44) 
    did -a $dname 39 $iif($gettok(%data,5,44) != -, $asctime($v1,mmm dd yyyy),-) 
    did -a $dname 45 $gettok(%data,6,44) 
    inc %x 
  } 
  did -ca $dname 16 $hget($dname,$+(%rte,.,%acct,.Delivery)) 
  did -ca $dname 27 $hget($dname,$+(%rte,.,%acct,.Billing)) 
  var %balance = $calc(($ctime($did($dname,22)) - $ctime) / 1209600) 
  if ($left($did($dname,16),1) == D) || ($did($dname,16).sel == 1) { 
    did -ra $dname 34 $+($,$round($calc(%balance * 6.8),2)) 
  } 
  elseif ($left($did($dname,16),1) == S) || ($did($dname,16).sel == 2) { 
    did -ra $dname 34 $+($,$round($calc(%balance * 3),2)) 
  } 
} 
 


And here's what I currently have for the sockets section
Code:
 alias find.a.person { 
  if (!$isid) { 
    set %lastname $1 
    set %firstname $2 
    set %street.num $gettok($did(Observer,11),1,32)
    set %street.name $gettok($did(Observer,11),2-,32)
    set %apt $did(Observer,13)
    sockopen findap findaperson.canada411.ca 80 
  } 
} 
on *:sockopen:findap:{ 
  unset %match
  unset %page*
  sockwrite -n $sockname GET $+(/10649/search/Find_Person?firstname_begins_with=1&amp;firstname=,%firstname,&amp;name_begins_with=1&amp;name=,%lastname,&amp;city_zip=Sarnia&amp;state_id=ON) HTTP/1.1 
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
} 
on *:sockread:findap:{ 
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      echo -s %nc
      did -ra Observer 15 %nc
      set %match found
    }
    elseif (*search_id* iswm %nc) &amp;&amp; !%search_id {
      set %search_id $gettok(%nc,2,34)
      set %search_id $gettok(%search_id,-1,61)
    }
    elseif ($+(*,%lastname,*,%firstname,*,Total*) iswm %nc) {
      var %nc = $gettok(%nc,-2,32)
      var %nc = $gettok(%nc,-1,62)
      set %pages $calc($int($calc(%nc / 10)) + 1)
      set %pages $calc(%pages - 1)
    }
    elseif ((&lt;/html* iswm %nc) || (%match)) {
      sockclose findap
      if ((!%match) &amp;&amp; (%pages)) {
        inc %page
        sockopen findap $+ %page findaperson.canada411.ca 80
      }
    }
  } 
}

on *:sockopen:$(findap $+ %page):{
  set %limit $calc(%page * 10 + 1)
  sockwrite -n $sockname GET $+(/10649/search/Alpha_Limit?search_id=,%search_id,&amp;old_search_type=Find_Person&amp;sort=&amp;alpha_limit=&amp;lower=,%limit,) HTTP/1.1
  sockwrite -n $sockname Host: findaperson.canada411.ca $+ $crlf $+ $crlf 
}

on *:sockread:$(findap $+ %page):{
  if ($sockerr) { 
    echo -a some error occurred oh no. 
    return 
  } 
  else { 
    var %nc 
    sockread %nc
    if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
      var %nc = $right(%nc,20)
      var %nc = $left(%nc,14)
      echo -s %nc
      did -ra Observer 15 %nc
      set %match found
      sockclose findap
    }
  }
  elseif ((&lt;/html* iswm %nc) || (%match)) {
    sockclose $(findap $+ %page)
    if ((!%match) &amp;&amp; (%pages)) {
      inc %page
      sockopen findap $+ %page findaperson.canada411.ca 80
    }
  }
} 
 
Posted By: genius_at_work

Re: Socket problems - 11/12/05 07:32 PM

Is it possible that some other portion of the code is producing the extra echo line? Using the exact code as above, I'm not seeing the extra echo. Maybe unload the main script, and make a new one only using the above code.

Also, there is another instance of the /sockclose findap that you didn't fix:

Code:
if ($+(*,%street.num,*,%apt,*,%street.name,*) iswm %nc) {
  var %nc = $right(%nc,20)
  var %nc = $left(%nc,14)
  echo -s %nc
  did -ra Observer 15 %nc
  set %match found
  [color:red]sockclose findap[/color]
}


-genius_at_work
Posted By: RusselB

Re: Socket problems - 11/12/05 07:40 PM

That was the problem...missed fixing that one close, so the socket wasn't closing properly
Posted By: genius_at_work

Re: Socket problems - 11/12/05 07:55 PM

Great! laugh

-genius_at_work
© 2019 mIRC Discussion Forums