This repository has been archived by the owner on Apr 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtwitch.js
173 lines (136 loc) · 3.94 KB
/
twitch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
var request = require('request');
var config = require('./config');
var twitch = {};
twitch.baseRequest = request.defaults({
headers: {
'Accept': 'application/vnd.twitchtv.v3+json',
'Client-ID': config.twitch.clientId
}
});
twitch.getOauthRequest = function(accessToken) {
var twi = this;
return (twi.baseRequest).defaults({
headers: {
'Authorization': 'OAuth ' + accessToken
}
});
}
/**
* Either parses a Twitch response into an object,
* or returns an HTTP status code (or -1) if there's an error
*/
twitch.handleErrors = function(error, response, body) {
if (error) return -1;
if (response.statusCode != 200) return response.statusCode;
var data;
try {
data = JSON.parse(body);
} catch (e) {
return -1;
}
if (typeof data.error !== 'undefined') {
return (typeof data.status === 'number') ? data.status : -1;
}
return data;
}
/**
* Gets the URL a user must access to authenticate via Twitch
*/
twitch.getLoginUrl = function() {
return config.twitch.apiRoot +
"/oauth2/authorize" +
"?response_type=code" +
"&client_id=" + config.twitch.clientId +
"&redirect_uri=" + config.twitch.redirectUri +
"&scope=" + config.twitch.scope;
}
/**
* Returns whether or not a username is a valid Twitch name
*/
twitch.validateUsername = function(username) {
var pattern = /^[A-Z0-9\-_]{4,25}$/i;
return pattern.test(username);
}
/**
* Attempts to get an access token for a user who tried to log in
*
* Calls callback with a single parameter. The parameter should
* be the object from the API response. This function will verify
* that the object includes an access_token. A refresh_token and
* scopes array are also expected, but not verified to exist.
*
* If an error occurred, it will instead return the HTTP status code
* (if there is no code available for the error, it'll be -1).
*/
twitch.authNewUser = function(code, callback) {
var twi = this;
(twi.baseRequest).post({
url: config.twitch.apiRoot + '/oauth2/token',
form: {
client_id: config.twitch.clientId,
client_secret: config.twitch.clientSecret,
grant_type: "authorization_code",
redirect_uri: config.twitch.redirectUri,
code: code
}
}, function(error, response, body) {
var data = twi.handleErrors(error, response, body);
if (typeof data !== 'object') {
callback(data);
return;
}
if (typeof data.access_token !== 'string') {
callback(-1);
return;
}
callback(data);
});
}
/**
* Make any basic GET call to the Twitch API
*
* Parameters:
* - endpoint: Twitch API endpoint to call (e.g. "/user")
* - callback: function to call with the resulting json or error code
* - oauthToken: OAuth access token (optional; won't use OAuth if unset)
*/
twitch.makeGetRequest = function(endpoint, callback, oauthToken) {
var twi = this;
var request = null;
if (oauthToken === false) {
request = twi.baseRequest;
} else {
request = twi.getOauthRequest(oauthToken);
}
request.get({
url: config.twitch.apiRoot + endpoint
}, function(error, response, body) {
var data = twi.handleErrors(error, response, body);
callback(data);
});
}
twitch.getUserByToken = function(accessToken, callback) {
var twi = this;
twi.makeGetRequest("/user", callback, accessToken);
}
// unused and thus untested
twitch.getUserByName = function(username, callback) {
var twi = this;
if (!twi.validateUsername(username)) {
callback(-1);
} else {
twi.makeGetRequest("/user/" + username, callback);
}
}
// TODO support for following more than 100 streams
twitch.getFollowedStreams = function(accessToken, callback) {
var twi = this;
twi.makeGetRequest("/streams/followed?limit=100&offset=0", function(data){
// if (typeof data.streams !== 'object' || !Array.isArray(data.streams)) {
// callback(-1);
// return;
// }
callback(data);
}, accessToken);
}
module.exports = twitch;