mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Jul 2014
Posts: 48
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Jul 2014
Posts: 48
So I have a script, and am trying to access the youtube API through sockets. Endgame goal: add a youtube video to a youtube playlist. so far I can store the youtube video ID, but just need to figure out how to add that ID to a playlist through the youtube API. I was able to do this through google's "try it feature" at the bottom of this page: https://developers.google.com/youtube/v3/docs/playlistItems/insert but when I try to access the API through mIRC script, it seems like nothing happens.


Code:
alias youtube {
  sockopen ytube www.googleapis.com 443
}

on *:sockopen:ytube:{
  if ($sockerr) { 
    sockclose $sockname
    halt 
  }
  sockwrite -n $sockname GET /youtube/v3/playlistItems?part=id&playlistId=<playlist ID Here>&key=<api key here> HTTP/1.1
  sockwrite -n $sockname Host: www.googleapis.com $crlf $+ $crlf
  
  echo -tag socket write
}

on *:sockread:ytube:{
  if ($sockerr) {
    sockclose $sockname 
    halt
  }
  ;This is where I have tried reading the socket, seeing what was being read, even just msg # read sockets. Nothing happens.
}

Obviously, I removed my playlist ID, and API key, but I know they are good, because When I go to the api url in my browser, it pulls up the playlist and all, so I know the url is good as well as the API key I am using.



So like I said, nothing seems to happen after the sockets are opened. Am I doing it wrong?




This is what it looks like when I pull it up in my browser:
Code:
{
 "kind": "youtube#playlistItemListResponse",
 "etag": "\"jSSWBfHLqA4YDVcP7dOWTvijbRo/C5H7H2Kp11az9zUYXUUF1rICsA8\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#playlistItem",
   "etag": "\"jSSWBfHLqA4YDVcP7dOWTvijbRo/YW4O1EVPAaOqaFMFKk8Q2EYA8uM\"",
   "id": "PLcsh5-UPddz_PFHkGNTxkhs3RilAMb3U09YGFOwlOfMg"
  }
 ]
}




Looking for suggestions... To actually add a video to the playlist, I know I will need to use POST instead of GET, but I am just trying to verify that I can even connect and read info, which doesn't seem to be working.

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
You need to use the -e switch for an ssl connection, and I don't know if the server would like an extra space after the host and before the $crlf.

Code:
alias youtube {
  sockopen -e ytube www.googleapis.com 443
}

on *:sockopen:ytube:{
  if ($sockerr) { 
    sockclose $sockname
    halt 
  }
  sockwrite -n $sockname GET /youtube/v3/playlistItems?part=id&playlistId=<playlist ID Here>&key=<api key here> HTTP/1.1
  sockwrite -n $sockname Host: www.googleapis.com
  sockwrite -n $sockname $crlf
  
  echo -tag socket write
}


This is a very basic sockread:
Code:
on *:sockread:ytube:{
  if ($sockerr) { return }
  var %read | sockread -f %read
  while ($sockbr) {
    if (%read != $null) echo -ag %read
    sockread -f %read
  }
}

Last edited by Loki12583; 24/07/14 03:13 AM.
Joined: Jul 2014
Posts: 48
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Jul 2014
Posts: 48
ok, didn't realize about the -e switch, and I also removed the extra space.

still does not seem like anything is happening after opening the socket.
I have the "echo -tag" posted as such
Code:
alias youtube {
  sockopen -e ytube www.googleapis.com 443
  echo -tag socket open
}

on *:sockopen:ytube:{
  if ($sockerr) { 
    sockclose $sockname
    halt 
  }
  sockwrite -n $sockname GET /youtube/v3/playlistItems?part=id&playlistId=<playlist ID Here>&key=<api key here> HTTP/1.1
  sockwrite -n $sockname Host: www.googleapis.com
  sockwrite -n $crlf
  
  echo -tag socket write
}



and also one in the sockread block. The only thing that is responded to is "socket open" which means that the code is hanging in, or not executing in the sockopen block, right?

Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
I left out the $sockname on one line, once you correct that you should receive a response (I tested it this time).

Code:
sockwrite -n $sockname $crlf

Joined: Jul 2014
Posts: 48
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Jul 2014
Posts: 48
I was wondering about that, but I have already tried with and without the sockname added. Still seems to hang on socket open. Before anyone asks, yes, I am testing with the ID and API key added in.

If you are getting a response, I must have done something else wrong.

This is my current code:
Code:
alias youtube {
  sockopen -e ytube www.googleapis.com 443
  echo -tag socket open
}

on *:sockopen:ytube:{
  echo -tag begin socket write
  if ($sockerr) { 
    echo -tag socket error
    sockclose $sockname
    halt 
  }
  
  sockwrite -n $sockname GET /youtube/v3/playlistItems?part=id&playlistId=<id here>&key=<api here> HTTP/1.1
  sockwrite -n $sockname Host: www.googleapis.com 
  sockwrite -n $sockname $crlf

  echo -tag socket write
}

on *:sockread:ytube:{
  if ($sockerr) { return }
  var %read | sockread -f %read
  while ($sockbr) {
    if (%read != $null) echo -ag %read
    sockread -f %read
    echo -tag socket reading
  }

  ;  var $youtubedata
  ;  sockread $youtubedata

  echo -tag socket read
}

Last edited by AeonPsych; 24/07/14 03:25 AM. Reason: added code
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Make sure to close the socket if it's still open.

Code:
alias youtube {
  sockclose ytube
  sockopen -e ytube www.googleapis.com 443
}

on *:sockopen:ytube:{
  if ($sockerr) { 
    sockclose $sockname
    echo -ag err
    halt 
  }
  sockwrite -n $sockname GET /youtube/v3/playlistItems?part=id&playlistId=<playlist ID Here>&key=<api key here> HTTP/1.0
  sockwrite -n $sockname Host: www.googleapis.com
  sockwrite -n $sockname $crlf
  
  echo -tag socket write
}

on *:sockread:ytube:{
  if ($sockerr) { return }
  var %read | sockread -f %read
  while ($sockbr) {
    if (%read != $null) echo -ag %read
    sockread -f %read
  }
}


I used this exact script and he response was printed. I think you may also want to request HTTP/1.0 instead of 1.1 so you don't need to support chunked transfer encoding.

Last edited by Loki12583; 24/07/14 03:29 AM.
Joined: Jul 2014
Posts: 48
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Jul 2014
Posts: 48
Once you brought it up about making sure the socket was closed, I thought that was it. Added in sockclose, and still no response after socket has been opened.


I even copy pasted your posted code. Still no response. Yours doesn't respond and all (but you don't have an echo for the sockopen).

I'm stumped. I might try this on a blank script file to see. Maybe I have another part of my script conflicting with it.

Edit: Trying it on a blank script still no response. I added in an echo just after
Code:
sockopen -e ytube www.googleapis.com 443

and it will respond with that, but none of the other echos respond.
I've tried with my playlist ID and API key plugged in, and also with your exact script copy/pasted. No change at all. Is there some kind of configuration or plugin I am supposed to have installed or set somewhere?

Last edited by AeonPsych; 24/07/14 03:47 AM.
Joined: Jan 2004
Posts: 1,358
L
Hoopy frood
Offline
Hoopy frood
L
Joined: Jan 2004
Posts: 1,358
Oh yeah, you do need the ssl libraries: http://www.mirc.com/ssl.html

In mIRC type //echo $sslready to see if they're loaded

Joined: Jul 2014
Posts: 48
A
Ameglian cow
OP Offline
Ameglian cow
A
Joined: Jul 2014
Posts: 48
"$false"

Guess that would be my problem...

Seems to work now. Looks like it echoed a bunch of garbage though. The only thing that looks relevant to me is the ETag. Which seems to be the only common thing between this response, and the response I got through the browser.


Edit: I've discovered that it is only reading the headers of the page response. Not the rest. Shouldn't be a problem, as I've now verified a connection can be made.
Just need to start figuring out how to POST now.


Link Copied to Clipboard