mIRC Homepage
Posted By: Thrull A little socket help - 07/11/11 12:51 PM
I'm trying to pull information off of http://174.142.79.66:8278/index.html

However, whenever I try and access the site via sockets, I get a bunch of gobbly-gook streaming in, I assume this is the music being played. All I want is the text that I see in my browser.

Here's the code I'm using the currently produces the gobbly-gook.

Code:
Alias Radio {
  sockopen Radio40 174.142.79.66 8278
  ;the below line is there to ensure that Mirc doesn't freeze up when it starts being hit by all the gobbly-gook
  timer 1 2 sockclose Radio40
}

on 1:sockopen:radio40: {
  window -e @radio
  sockwrite -n radio40 GET / HTTP/1.1
  ;sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10
  sockwrite -n $sockname Host: 174.142.79.66 $+ $crlf $+ $crlf
  sockwrite -n radio40 Accept-Charset: text/html; charset=windows-1252
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.2; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
  sockwrite -n $sockname Connection: Close
  sockwrite -n $sockname $crlf
}

on *:sockread:radio40: {
  sockread %radio40.temp
  echo @radio %radio40.temp
}


Thanks
Posted By: jaytea Re: A little socket help - 07/11/11 12:56 PM
Quote:

Code:
  sockwrite -n $sockname Host: 174.142.79.66 $+ $crlf $+ $crlf



get rid of those CRLFs. an empty line signals the end of your HTTP request.
Posted By: Thrull Re: A little socket help - 07/11/11 01:08 PM
Thanks, that got rid of the spam, but now it just returns

HTTP/1.0 200 OK
content-type:text/html
Posted By: Tomao Re: A little socket help - 07/11/11 06:16 PM
Code:
alias radio {
  if ($window(@radio)) clear @radio
  sockopen Radio40 174.142.79.66 8278
}
on *:sockclose:radio40:{
  echo @radio Server Status: Server is currently down.
}
alias -l getinfo {
  return $regsubex($bvar(&radio.temp,1-).text,/<[^>]*>|&nbsp;|<.*/g,$chr(32))
}
on *:sockopen:radio40:{
  window -e @radio
  sockwrite -n $sockname GET / HTTP/1.1
  sockwrite -n $sockname Host: 174.142.79.66
  sockwrite -n $sockname Accept-Charset: text/html; charset=windows-1252
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; $&
    Windows NT 5.2; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
  sockwrite -n $sockname Connection: Close
  sockwrite -n $sockname $crlf
}
on *:sockread:radio40:{
  sockread &radio.temp
  if ($regex($getinfo,/(server status.*) stream/i)) echo @radio $regml(1)
  if ($regex($getinfo,/d>(.*\s\050.*\051)/i)) echo @radio Stream Status: $regml(1)
  if ($regex($getinfo,/(listener peak: \d+)/i)) echo @radio $regml(1)
  if ($regex($getinfo,/(average listen time:.*)/i)) echo @radio $gettok($regml(1),1-5,32)
  if ($regex($getinfo,/(stream title:.*)content/i)) echo @radio $regml(1)
  if ($regex($getinfo,/(content type:.*)/i)) {
    tokenize 32 $regml(1) 
    echo @radio $1-3
    echo @radio $4-6
    echo @radio $7-9
    echo @radio $11-12
    echo @radio $13-15
    echo @radio $16-18
    sockclose $sockname
  }
}


Posted By: Riamus2 Re: A little socket help - 07/11/11 08:34 PM
I can't test right now, so I don't know exactly what "gobbly-gook" you are seeing, but HTTP/1.1 often sends data in chunks. This can cause some confusion when trying to work with it. You might try using HTTP/1.0 and see if that makes it easier for what you're doing.
Posted By: Tomao Re: A little socket help - 07/11/11 09:51 PM
HTTP/1.1 works just as dandy.
Posted By: Wims Re: A little socket help - 07/11/11 09:54 PM
His code is actually trying to listen to the radio, after the header he will constantly receive data, which does freeze mIRC if you have an echo on each data received.
Also, "1.1" doesn't necessarily send data as chunk
Posted By: Tomao Re: A little socket help *DELETED* - 07/11/11 10:06 PM
Post deleted by Tomao
Posted By: Wims Re: A little socket help - 07/11/11 10:35 PM
Use sockread -n on the binvar
Posted By: Riamus2 Re: A little socket help - 07/11/11 10:40 PM
Yeah, I should have said "usually" or "often" there. I fixed it. smile
Posted By: Tomao Re: A little socket help *DELETED* - 08/11/11 03:58 AM
Wims, that didn't work with the -n switch. Anyway, I've worked it out and here is the finished product:
Code:
alias radio {
  if ($window(@radio)) clear @radio
  sockopen Radio40 174.142.79.66 8278
}
on *:sockopen:radio40:{
  if (!$sockerr) {
    window -e @radio
    sockwrite -n $sockname GET / HTTP/1.1
    sockwrite -n $sockname Host: 174.142.79.66
    sockwrite -n $sockname Accept-Charset: text/html; charset=windows-1252
    sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; $&
      Windows NT 5.2; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
    sockwrite -n $sockname Connection: Close | sockwrite -n $sockname $crlf
  }
}
alias -l getinfo {
  return $regsubex($bvar(&radio.temp,1-).text,/<[^>]*>|&nbsp;|<.*|\t/g,$chr(32))
}
on *:sockread:radio40:{
  if (!$sockerr) {
    sockread &radio.temp
    if (Server is currently down isin $getinfo) echo @radio Server Status: $v1 $+ .
    else {
      if ($regex($getinfo,/(current song:.*)written/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(server status.*) stream/i)) echo @radio $regml(1)
      if ($regex($getinfo,/d>(.*\s\050.*\051)/i)) echo @radio Stream Status: $regml(1)
      if ($regex($getinfo,/(listener peak: \d+)/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(average listen time:.*)/i)) echo @radio $gettok($regml(1),1-5,32)
      if ($regex($getinfo,/(stream title:.*)content/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(content type:.*)/i)) {
        tokenize 32 $regml(1) | echo @radio $1-3 | echo @radio $4-6 | echo @radio $7-9 
        echo @radio $11-12 | echo @radio $13-15 | echo @radio $16-18
      }
    }
  }
}
I've added the output for Stream URL, ICQ, AIM and IRC because sometimes they do show their info, sometimes they're not applicable.
Posted By: Thrull Re: A little socket help - 08/11/11 07:52 AM
Ok, thank you kindly guys. I really just needed to know how to get the info, I'd have parsed it myself. But thanks for the extra work. Makes my life just a bit easier.

Haven't tested it while the stream is running, but I shall soon.

Thanks again!
Posted By: Tomao Re: A little socket help *DELETED* - 08/11/11 06:05 PM
Well, using tokenize may not be ideal because some of the token lengths are variable, so I think it'd be best to do:
Code:
alias radio {
  if ($window(@radio)) clear @radio
  sockopen Radio40 174.142.79.66 8278
}
on *:sockopen:radio40:{
  if (!$sockerr) {
    window -e @radio
    sockwrite -n $sockname GET / HTTP/1.1
    sockwrite -n $sockname Host: 174.142.79.66
    sockwrite -n $sockname Accept-Charset: text/html; charset=windows-1252
    sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows; U; $&
      Windows NT 5.2; rv:1.7.3) Gecko/20040913 Firefox/0.10.1
    sockwrite -n $sockname Connection: Close | sockwrite -n $sockname $crlf
  }
}
alias -l getinfo {
  return $regsubex($bvar(&radio.temp,1-).text,/<[^>]*>|&nbsp;|<.*|\t/g,$chr(32))
}
on *:sockread:radio40:{
  if (!$sockerr) {
    sockread &radio.temp
    if (Server is currently down isin $getinfo) echo @radio Server Status: $v1 $+ .
    else {
      if ($regex($getinfo,/(server status.*)stream/i)) echo @radio $regml(1)
      if ($regex($getinfo,/d>(.*)listener/i)) echo @radio Stream Status: $regml(1)
      if ($regex($getinfo,/(listener peak.*)average/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(average listen time:.*)/i)) echo @radio $gettok($regml(1),1-5,32)
      if ($regex($getinfo,/(content type:.*)stream/i)) echo @radio $gettok($regml(1),1-3,32)
      if ($regex($getinfo,/(stream genre:.*)stream url/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(stream url:.*)stream (icq|aim)/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(stream icq:.*)stream (icq|aim)/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(stream aim:.*)stream irc/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(stream irc:.*)current/i)) echo @radio $regml(1)
      if ($regex($getinfo,/(current song:.*)/i)) echo @radio $regml(1)
    }
  }
}
© mIRC Discussion Forums