Skip to content

Instantly share code, notes, and snippets.

@Pepijn98
Last active August 27, 2020 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pepijn98/4f9ad75b1d693308815cdf9136d03c65 to your computer and use it in GitHub Desktop.
Save Pepijn98/4f9ad75b1d693308815cdf9136d03c65 to your computer and use it in GitHub Desktop.
Simple nodejs script to quickly pull channel info from the twitch api (no npm packages required)
#! /usr/bin/env node
const https = require("https");
const fs = require("fs");
const fsp = require("fs/promises");
const path = require("path");
const help_options = ["help", "--help", "-h"];
const config_dir = path.join(process.env.HOME, ".config", "twitch");
const config_file = path.join(process.env.HOME, ".config", "twitch", "config.json")
const args = process.argv.slice(2);
const subcommand = args.shift();
const option_type = args.shift();
const channel = args.shift();
function request_channel_info(client_id) {
if (!channel) {
console.error("Error: Missing channel id");
return;
}
https.get({
host: "api.twitch.tv",
path: `/kraken/channels/${channel}`,
headers: {
"Accept": "application/vnd.twitchtv.v5+json",
"Client-ID": client_id
}
}, (resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
console.log(JSON.parse(data));
});
}).on("error", (err) => {
console.error("Error: " + err.message);
});
}
function request_channel_info_by_name(client_id) {
if (!channel) {
console.error("Error: Missing channel name");
return;
}
https.get({
host: "api.twitch.tv",
path: `/kraken/users?login=${channel}`,
headers: {
"Accept": "application/vnd.twitchtv.v5+json",
"Client-ID": client_id
}
}, (resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
const response = JSON.parse(data);
if (response.users.length <= 0) {
console.error("Error: No user found with this name");
return;
}
console.log(response.users[0]);
});
}).on("error", (err) => {
console.error("Error: " + err.message);
});
}
async function main() {
if (help_options.includes(subcommand)) {
console.log(`
$$$$$$$$\\ $$\\ $$\\ $$\\
\\__$$ __| \\__| $$ | $$ |
$$ |$$\\ $$\\ $$\\ $$\\ $$$$$$\\ $$$$$$$\\ $$$$$$$\\
$$ |$$ | $$ | $$ |$$ |\\_$$ _| $$ _____|$$ __$$\\
$$ |$$ | $$ | $$ |$$ | $$ | $$ / $$ | $$ |
$$ |$$ | $$ | $$ |$$ | $$ | $$\ $$ | $$ | $$ |
$$ |\\$$$$$\\$$$$ |$$ | \\$$$$ |\\$$$$$$$\\ $$ | $$ |
\\__| \\_____\\____/ \\__| \\____/ \\_______|\\__| \\__|
`);
console.log(`
${process.argv[1].split("/").pop()} <subcommand> [option]
Subcommands:
channel\t\tGet info about a channel using id or name
help\t\tShow this message
Options:
--name\t\tChannel name, returns basic channel data (including channel id)
--id\t\tChannel id, returns more detailed channel info
`);
return;
}
const config_dir_check = fs.existsSync(config_dir);
if (!config_dir_check) {
await fsp.mkdir(config_dir);
}
const config_file_check = fs.existsSync(config_file);
if (!config_file_check) {
await fsp.writeFile(config_file, JSON.stringify({ client_id: "" }));
}
const config = JSON.parse(await fsp.readFile(config_file));
if (!config.client_id) {
console.error(`Error: No client id set in the config file, please update ${config_file}`);
return;
}
switch (subcommand) {
case "channel":
switch (option_type) {
case "--id":
request_channel_info(config.client_id);
break;
case "--name":
request_channel_info_by_name(config.client_id);
break;
default:
console.error("Error: Invalid option, this can only be \"--id\" or \"--name\"");
break;
}
break;
default:
console.error("Error: Invalid subcommand, this can only be \"channel\" or \"help\"");
break;
}
}
main().catch(console.error);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment