Skip to content

Commit

Permalink
Add Get Viewers
Browse files Browse the repository at this point in the history
  • Loading branch information
tnovas committed Mar 19, 2018
2 parents 8abff85 + 602abc7 commit dd44c18
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 6 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ For get your live stream information you have to call `liveStream`
youtube.liveStream();
```

### Get Viewers:
For get live viewers you have to call `getViewers`

```js
youtube.getViewers();
```

### Get Live Chat:
For get live chat message you have to call `liveChat`

Expand Down Expand Up @@ -125,4 +132,5 @@ First change the `clientId` and `clientSecret` in `tests/integration.js` with yo
- `http://localhost:8080/getChannel` return information of your [channel](#get-channel)
- `http://localhost:8080/credentials` [get credentials](#get-credentials)
- `http://localhost:8080/reconnect` [refresh access token](#refresh-access-token)
- `http://localhost:8080/getViewers` [get viewers](#get-viewers)

18 changes: 14 additions & 4 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
let axios = require('axios');
let OAuth2 = require('oauth20');
let getYoutube = Symbol('getYoutube');
let getViewers = Symbol('getViewers');
let credentialsYoutube = Symbol('credentialsYoutube');
let urlsYoutube = Symbol('urlsYoutube');

Expand Down Expand Up @@ -35,7 +36,7 @@ class Youtube extends OAuth2 {
}

getChannel() {
let url = `${this[urlsYoutube].channels}`;
let url = this[urlsYoutube].channels;
let params = {
part: 'snippet,contentDetails,brandingSettings,invideoPromotion,statistics',
mine: true,
Expand All @@ -46,7 +47,7 @@ class Youtube extends OAuth2 {
}

liveStream() {
let url = `${this[urlsYoutube].videos}`;
let url = this[urlsYoutube].videos;
let params = {
part: 'statistics',
id: this[credentialsYoutube].liveId,
Expand All @@ -57,8 +58,12 @@ class Youtube extends OAuth2 {
return this[getYoutube](url, params);
}

getViewers() {
return this[getViewers]();
}

liveChat() {
let url = `${this[urlsYoutube].chats}`;
let url = this[urlsYoutube].chats;
let params = {
part: 'id,snippet,authorDetails',
key: this[credentialsYoutube].key,
Expand All @@ -69,7 +74,7 @@ class Youtube extends OAuth2 {
}

liveBroadcast() {
let url = `${this[urlsYoutube].broadcasts}`;
let url = this[urlsYoutube].broadcasts;
let params = {
part: 'snippet',
broadcastType: 'persistent',
Expand All @@ -91,6 +96,11 @@ class Youtube extends OAuth2 {
headers: {Authorization: `Bearer ${this.getCredentials().accessToken}`}
});
}

[getViewers]() {
let url = `https://www.youtube.com/live_stats?v=${this[credentialsYoutube].liveId}`;
return require('axios').get(url);
}
}

module.exports = Youtube;
17 changes: 16 additions & 1 deletion tests/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ describe('youtube', function() {
channels: 'channels',
chats: 'liveChat/messages',
broadcasts: 'liveBroadcasts',
videos: 'videos'
videos: 'videos',
viewers: 'https://www.youtube.com/live_stats?v='
};
});

Expand Down Expand Up @@ -153,6 +154,20 @@ describe('youtube', function() {
youtube.liveBroadcast().catch((err) => expect(500).to.equal(err.response.status));
});

it('getViewers() should get a count of viewers', () => {
var viewers = 1240;

mock.onGet(urls.viewers).replyOnce(200, {response: viewers});

youtube.getViewers().then(() => expect(JSON.stringify(response)).to.equal(JSON.stringify(response)));
});

it('getViewers() should throw error with a message', () => {
mock.onGet(urls.broadcasts).replyOnce(500, { error: 'error' });

youtube.getViewers().catch((err) => expect(500).to.equal(err.response.status));
});

it('getCredentials() should get credentials', () => {
var credentials = {
accessToken: 'token',
Expand Down
6 changes: 5 additions & 1 deletion tests/integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ app.get('/getChannel', (req, res) => {

app.get('/credentials', (req, res) => {
res.json(youtube.getCredentials());
})
});

app.get('/getViewers', (req, res) => {
youtube.getViewers().then((result) => res.json(result.data)).catch((err) => res.json(err.response.data));
});

app.get('/', (req, res) => {
res.json(youtube.authorizationUrl());
Expand Down

0 comments on commit dd44c18

Please sign in to comment.