nicholastay/node-twitch-get-stream

Javascript solution - get witch stream direct link (for example) in Chrome extension

anonym24 opened this issue · 2 comments

Javascript solution - get witch stream direct link (for example) in Chrome extension

Is there a solution to get stream links with pure JavaScript?

If it requires client-id where can I get it and which one do I need for Chrome extension?

I used browserify to get all modules in one js file
Also I needed to replace some http to https in your code to work it in browser

But I get another error:

bundle2.js:13 Error caught: TypeError: request.get(...).set(...).query(...).buffer is not a function
    at bundle2.js:2493
    at tryCallTwo (bundle2.js:600)
    at doResolve (bundle2.js:755)
    at new Promise (bundle2.js:621)
    at getPlaylist (bundle2.js:2480)
    at bundle2.js:2511
    at tryCallOne (bundle2.js:592)
    at bundle2.js:678
    at MutationObserver.flush (bundle2.js:137)

says buffer isn't not a function:

var getPlaylist = function(channel, accessToken) {
  // Get the playlist with given access token data (parsed /access_token response)
  return new Promise(function(resolve, reject) {
    request
      .get('https://usher.twitch.tv/api/channel/hls/' + channel + '.m3u8')
      .set({ 'Client-ID': clid })
      .query({
        player: 'twitchweb',
        token: accessToken.token,
        sig: accessToken.sig,
        allow_audio_only: true,
        allow_source: true,
        type: 'any',
        p: getRandomIntInclusive(1, 99999)
      })
      .buffer() // buffer the response for m3u data
      .end(function(err, res) {
        if (err) return reject(err);
        if (!res.ok) return reject(new Error('Could not access the twitch API to get the playlist, maybe your internet or twitch is down.'));
        
        return resolve(res.text);
      });
  });
}
twitchStreams.get('dreamhackcs')
    .then(function(streams) {
        console.log('Got stream data.');

        for (var stream of streams)
            console.log(stream.quality + ' (' + stream.resolution + '): ' + stream.url);
    })
    .catch(function(error) {
        if (error)
            return console.log('Error caught:', error);
    });

},{"twitch-get-stream":20}],2:[function(require,module,exports){
"use strict";

I think I tried this in the past, but some API endpoints don't allow cross-origin requests, thus the request will fail. I haven't used browserify in a while, so I don't 100% remember how it works. I'm kinda short on time right now, but if I have time in the future I may look into this again.

If you can work out how to do it that would be cool! An idea I think is you may have to proxy the requests if you do encounter the CORS errors.

Edit: this lib uses superagent for web requests, and they seem to support browserify, so idk

Edit 2: to answer your 2nd question, I guess you could look at my code and follow similar requests but use ajax in the browser instead, and also follow my above advice if you bump into cors