mIRC Homepage

Cant get socket to work

Posted By: truguce

Cant get socket to work - 30/04/06 05:37 PM

Not sure if I am doing this right, this is my first try at using sockets. All im trying to do is get it to echo back to me the billboard new releases. I am not sure if it is my script that has the problem or if it is the site that i am trying to access. This is what I have so far
Code:
alias sockettest {
  sockopen toplist www.billboard.com 80
}
on *:SOCKOPEN:toplist: {
  sockwrite -nt $sockname post www.billboard.com/bbcom/releases/releases_display.jsp?rollDate=0
  sockwrite -nt $sockname HOST: billboard.com $+ $crlf $+ $crlf 
  sockwrite -nt $sockname Accept-Language: en-us
  sockwrite -nt $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)
}
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    echo -a %temptext
  }
}

and this is what i get echoed back to me
Quote:
HTTP/1.0 400 Bad request
Content-Type: text/html
* /echo: insufficient parameters (line 18, sockettest.mrc)
<h2>Bad request</h2>

This webpage i am trying to echo is www.billboard.com/bbcom/releases/releases_display.jsp?rollDate=0
As I said not sure if it is me or the site. Thanks
Posted By: Danthemandoo

Re: Cant get socket to work - 30/04/06 05:49 PM

Code:
alias sockettest {
  sockopen toplist www.billboard.com 80
}
on *:SOCKOPEN:toplist: {
  sockwrite -nt $sockname [color:red]GET /bbcom/releases/releases_display.jsp?rollDate=0[/color]
  sockwrite -nt $sockname HOST: billboard.com 
  sockwrite -nt $sockname Accept-Language: en-us
  sockwrite -nt $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)
}
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    echo -a %temptext
  }
}


Notes: Once you "sockopen" to a site, you no longer need the "www" bit in the GET. Its also a REALLY big site, so it'll take about 10 seconds to echo.
Posted By: truguce

Re: Cant get socket to work - 30/04/06 05:59 PM

I tried that and got this
Quote:
HTTP/0.9 400 Bad Request
* /echo: insufficient parameters (line 18, sockettest.mrc)

I also tried it on yahoo sports with no such luck. I think i am using the wrong post/get method this is what i have for yahoo
Code:
alias sockettest {
  sockopen toplist www.yahoo.com 80
}
on *:SOCKOPEN:toplist: {
  sockwrite -nt $sockname GET sports.yahoo.com/mlb/scoreboard
  sockwrite -nt $sockname HOST: yahoo.com 
  sockwrite -nt $sockname Accept-Language: en-us
  sockwrite -nt $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)
}
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    echo -a %temptext
  }
}

Quote:
<h1>Sorry, Bad Request.</h1>
Your browser sent a request that this server could not understand.<P>
Invalid URI in request GET sports.yahoo.com/mlb/scoreboard<P>
<p>Please check the URL for proper spelling and capitalization. If
you're having trouble locating a destination on Yahoo!, try visiting the
<strong><a

i always get a bad request error. frown
Posted By: Danthemandoo

Re: Cant get socket to work - 30/04/06 06:02 PM

Code:
alias sockettest {
  sockopen toplist sports.yahoo.com 80
}
on *:SOCKOPEN:toplist: {
  sockwrite -nt $sockname GET /mlb/scoreboard
  sockwrite -nt $sockname HOST: yahoo.com 
  sockwrite -nt $sockname Accept-Language: en-us
  sockwrite -nt $sockname User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)
}
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    echo -a %temptext
  }
}


This one and the one above both work perfectly well for me.
Posted By: truguce

Re: Cant get socket to work - 30/04/06 06:39 PM

Weird i put the yahoo one just as you have and it works, but the billlboard i still get
Quote:
HTTP/0.9 400 Bad Request
* /echo: insufficient parameters (line 18, sockettest.mrc)
Not sure what the problem is on my end
Posted By: genius_at_work

Re: Cant get socket to work - 30/04/06 08:34 PM

This line:

sockwrite -nt $sockname GET /mlb/scoreboard


should be like this:

sockwrite -nt $sockname GET /mlb/scoreboard HTTP/1.0

-genius_at_work
Posted By: truguce

Re: Cant get socket to work - 30/04/06 11:24 PM

thats it!!!! now it will list the billboard site. Thanks much!!!!
Posted By: truguce

Re: Cant get socket to work - 01/05/06 12:34 AM

Now that I can get the script to work I got a few problems. I found some code by Riamus in his weather bot to get $htmlfree info.
Code:
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    if (*&lt;div class="searchResultAlb_Col_txt_1_1_1"&gt;* iswm %temptext) || (*&lt;div class="searchResultAlb_Col_txt_1_2"&gt;* iswm %temptext) {
      write socktemp.txt $htmlfree(%temptext)
    }
  }
}
alias -l htmlfree {
  var %x, %i = $regsub($1-,/(^[^&lt;]*&gt;|&lt;[^&gt;]*&gt;|&lt;[^&gt;]*$)/g,$null,%x), %x = $remove(%x,&amp;nbsp;)
  return %x
}

It writes the album name and the artist on two seprate lines and has 4 tab spaces in front of it.
Quote:
10 Bare Essentials
Artist: Bobby Bare

Thats how it writes to the txt file. What I cant figure out is how to get rid of the 4 tab spaces and put the album and artist on the same line. I tried a few different things but all with no success. The *<div class="searchResultAlb_Col_txt_1_1_1">* is the album line and *<div class="searchResultAlb_Col_txt_1_2">* is the artist line. Thanks
Posted By: genius_at_work

Re: Cant get socket to work - 01/05/06 12:59 AM

The artist and song data are probably on different lines in the html code. To get both items on one line, you need to store the first item in a variable when it arrives, and then write both items at the same time when the second arrives. Here is a pseudocode example:

if (song isin thisdata) {
set %song $htmlfree($1-)
}
elseif (artist isin thisdata) {
write file.txt %song $htmlfree($1-)
}

-genius_at_work
Posted By: truguce

Re: Cant get socket to work - 01/05/06 02:40 AM

hmm i thought i tried that but quess not because it worked perfect. The only thing is that there is 4 tab spaces in between each item. In the front on each line there is 4 tab spaces and the after the album name before the artist there is 4 more. I am sure it is in the $htmlfree but not to sure how that code works.
This is what I have now
Code:
on *:SOCKREAD:toplist: {
  if ($sockerr) {
    echo -s SockError $sockname $sockerr
    return
  }
  else {
    var %temptext
    sockread %temptext
    if (*&lt;div class="searchResultAlb_Col_txt_1_1_1"&gt;* iswm %temptext) {
      set %song $htmlfree(%temptext)
    }
    elseif (*&lt;div class="searchResultAlb_Col_txt_1_2"&gt;* iswm %temptext) {
      write sockettemp.txt %song $htmlfree(%temptext)
    }
  }
}
alias -l htmlfree {
  var %x, %i = $regsub($1-,/(^[^&lt;]*&gt;|&lt;[^&gt;]*&gt;|&lt;[^&gt;]*$)/g,$null,%x), %x = $remove(%x,&amp;nbsp;)
  return %x
}
Posted By: genius_at_work

Re: Cant get socket to work - 01/05/06 04:13 AM

Just change this line:

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

To this line:

var %x, %i = $regsub($1-,/(^[^<]*>|<[^>]*>|<[^>]*$)/g,$null,%x), %x = $remove(%x,&nbsp;,$chr(9))

-genius_at_work
Posted By: truguce

Re: Cant get socket to work - 01/05/06 04:38 AM

Im amazed at how much you know. As always it works perfect. The album name and artist is right next to each other. Thank you for all your help!
© 2021 mIRC Discussion Forums