twitchrequest
Advanced tools
Comparing version 1.1.1 to 1.1.2
@@ -18,3 +18,3 @@ declare module 'twitchrequest' { | ||
constructor(options: TwitchRequestOptions); | ||
public once<K extends keyof TwitchRequestEvents>(event: K, listener: (...args: TwitchRequestEvents[K]) => void): this; | ||
@@ -25,2 +25,6 @@ public on<K extends keyof TwitchRequestEvents>(event: K, listener: (...args: TwitchRequestEvents[K]) => void): this; | ||
public getStream(username: string): Promise<StreamData>; | ||
public addChannel(channel: string): void; | ||
public removeChannel(channel: string): void; | ||
public includesChannel(channel: string): boolean; | ||
private getTwitchChannel(channel: string): TwitchChannel; | ||
} | ||
@@ -27,0 +31,0 @@ |
274
index.js
@@ -63,62 +63,9 @@ "use strict"; | ||
_this.listener = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var getToken, token, getData; | ||
var token; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
getToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var promise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: "https://id.twitch.tv/oauth2/token", | ||
json: true, | ||
body: { | ||
client_id: this.clientid, | ||
client_secret: this.clientsecret, | ||
grant_type: 'client_credentials' | ||
} | ||
}; | ||
Request.post(options, function (err, res, body) { | ||
if (err) | ||
return console.log(err); | ||
resolve(res.body.access_token); | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, promise]; | ||
}); | ||
}); }; | ||
return [4 /*yield*/, getToken()]; | ||
case 0: return [4 /*yield*/, this.getToken()]; | ||
case 1: | ||
token = _a.sent(); | ||
getData = function (url, token) { | ||
var promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: url, | ||
method: 'GET', | ||
headers: { | ||
'client-id': this.clientid, | ||
'Authorization': 'Bearer ' + token | ||
} | ||
}; | ||
Request.get(options, function (err, res, body) { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve(JSON.parse(body)); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return promise; | ||
}; | ||
this.channel.forEach(function (ch) { return __awaiter(_this, void 0, void 0, function () { | ||
@@ -130,3 +77,3 @@ var response, e, res, r, ee, err_1; | ||
_a.trys.push([0, 7, , 8]); | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/search/channels?query=" + ch.name), token)]; | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/search/channels?query=" + ch.name), token)]; | ||
case 1: | ||
@@ -139,3 +86,3 @@ response = _a.sent(); | ||
e = response.data.find(function (d) { return d.display_name === ch.name; }); | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/games?id=" + e.game_id), token)]; | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/games?id=" + e.game_id), token)]; | ||
case 3: | ||
@@ -145,3 +92,3 @@ res = _a.sent(); | ||
if (!(e.is_live && !ch.isLive())) return [3 /*break*/, 5]; | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/streams?user_login=" + ch.name), token)]; | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/streams?user_login=" + ch.name), token)]; | ||
case 4: | ||
@@ -217,73 +164,12 @@ r = _a.sent(); | ||
promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var getToken, token, getData, user, response, e, err_2; | ||
var _this = this; | ||
var token, user, response, e, err_2; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
getToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var prom; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
prom = new Promise(function (resolv, rej) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: "https://id.twitch.tv/oauth2/token", | ||
json: true, | ||
body: { | ||
client_id: this.clientid, | ||
client_secret: this.clientsecret, | ||
grant_type: 'client_credentials' | ||
} | ||
}; | ||
Request.post(options, function (err, res, body) { | ||
if (err) { | ||
resolv("Error"); | ||
} | ||
else { | ||
resolv(res.body.access_token); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, prom]; | ||
}); | ||
}); }; | ||
return [4 /*yield*/, getToken()]; | ||
case 0: return [4 /*yield*/, this.getToken()]; | ||
case 1: | ||
token = _a.sent(); | ||
getData = function (url, token) { return __awaiter(_this, void 0, void 0, function () { | ||
var promise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
promise = new Promise(function (reso, reje) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: url, | ||
method: 'GET', | ||
headers: { | ||
'client-id': this.clientid, | ||
'Authorization': 'Bearer ' + token | ||
} | ||
}; | ||
Request.get(options, function (err, res, body) { | ||
if (err) { | ||
reje(err); | ||
} | ||
else { | ||
reso((JSON.parse(body))); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, promise]; | ||
}); | ||
}); }; | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 4, , 5]); | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/users?login=" + username), token)]; | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/users?login=" + username), token)]; | ||
case 3: | ||
@@ -335,73 +221,12 @@ response = _a.sent(); | ||
promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var getToken, token, getData, user, response, e, res, ee, err_3; | ||
var _this = this; | ||
var token, user, response, e, res, ee, err_3; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
getToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var prom; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
prom = new Promise(function (resolv, rej) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: "https://id.twitch.tv/oauth2/token", | ||
json: true, | ||
body: { | ||
client_id: this.clientid, | ||
client_secret: this.clientsecret, | ||
grant_type: 'client_credentials' | ||
} | ||
}; | ||
Request.post(options, function (err, res, body) { | ||
if (err) { | ||
resolv("Error"); | ||
} | ||
else { | ||
resolv(res.body.access_token); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, prom]; | ||
}); | ||
}); }; | ||
return [4 /*yield*/, getToken()]; | ||
case 0: return [4 /*yield*/, this.getToken()]; | ||
case 1: | ||
token = _a.sent(); | ||
getData = function (url, token) { return __awaiter(_this, void 0, void 0, function () { | ||
var promise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
promise = new Promise(function (reso, reje) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: url, | ||
method: 'GET', | ||
headers: { | ||
'client-id': this.clientid, | ||
'Authorization': 'Bearer ' + token | ||
} | ||
}; | ||
Request.get(options, function (err, res, body) { | ||
if (err) { | ||
reje(err); | ||
} | ||
else { | ||
reso((JSON.parse(body))); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, promise]; | ||
}); | ||
}); }; | ||
_a.label = 2; | ||
case 2: | ||
_a.trys.push([2, 7, , 8]); | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/streams?user_login=" + username.toLowerCase()), token)]; | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/streams?user_login=" + username.toLowerCase()), token)]; | ||
case 3: | ||
@@ -414,11 +239,7 @@ response = _a.sent(); | ||
case 4: | ||
e = response.data.find(function (d) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, d.user_name.toLowerCase() === username]; | ||
}); }); }); | ||
return [4 /*yield*/, getData(("https://api.twitch.tv/helix/search/channels?query=" + username), token)]; | ||
e = response.data.find(function (d) { return d.user_name.toLowerCase() === username; }); | ||
return [4 /*yield*/, this.getData(("https://api.twitch.tv/helix/search/channels?query=" + username), token)]; | ||
case 5: | ||
res = _a.sent(); | ||
ee = res.data.find(function (d) { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { | ||
return [2 /*return*/, d.display_name.toLowerCase() === username]; | ||
}); }); }); | ||
ee = res.data.find(function (d) { return d.display_name.toLowerCase() === username; }); | ||
user = new StreamData(e, e.user_name.toLowerCase(), e.title, e.game_name, ee.thumbnail_url, e.thumbnail_url.replace('{width}', '440').replace('{height}', '248'), e.viewer_count); | ||
@@ -439,2 +260,69 @@ resolve(user); | ||
}); }; | ||
/** | ||
* @private | ||
*/ | ||
_this.getToken = function () { return __awaiter(_this, void 0, void 0, function () { | ||
var promise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: "https://id.twitch.tv/oauth2/token", | ||
json: true, | ||
body: { | ||
client_id: this.clientid, | ||
client_secret: this.clientsecret, | ||
grant_type: 'client_credentials' | ||
} | ||
}; | ||
Request.post(options, function (err, res, body) { | ||
if (err) { | ||
resolve("Error"); | ||
} | ||
else { | ||
resolve(res.body.access_token); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, promise]; | ||
}); | ||
}); }; | ||
/** | ||
* @private | ||
* @param url URL | ||
* @param token Token | ||
*/ | ||
_this.getData = function (url, token) { return __awaiter(_this, void 0, void 0, function () { | ||
var promise; | ||
var _this = this; | ||
return __generator(this, function (_a) { | ||
promise = new Promise(function (resolve, reject) { return __awaiter(_this, void 0, void 0, function () { | ||
var options; | ||
return __generator(this, function (_a) { | ||
options = { | ||
url: url, | ||
method: 'GET', | ||
headers: { | ||
'client-id': this.clientid, | ||
'Authorization': 'Bearer ' + token | ||
} | ||
}; | ||
Request.get(options, function (err, res, body) { | ||
if (err) { | ||
reject(err); | ||
} | ||
else { | ||
resolve((JSON.parse(body))); | ||
} | ||
}); | ||
return [2 /*return*/]; | ||
}); | ||
}); }); | ||
return [2 /*return*/, promise]; | ||
}); | ||
}); }; | ||
_this.interval = options.interval * 1000 || 30000; | ||
@@ -441,0 +329,0 @@ _this.channel = []; |
{ | ||
"name": "twitchrequest", | ||
"version": "1.1.1", | ||
"version": "1.1.2", | ||
"description": "Get Twitch requests for streams, especially when a stream goes live or stops streaming", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -9,3 +9,3 @@ # What is Twitch Requests? | ||
``` | ||
const { TwitchRequests } = require('twitchrequests'); | ||
const { TwitchRequest } = require('twitchrequest'); | ||
@@ -25,3 +25,3 @@ const options = { | ||
}; | ||
const client = new TwitchRequests(options); | ||
const client = new TwitchRequest(options); | ||
@@ -28,0 +28,0 @@ client.on('live', (data) => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
238547
3009