mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
Hello, I have been trying to create a script to find out if someone is following someone on twitch and have found out that by reading the following page will leave a bunch of info:
https://api.twitch.tv/kraken/users/jester87d3/follows/channels/k3mistvideo

where as if the person is not following it will return a 404 ex:
https://api.twitch.tv/kraken/users/notfollowingname/follows/channels/k3mistvideo

I have tried using this code:

Alias Demo {
sockClose demo
sockOpen -e demo api.twitch.tv 443
%nickfollowing = /kraken/users/ $+ $$1 $+ /follows/channels/k3mistvideo
}

ON *:TEXT:!test:#:{
DEMO $nick
}
on *:SockOpen:demo: {
sockwrite -nt demo GET %nickfollowing HTTP/1.0
sockwrite -nt demo Host: api.twitch.tv
sockwrite -nt demo $crlf
}
on *:SockRead:demo: {
var %read
sockRead %read
/msg #k3mistvideo %read
if (!*"status":404*) {
/msg #k3mistvideo Following
}
Else {
/msg #k3mistvideo Not Following
}
}


But the above script will not get past:
sockOpen -e demo api.twitch.tv 443
There is no error or anything what do you guys think?
NOTE: you need to be using Google Chrome to see the text. Otherwise it will just try to download a JSON file.

Last edited by RuLerZ; 11/05/13 02:49 AM.
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
How do you know it was getting up to that line but not past it?

I rewrote a bit:

Code:
alias demo {
  var %user = $$1, %chan = $2, %sockname = demo. $+ $ticks
  hfree -w %sockname | hmake %sockname
  hadd %sockname user %user
  hadd %sockname chan %chan
  hadd %sockname host api.twitch.tv
  hadd %sockname port 443
  hadd %sockname request /kraken/users/ $+ %user $+ /follows/channels/k3mistvideo
  sockopen -e %sockname $hget(%sockname,host) $hget(%sockname,port)
}

on *:text:!test:#:{
  demo $nick #
}

on *:sockopen:demo.*: {
  var %<< = sockwrite -nt $sockname
  %<< GET $hget($sockname,request) HTTP/1.0
  %<< Host: $sock($sockname).addr
  %<< $crlf
}

on *:sockread:demo.*: {
  var %header, %content
  if (!$hget($sockname,header.complete)) {
    sockread %header
    while (%header != $null) {
      if (HTTP/* 2?? * iswm %header) { hadd $sockname following $true }
      elseif (HTTP/* 4?? * iswm %header) { hadd $sockname following $false }
      ;echo -ag %header
      sockread %header
    }
    if ($sockbr) hadd $sockname header.complete $true
  }

  if ($hget($sockname,header.complete)) {
    while ($sockbr) {
      sockread -f %content
      ;if (%content != $null) echo -ag %content
    }
  }
}

on *:sockclose:demo.*:{
  if ($hget($sockname,following)) { var %msg = Following }
  else { var %msg = Not following }
  
  if ($hget($sockname,chan)) { msg $v1 %msg }
  else { echo -ag %msg }
  
  hfree $sockname
}

Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
I am testing it by getting the script to message the channel with test messages at different spots in the script. I have put this in the code to test it out:
I am testing this in a channel named #RuLerZ

Alias demo {
var %user = $$1, %chan = $2, %sockname = demo. $+ $ticks
hfree -w %sockname | hmake %sockname
hadd %sockname user %user
hadd %sockname chan %chan
hadd %sockname host api.twitch.tv
hadd %sockname port 443
hadd %sockname request /kraken/users/ $+ %user $+ /follows/channels/k3mistvideo
sockopen -e %sockname $hget(%sockname,host) $hget(%sockname,port)
/msg #RuLerZ test1
}

on *:text:!test:#:{
demo $nick #
}

on *:sockopen:demo.*: {
/msg #RuLerZ test2
var %<< = sockwrite -nt $sockname
%<< GET $hget($sockname,request) HTTP/1.0
%<< Host: $sock($sockname).addr
%<< $crlf
}

on *:sockread:demo.*: {
/msg #RuLerZ test3
var %header, %content
if (!$hget($sockname,header.complete)) {
sockread %header
while (%header != $null) {
if (HTTP/* 2?? * iswm %header) { hadd $sockname following $true }
elseif (HTTP/* 4?? * iswm %header) { hadd $sockname following $false }
;echo -ag %header
sockread %header
}
if ($sockbr) hadd $sockname header.complete $true
}

if ($hget($sockname,header.complete)) {
while ($sockbr) {
sockread -f %content
;if (%content != $null) echo -ag %content
}
}
}

on *:sockclose:demo.*:{
/msg #RuLerZ test4
if ($hget($sockname,following)) { var %msg = Following }
else { var %msg = Not following }

if ($hget($sockname,chan)) { msg $v1 %msg }
else { echo -ag %msg }

hfree $sockname
}

This is the response:

[02:12] <@RuLerZ1366> !test
[02:12] <@botttttttt> test1

I am not sure why but it is just not opening the socket for me. Thank you so much for helping me out and looking into this with me. I greatly appreciate all the help you can give me.

Last edited by RuLerZ; 11/05/13 06:27 AM.
Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
Also I should let you know that if I just change the script a little to cause it to not work by taking out the ssl on that page it does work but with a response below:

Alias demo {
var %user = $$1, %chan = $2, %sockname = demo. $+ $ticks
hfree -w %sockname | hmake %sockname
hadd %sockname user %user
hadd %sockname chan %chan
hadd %sockname host api.twitch.tv
hadd %sockname port 80
hadd %sockname request /kraken/users/ $+ %user $+ /follows/channels/k3mistvideo
sockopen %sockname $hget(%sockname,host) $hget(%sockname,port)
/msg #RuLerZ test1
}

Every thing else is the same in remote with the following results:

[02:18] <@RuLerZ1364> !test
[02:18] <@botttttttt> test1
[02:18] <@botttttttt> test2
[02:18] <@botttttttt> test3
[02:18] <@botttttttt> test4
[02:18] <@botttttttt> Not following

but I am following so I change the remote to this:

on *:text:!test:#:{
demo $nick #
}

on *:sockopen:demo.*: {
/msg #RuLerZ test2
var %<< = sockwrite -nt $sockname
%<< GET $hget($sockname,request) HTTP/1.0
%<< Host: $sock($sockname).addr
%<< $crlf
}

on *:sockread:demo.*: {
/msg #RuLerZ test3
var %header, %content
if (!$hget($sockname,header.complete)) {
sockread %header
/msg #RuLerZ %header
while (%header != $null) {
if (HTTP/* 2?? * iswm %header) { hadd $sockname following $true }
elseif (HTTP/* 4?? * iswm %header) { hadd $sockname following $false }
;echo -ag %header
sockread %header
}
if ($sockbr) hadd $sockname header.complete $true
}

if ($hget($sockname,header.complete)) {
while ($sockbr) {
sockread -f %content
;if (%content != $null) echo -ag %content
}
}
}

on *:sockclose:demo.*:{
/msg #RuLerZ test4
if ($hget($sockname,following)) { var %msg = Following }
else { var %msg = Not following }

if ($hget($sockname,chan)) { msg $v1 %msg }
else { echo -ag %msg }

hfree $sockname
}

And the response is this:

<@RuLerZ1364> !test
<@botttttttt> test1
<@botttttttt> test2
<@botttttttt> test3
<@botttttttt> HTTP/1.1 400 Bad Request
<@botttttttt> test4
<@botttttttt> Not following

I hope this helps. 400 due to the page being HTTPS SSL I am lost. Also no errors in the script pop up stopping the script in the server window.

Last edited by RuLerZ; 11/05/13 06:33 AM.
Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
I have a feeling that due to the page not working in IE that may have something to do with it not opening the socket so if there is any way to test whether a page wants me to download a JSON file vs The webpage cannot be found that could work just as well, but it still needs to open the socket to do that as well.

Last edited by RuLerZ; 11/05/13 06:48 AM.
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
The response from the server contains the 404 error status, but also contains the json. IE just doesn't display it for some reason.

To start, make sure that $sslready evaluates to $true. I would expect an error if you didn't have the DLLs, but check just to make sure. If it evaluates to $false, download the necessary files listed here: http://www.mirc.com/ssl.html

Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
Ok thank you so much you were right $sslready was false so I downloaded the Oped SSL and now it goes a bit further but there is still a problem, sorry.

Here is my code:

Alias demo {
/msg #RuLerZ 1
var %user = $$1, %chan = $2, %sockname = demo. $+ $ticks
hfree -w %sockname | hmake %sockname
hadd %sockname user %user
hadd %sockname chan %chan
hadd %sockname host api.twitch.tv
hadd %sockname port 443
hadd %sockname request /kraken/users/ $+ %user $+ /follows/channels/k3mistvideo
sockopen -e %sockname $hget(%sockname,host) $hget(%sockname,port)
}

on *:text:!test:#:{
demo $nick #
}

on *:sockopen:demo.*: {
var %<< = sockwrite -nt $sockname
%<< GET $hget($sockname,request) HTTP/1.0
%<< Host: $sock($sockname).addr
%<< $crlf
}

on *:sockread:demo.*: {
/msg #RuLerZ 2
var %header, %content
if (!$hget($sockname,header.complete)) {
sockread %header
/msg #rulerz %content
while (%header != $null) {
if (HTTP/* 2?? * iswm %header) { hadd $sockname following $true /msg }
elseif (HTTP/* 4?? * iswm %header) { hadd $sockname following $false }
;echo -ag %header
sockread %header
}
if ($sockbr) {
hadd $sockname header.complete $true
}
}

if ($hget($sockname,header.complete)) {
while ($sockbr) {
sockread -f %content
}
}
}

on *:sockclose:demo.*:{
/msg #RuLerZ 3
if ($hget($sockname,following)) { var %msg = Following }
else { var %msg = Not following }

if ($hget($sockname,chan)) { msg $v1 %msg }
else { echo -ag %msg }

hfree $sockname
}


Here is the results:
[19:43] <@RuLerZ> !test
[19:43] <@Bot> 1
[19:43] <@Bot> 3
[19:43] <@Bot> Not following

It appears to be completely missing the sockread It does however open the HTTPS site now though. Thank you so much for helping me out with this issue it has been a tough thing for me.

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Please wrap your code in [code][/code] tags or [spoiler][/spoiler] if it's exceptionally long. Replace your sockopen event with this to check for errors, it's a good idea to print this error information at the start and end of the other socket events as well. You may have errors on your status page if the sockwrites were not successful in the sockopen.

Also note that you're messaging %content in the wrong location, and that you would have a bunch of error messages if it got that far. After sockread %header, you should be messaging %header, not %content.

Code:
on *:sockopen:demo.*: {
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
  var %<< = sockwrite -nt $sockname
  %<< GET $hget($sockname,request) HTTP/1.0
  %<< Host: $sock($sockname).addr
  %<< $crlf
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
}

Last edited by Loki12583; 13/05/13 02:01 AM.
Joined: Oct 2012
Posts: 164
D
Vogon poet
Offline
Vogon poet
D
Joined: Oct 2012
Posts: 164
Forget all this hash table stuff, that is only unnecessarily complicating matters.

The only data your using is a nick and #. These can more easily be stored/retrieved via /sockmark.

Code:
on *:text:!test:#:{
  demo $nick #
}

alias demo {
  msg # 1
  var %sockname = demo. $+ $ticks
  sockopen -e %sockname api.twitch.tv 443
  sockmark %sockname $1-
  ; ^^ This marks the socket with the "$nick" + "#channel" data
}

on *:sockopen:demo.*:{
  tokenize 32 $sock($sockname).mark
  ; now $1 is the nick passed to the alias and $2 is the channel
  
  msg $2 sockopen (Checking $1 status)
  
  var %<< = sockwrite -nt $sockname
  %<< GET /kraken/users/ $+ $1 $+ /follows/channels/k3mistvideo HTTP/1.0
  %<< Host: $sock($sockname).addr
  %<< $crlf
}

on *:sockread:demo.*:{
  tokenize 32 $sock($sockname).mark
  ; now $1 is the nick passed to the alias and $2 is the channel
  
  var %header
  sockread %header
  if (*status: 200 OK* iswm %header) { msg $2 STATUS: $1 is following | sockclose $sockname }
  if (*status: 404 Not Found* iswm %header) { msg $2 STATUS: $1 is NOT following | sockclose $sockname }
}


Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
Loki12583 Sorry about that I am still a little green in these forums. I have put those commands and have the script like this:

Code:
on *:sockopen:demo.*: {
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
...
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
}

on *:sockread:demo.*: {
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
  /msg #RuLerZ 2
...
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
}

on *:sockclose:demo.*:{
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
  /msg #RuLerZ 3
...
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
}


Alias demo {
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
  /msg #RuLerZ 1
...
  msg #rulerz error: $sockerr $sock($sockname).wsmsg
}


The Response is this:
[01:41] <@RuLerZ> !test
[01:41] <@Bot> error:
[01:41] <@Bot> 1
[01:41] <@Bot> error:
[01:41] <@Bot> error: 0 [0] Unknown Error
[01:41] <@Bot> error: 0 [0] Unknown Error
[01:41] <@Bot> error: 3 [10101] Host disconnected
[01:41] <@Bot> 3
[01:41] <@Bot> Not following
[01:41] <@Bot> error: 3 [10101] Host disconnected

Deega

I have put your code in and its the same thing it will not run the sockread. Just says:
[01:50] <@RuLerZ> !best
[01:50] <@bot> 1
[01:50] <@bot> sockopen (Checking RuLerZ1364 status)


I am sorry this is such a pain guys I really appreciate all the help you have been up to this point I have also tried this code on another computer with only mirc on it to make sure it was not a firewall issue, same results.

Last edited by RuLerZ; 13/05/13 06:19 AM. Reason: working now lol
Joined: Oct 2012
Posts: 164
D
Vogon poet
Offline
Vogon poet
D
Joined: Oct 2012
Posts: 164
Those "errors" are actually not errors smile
The "error" lines without data ([01:41] <@Bot> error:) are not really socket errors (they just are not associated with any sockets)

The lines with error "0" ([01:41] <@Bot> error: 0 [0] Unknown Error) are not errors (error "0" means no error)

The last ones come from the sockclose event you have, and of course by that point the host HAS disconnected, again not an error.
[01:41] <@Bot> error: 3 [10101] Host disconnected


Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
I have tried multiple machines and cannot get sockread to work Fresh install windows 7 fresh install mirc newest version with/without Open SSL with the same results not running the code in sockread. Why would this not work on my machines. Sockread does work fine with port 80 connections but not 443.

Last edited by RuLerZ; 13/05/13 03:35 PM.
Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
I got it working had to play a whole lot with openssl-0.9.8y-setup.exe vs openssl-1.0.1e-setup.exe and the c++ had to install it in a very specific order for some reason for it to work. Thank you everyone!!!

Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
Hello everyone,

I have been trying to adjust the code used above to get specific detail from a page but have been getting nowhere. I am trying to get from:

%<< GET /kraken/users/ $+ $1 $+ /follows/channels/k3mistvideo HTTP/1.0

to get "created_at":"2014-01-08T18:59:15Z" from a JSON file. For example:

https://api.twitch.tv/kraken/users/jester87d3/follows/channels/k3mistvideo

returns 2013-05-05

Any help would be great, thanks in advance.

Joined: Apr 2013
Posts: 27
R
RuLerZ Offline OP
Ameglian cow
OP Offline
Ameglian cow
R
Joined: Apr 2013
Posts: 27
I found the $json script found here: http://www.mircscripts.org/comments.php?cid=4407 that helped me find the answer to my question. (=


Link Copied to Clipboard