mIRC Home    About    Download    Register    News    Help

Print Thread
#242769 30/08/13 02:04 AM
Joined: Aug 2013
Posts: 3
L
Lynx876 Offline OP
Self-satisified door
OP Offline
Self-satisified door
L
Joined: Aug 2013
Posts: 3
Hi. I recently started creating a Twitch bot, but I'm having trouble with the API/JSON.

I'll jump straight in and show my code and explain what's happening etc.
Code:
on *:SOCKOPEN:TwitchFollowers: {

  if ($sockerr) { echo -ag Socket error! | halt }
  else { echo -ag TwitchFollowers socket open. }

  sockwrite -nt TwitchFollowers GET /kraken/channels/lynx876/follows HTTP/1.1
  sockwrite -nt TwitchFollowers Host: twitch.tv
  sockwrite -nt TwitchFollowers $crlf

  sockread
}

on @*:TEXT:!followers:#: {

  ; if ($sslready) { echo -ag sslReady is true }
  ; else { echo -ag sslReady is false }

  set %address api.twitch.tv

  sockopen -e TwitchFollowers %address 443
}

on *:SOCKCLOSE:TwitchFollowers: {
  echo -ag TwitchFollowers socket closed.
}

on *:SOCKREAD:TwitchFollowers: {
  if ($sockerr) { echo -ag Socket error! | halt }

  while (1) {
    sockread %temp

    ; when nothing is read, the buffer was empty, so we return
    if ($sockbr == 0) return

    echo -ag sockread: %temp
  }
}


The following is the output.
I <SNIP>'ed some out, not sure if this is for my account only.
Code:
TwitchFollowers socket open.
sockread: HTTP/1.1 200 OK
sockread: Server: nginx
sockread: Date: Fri, 30 Aug 2013 01:53:45 GMT
sockread: Content-Type: application/json; charset=utf-8
sockread: Content-Length: 8628
sockread: Connection: close
sockread: status: 200 OK
sockread: x-api-version: 3
sockread: x-request-id: <SNIP>
sockread: via: Twice 0.2 twice8:3356
sockread: x-runtime: 0.830537
sockread: x-geo: GB
sockread: etag: "<SNIP>"
sockread: x-ua-compatible: IE=Edge,chrome=1
sockread: cache-control: max-age=0, private, must-revalidate
sockread: x-rack-cache: miss
sockread: Front-End-Https: on
sockread:
TwitchFollowers socket closed.


So, I see that 'Content-Type' is JSON. How would I go about parsing that data?

On my followers json page, can I not just parse this as text?
If so, how do I actually go about getting the data in a variable instead of the above output when I connect?

Thanks for any help!

Joined: Aug 2013
Posts: 3
L
Lynx876 Offline OP
Self-satisified door
OP Offline
Self-satisified door
L
Joined: Aug 2013
Posts: 3
OK. So I found out why I wasn't getting any data from this!

In sockread, I had the following line which was causing the problem.
Code:
if ($sockbr == 0) return


The output has a blank line before outputting the data!

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
There are existing json parsing scripts, you should probably use one of those.

Joined: Aug 2013
Posts: 3
L
Lynx876 Offline OP
Self-satisified door
OP Offline
Self-satisified door
L
Joined: Aug 2013
Posts: 3
I only want certain data pulled from it, so I created a C++ program that strips all the stuff I don't need then I call that from mIRC with parameters. Seems to be working fine.

Joined: Dec 2013
Posts: 1
L
Mostly harmless
Offline
Mostly harmless
L
Joined: Dec 2013
Posts: 1
Here is my working code of a Twitch Info Script for mIRC, hope it helps:
Twitch Sockread Script for mIRC


Link Copied to Clipboard