mIRC Home    About    Download    Register    News    Help

Print Thread
Guide to Sockets #245084 10/04/14 06:27 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Hey, I'm just starting out on learning Sockets.

I was following this guide and realized that it was heavily outdated when I tried viewing the source and it differed greatly from what was described in the tutorial (2006).

So, could anyone provide a fairly recent tutorial to sockets with examples that are up-to-date please?
Would help out a ton!


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245086 10/04/14 06:59 PM
Joined: Jul 2006
Posts: 3,740
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,740
http://en.wikichip.org/wiki/MIRC, best tutorials and up to date you can find, including sockets


Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Guide to Sockets [Re: Wims] #245092 10/04/14 09:46 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
So, I followed the examples, and when sockreading the YouTube part, I could not succeed in finding
Code:
if (*Uploaded by* iswm %x)
- So I opened the page source and found it myself, instead of using Uploaded by, I searched for SHARE_CAPTION which was in
Quote:
'SHARE_CAPTION': "Uploaded by National Geographic",


Now it worked. However! It echoed in Swedish, my native language.
Quote:
'SHARE_CAPTION': "Laddades upp av National Geographic",
and changing the code to
Code:
if (*Laddades upp av* iswm %x)
would echo the same thing.

Is there a certain reason for this?

I also discovered an error in the first example with Zigwap. - The code presented in the wiki says to parse ($6) when the actual source displays ($5). The reason for this is because the older version displayed in the wiki has
Quote:
<p align="center">Your random color is: Pink</p>
Whereas the newest page no longer has the align.


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245096 10/04/14 11:03 PM
Joined: Jan 2004
Posts: 1,314
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,314
Youtube is probably guessing which language to use based on your IP address, you can request a preferred language by using the accept-language header:

accept-language:en-US,en;q=0.8

As for parsing, you may find it easier to save the entire response to file before working with it. This will allow you to use (pretty much) the same socket events for all pages instead of creating duplicate events for every page you want.

Re: Guide to Sockets [Re: Loki12583] #245097 10/04/14 11:26 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Thanks, that header did the trick. smile

Yeah, I figured out the last part just now as well and I saved it in my mircdir, really eases the whole experience. - I'll feel more familiar working this way.

Now, question about that. What I did was to write textfile.txt %x after sockreading %x, this would print everything, sort of... My mIRC application suddenly took a break for 10 seconds and stop responding, and printing this error:
Quote:
* /write: line too long (line 44, remote.ini)
a couple of times.

I have a feeling that there's a better way to do this, care to share? smile


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245098 10/04/14 11:39 PM
Joined: Jul 2006
Posts: 3,740
W
Wims Offline
Hoopy frood
Offline
Hoopy frood
W
Joined: Jul 2006
Posts: 3,740
Writting to a file and then processing is cleaner and allow for generic code but it's also much much slower, that's also why people parse what they need line by line.
There is a line length limit in mIRC, you can't have an expression/statement/parameter that is more than 4150, which is why you get an error, you must read into a binary variable with /sockread to avoid that, and use /bwrite to write to a file.


Last edited by Wims; 10/04/14 11:39 PM.

Looking for a good help channel about mIRC? Check #mircscripting @ irc.swiftirc.net
Re: Guide to Sockets [Re: Wims] #245099 10/04/14 11:59 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Is this what I'm supposed to do?
Code:
  var %x 
  bset -t &bvar %x
  bwrite youtubesock.ext &bvar
  sockread %x


This gave me this:
Quote:
* /bset: line too long (line 43, remote.ini)
-
* [10053] Software caused connection abort


Got the part of bwrite down thanks to Wims on the #mircscripting channel.
This was what I needed
Code:
  /sockread -fn &x 
  sockread $sock($sockname).rq &x
  /bwrite youtubesock -1 -1 &x



Last edited by Nillen; 11/04/14 12:46 AM.

Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Wims] #245120 11/04/14 12:16 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
The website I'm looking to pull info from uses the POST data method instead of GET, which isn't mentioned much in the wiki how to deal with.

At least, I think it's POST, since I couldn't get my sockread to work and looking at the page source it tells me this
Quote:
data-method="post"
And use <form></form>
Source: view-source:http://dotabuff.com/players/35434359
Anything special I need to do about this?


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245121 11/04/14 12:20 PM
Joined: Jan 2004
Posts: 1,314
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,314
http://en.wikipedia.org/wiki/POST_(HTTP)

If you're using Chrome you can press ctrl+shift+j and go to the Network tab, this will allow you to look at all the HTTP headers you'll need to mirror in your mIRC sockets. If you're using Firefox you'll need to install the FireBug extension.

Re: Guide to Sockets [Re: Loki12583] #245123 11/04/14 12:32 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
Thanks, I found out that it was GET that I was supposed to use -

Which actually only makes me more confused.

I made a textbook example code to try and make it work, but unsuccessfully and I have no idea what's causing the issue.

Code:
alias DotaBuff {  
  echo -ag DotaBuff Alias
  if ($sockerr) echo -ag $sockerr alias
  sockclose DotaBuff
  sockopen DotaBuff www.dotabuff.com 80
}

on *:sockopen:DotaBuff: {
  echo -ag DotaBuff Sockopen
  if ($sockerr) echo -ag $sockerr open 
  sockwrite -n DotaBuff GET /players/35434359 HTTP/1.1
  sockwrite -n DotaBuff Host: www.dotabuff.com
  sockwrite -n DotaBuff
}

on *:sockread:DotaBuff: {
  var %r
  sockread %r
  if ($sockerr) echo -ag $sockerr read
  echo -ag DotaBuff Sockread
  if (*Win Rate* iswm %r) { 
    echo -ag found it!
    tokenize 32 %r
    echo -ag Win Rate: $noHTML($1-)
  }
}

on *:sockclose:DotaBuff: { 
  if ($sockerr) echo -ag $sockerr close
  echo -ag DotaBuff Sockclose
}


Which is just this code re-made to my interest.
Code:
alias Zigwap {
  echo -ag Zigwap Alias
  if ($sockerr) echo -ag $sockerr alias
  sockclose Zigwap
  sockopen Zigwap www.zigwap.com 80
}

on *:sockopen:Zigwap: {
  echo -ag Zigwap Open
  if ($sockerr) echo -ag $sockerr Open
  sockwrite -n Zigwap GET /mirc/sockets_demo HTTP/1.0
  sockwrite -n Zigwap Host: www.zigwap.com
  sockwrite -n Zigwap
}

on *:sockread:Zigwap: {
  echo -ag Zigwap Read
  if ($sockerr) echo -ag $sockerr Read
  var %read
  sockread %read
  if (*Your random color is: * iswm %read) {
    tokenize 32 %read
    echo $color(info) -a Random Color: $noHTML($5)
    sockclose $sockname
  }
}

on *:sockclose:Zigwap: {
  echo -ag Zigwap Close
  if ($sockerr) echo -ag $sockerr Close
}


These are the echoes I'm receiving.
Quote:
DotaBuff Alias
DotaBuff Sockopen
DotaBuff Sockclose


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245125 11/04/14 12:58 PM
Joined: Jan 2004
Posts: 1,314
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,314
Are you sure there isn't a line too long error? The line with that text is over 22,000 characters.

Dota 2 appears to have an API, which you can use instead of downloading and parsing web pages: http://dev.dota2.com/showthread.php?t=47115

Re: Guide to Sockets [Re: Loki12583] #245128 11/04/14 01:54 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
I most likely would get that error if I could read the socket, I tried adding another example presented.
Code:
on *:sockread:DotaBuff: {
  window -deC @ $+ $sockname -1 -1 700 700
  var %read
  sockread -f %read
  aline -p @ $+ $sockname : $+ %read
  if ($sockerr) echo -ag $sockerr read
  echo -ag DotaBuff Sockread
  if (*Win Rate* iswm %read) { 
    echo -ag found it!
    tokenize 32 %read
    echo -ag Win Rate: $noHTML($1-)
  }
}

Which opened a window with a Bad request
Quote:

:HTTP/1.1 400 Bad Request
DotaBuff Sockread
:Server: nginx/1.4.7
DotaBuff Sockread
:Date: Fri, 11 Apr 2014 13:52:10 GMT
DotaBuff Sockread
:Content-Type: text/html
DotaBuff Sockread
:Content-Length: 172
DotaBuff Sockread
:Connection: close
DotaBuff Sockread
:
DotaBuff Sockread
:<html>
DotaBuff Sockread
:<head><title>400 Bad Request</title></head>
DotaBuff Sockread
:<body bgcolor="white">
DotaBuff Sockread
:<center><h1>400 Bad Request</h1></center>
DotaBuff Sockread
:<hr><center>nginx/1.4.7</center>
DotaBuff Sockread
:</body>
DotaBuff Sockread
:</html>
DotaBuff Sockread
DotaBuff Sockclose
Apologies for the Sockreads everywhere, it's just to make me understand the process a bit better.

I knew about the api from before, which is how DotaBuff pulls their info. I just thought it'd be simpler for me to read it from the DotaBuff website since they've compressed all the useful info I desire already. I might be mistaken however.


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245129 11/04/14 02:20 PM
Joined: Jan 2004
Posts: 1,314
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,314
They require a User-Agent header:

Code:
on *:sockopen:DotaBuff: {
  echo -ag DotaBuff Sockopen
  if ($sockerr) echo -ag $sockerr open 
  sockwrite -n $sockname GET /players/35434359 HTTP/1.0
  sockwrite -n $sockname Host: dotabuff.com
  sockwrite -n $sockname User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.5 Safari/537.36
  sockwrite -n $sockname Connection: close
  sockwrite -n $sockname
}

Re: Guide to Sockets [Re: Loki12583] #245130 11/04/14 02:34 PM
Joined: Dec 2013
Posts: 779
N
Nillen Offline OP
Hoopy frood
OP Offline
Hoopy frood
N
Joined: Dec 2013
Posts: 779
How did you learn this? Was there something in the page source I was supposed to pay attention to?

Also, should I assume that HTTP/1.0 should always be used? I tried it first with 1.1 unsuccessfully.


Nillens @ irc.twitch.tv
Nillen @ irc.rizon.net
Re: Guide to Sockets [Re: Nillen] #245131 11/04/14 02:46 PM
Joined: Jan 2004
Posts: 1,314
L
Loki12583 Offline
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,314
There's no information given by the server that User-Agent is required, it's just something servers will do to prevent bots from connecting. You should prefer HTTP/1.0 over 1.1 unless you're going to implement support for chunked transfer encoding, as there's no request header to prevent the server from using it.

http://en.wikipedia.org/wiki/Chunked_transfer_encoding