UNPKG

twitcher.js

Version:

TWİTCH APİ MODULE

186 lines (184 loc) 7.45 kB
const axios = require("axios") const event = require("events") const fs = require("fs") class Twitch { constructor(op = {}) { this.secret = op.Secret this.id = op.İD if(!this.secret && this.id){ throw Error("Please Enter Client Secret or Client İD!\n https://dev.twitch.tv/console/apps App Create!") } } async setToken(){ let data = JSON.parse(fs.readFileSync(__dirname+`/config.json`, "utf8")); if(data["token_twitch"]){return} let t = await axios( { method: 'POST', url: 'https://id.twitch.tv/oauth2/token?client_id='+this.id+'&client_secret='+this.secret+'&grant_type=client_credentials' } ).catch((error) => { return console.log(error.response.data) }) data["token_twitch"] = t.data.access_token fs.writeFileSync(__dirname+`/config.json`, JSON.stringify(data, null, 1)) return "Token Seted!" } async getToken(){ let data = JSON.parse(fs.readFileSync(__dirname+`/config.json`, "utf8")); if(!data["token_twitch"]){return await this.setToken()} return data["token_twitch"] } async getUser(username){ if(!username){throw Error("Please Username Enter / login method using..")} let a = await axios( 'https://api.twitch.tv/helix/users?login='+username,{ headers: { 'Authorization': 'Bearer '+await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { console.log(error) })//a.data.data[0].id let b = await axios('https://api.twitch.tv/helix/users/follows?to_id=96413753',{ headers: { 'Authorization': 'Bearer '+await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { console.log(error) }) let u = a.data.data[0] let t = b.data return { id: u.id, login: u.login, display_name: u.display_name, type: u.type, broadcaster_type: u.broadcaster_type, description: u.description, profile_image_url: u.profile_image_url, offline_image_url: u.offline_image_url, view_count: u.view_count, created_at: u.created_at, total_follower:t.total, last_follower:{ login:t.data[0].from_login, name:t.data[0].from_name, id:t.data[0].from_id, followed_at:t.data[0].followed_at }, followers:[t.data] } } async getStream(username){ let b = await axios('https://api.twitch.tv/helix/streams?user_login='+username,{ headers: { 'Authorization': 'Bearer '+await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { console.log(error) }) let s = b.data.data[0] return b.data.data[0] ? { title:s.title, viewer_count: 395, id:s.id, user_id:s.user_id, user_login:s.user_login, user_name:s.user_name, game_id:s.game_id, game_name:s.game_name, type:s.type, started_at:s.started_at, language:s.language, thumbnail_url:s.thumbnail_url.replace("{width}x{height}","1080x720").replace("{width}x{height}","1080x720"), } : false } async getVideo(id){ if(!id){throw Error("Please Enter Video İD or Video URL")} if(id.split("www.twitch.tv/videos/")[1]){ id = id.slice(id.length-10) if(id.length > 10){throw Error("İNVALİD / Please Enter Video İD or Video URL")} } let b = await axios('https://api.twitch.tv/helix/videos?id='+id,{ headers: { 'Authorization': 'Bearer '+await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { console.log(error) }) let v = b.data.data[0] let vj = { title:v.title, description:v.description, id: v.id, created_at: v.created_at, published_at: v.published_at, viewable:v.viewable, stream_id:v.stream_id, user_id:v.user_id, user_login:v.user_login, user_name:v.user_name, language:v.language, url:v.url, view_count:v.view_count, thumbnail_url:v.thumbnail_url.replace("%{width}x%{height}","1080x720").replace("%{width}x%{height}","1080x720"), duration:v.duration.replace("h",":").replace("h",":").replace("m",":").replace("m",":").replace("s","").replace("s","") } return b.data.data[0] ? vj : false } async getClips(id){ if(!id){throw Error("Please Enter id / broadcaster_id")} if(id.split("www.twitch.tv/")[1]){ let idd = id+"" id = idd.split("/")[5].split("?")[0] } let b = await axios('https://api.twitch.tv/helix/clips?'+`${Number(id) ? `broadcaster_id=${id}` : `id=${id}`}`,{ headers: { 'Authorization': 'Bearer '+await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { return console.log(error.response.data.message) }) let c = b.data.data[0] return Number(id) === false ? { title:c.title, id:c.id, view_count:c.view_count, url:c.url, embed_url:c.embed_url, broadcaster_id:c.broadcaster_id, broadcaster_name:c.broadcaster_name, creator_id:c.creator_id, creator_name:c.creator_name, game_id:c.game_id, language:c.language, created_at:c.created_at, thumbnail_url:c.thumbnail_url, duration:c.duration, } : b.data.data } async get(get,op,e){ if(!get && !op){throw Error("Please Enter options")} if(op[0]){throw Error("Please Options Enter JsonTYPE\n"+`FALSE USE - ${op.replace(`{"`,"").replace(`{"`,"").replace(`{"`,"").replace(`"}`,"").replace(`"}`,"").replace(`":`,"").replace(`"`,",").replace(`"`,",").replace(`"`,",")}\nTRUE USE - {"example":"${op}"}`)} let mt = op.method+"" let gt = get+"" let b = await axios({ url: 'https://api.twitch.tv/helix/'+gt, params:op, method:op.method ? mt.toLocaleUpperCase() : "GET", headers: { 'Authorization': 'Bearer '+ await this.getToken(), 'Client-Id': this.id, 'Content-Type': 'application/json', }}).catch((error) => { return console.log(error.response) }) let c = b.data.data e(c) return c } } module.exports = Twitch