Skip to content

Instantly share code, notes, and snippets.

@jstrickl-tunes
Created August 2, 2022 20:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstrickl-tunes/b99a477b32fe2edd19597868d5ae2463 to your computer and use it in GitHub Desktop.
Save jstrickl-tunes/b99a477b32fe2edd19597868d5ae2463 to your computer and use it in GitHub Desktop.
import * as coda from "@codahq/packs-sdk";
export const pack = coda.newPack();
pack.setUserAuthentication({
type: coda.AuthenticationType.OAuth2,
authorizationUrl: "https://id.twitch.tv/oauth2/authorize",
tokenUrl: "https://id.twitch.tv/oauth2/token",
scopes: ["user:read:email", "channel:manage:schedule", "user:read:follows"],
additionalParams: { response_type: "code" },
scopeDelimiter: " ",
});
async function getUser(context) {
let response = await context.fetcher.fetch({
method: "GET",
url: "https://api.twitch.tv/helix/users",
headers: { "Client-ID": "lnh5mqe2yfy80n7v0nk8m5uwt3krif" }
});
return await response.body.data[0];
}
pack.addFormula({
name: "Current_User",
description: "Gets current account user.",
connectionRequirement: coda.ConnectionRequirement.Required,
parameters: [],
resultType: coda.ValueType.String,
execute: async function ([], context) {
return getUser(context).then((data) => data.display_name);
},
});
const FollowedStreamsSchema = coda.makeObjectSchema({
properties: {
user_name: { type: coda.ValueType.String },
user_login: { type: coda.ValueType.String },
id: { type: coda.ValueType.String },
game_name: { type: coda.ValueType.String },
title: { type: coda.ValueType.String },
viewer_count: { type: coda.ValueType.String },
thumbnail: { type: coda.ValueType.String, codaType: coda.ValueHintType.ImageAttachment, fromKey: "thumbnail_url", },
url: { type: coda.ValueType.String, codaType: coda.ValueHintType.Url, },
},
featuredProperties: ["user_name", "game_name", "thumbnail", "url", "viewer_count"],
displayProperty: "user_name", // Which property above to display by default.
idProperty: "id", // Which property above is a unique ID.
});
pack.addSyncTable({
name: "FollowedActiveStreams",
description: "Gets the users followed streams currently active.",
schema: FollowedStreamsSchema,
identityName: "Streams",
formula: {
name: "FollowedStreams",
description: "Gets the users followed streams currently active.",
extraOAuthScopes: ["user:read:follows"],
parameters: [],
execute: async function ([], context) {
let thing = getUser(context).then(async data => {
let url = `https://api.twitch.tv/helix/streams/followed?user_id=${data.id}`;
let response = await context.fetcher.fetch({
method: "GET",
url: url,
headers: { "Client-ID": "lnh5mqe2yfy80n7v0nk8m5uwt3krif" }
});
let items = response.body.data;
for (var i = 0; i < items.length; i++) {
let new_thumbnail = items[i].thumbnail_url.replace("{width}", "200");
new_thumbnail = new_thumbnail.replace("{height}", "200");
items[i].url = "https://www.twitch.tv/" + items[i].user_login;
items[i].thumbnail_url = new_thumbnail;
}
return {
result: items,
}
});
return await thing;
}
},
});
pack.addSyncTable({
name: "ActiveStreams",
description: "Gets the users followed streams currently active.",
schema: FollowedStreamsSchema,
identityName: "Streams2",
formula: {
name: "FollowedStreams",
description: "Gets the users followed streams currently active.",
extraOAuthScopes: ["user:read:follows"],
parameters: [
coda.makeParameter({
type: coda.ParameterType.StringArray,
name: "channel_names",
description: "The Channel Names.",
optional: true,
}),
],
execute: async function ([channel_names = []], context) {
let param: string = "";
channel_names.forEach((name) => {
param += "user_login=" + name + "&";
});
param = param.slice(0, -1);
let thing = getUser(context).then(async data => {
let url = `https://api.twitch.tv/helix/streams?${param}`;
let response = await context.fetcher.fetch({
method: "GET",
url: url,
headers: { "Client-ID": "lnh5mqe2yfy80n7v0nk8m5uwt3krif" }
});
let items = response.body.data;
for (var i = 0; i < items.length; i++) {
let thumbnail = items[i].thumbnail_url.replace("{width}", "200");
thumbnail = thumbnail.replace("{height}", "200");
items[i].url = "https://www.twitch.tv/" + items[i].user_login;
items[i].thumbnail_url = thumbnail;
}
return {
result: items,
}
});
return await thing;
}
},
});
pack.addFormula({
name: "CreateStreamScheduleSegment",
description: "<Help text for the formula>",
parameters: [
coda.makeParameter({
type: coda.ParameterType.String,
name: "start_time",
description: "<Help text for the parameter>",
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "timezone",
description: "<Help text for the parameter>",
}),
coda.makeParameter({
type: coda.ParameterType.Boolean,
name: "is_recurring",
description: "<Help text for the parameter>",
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "duration",
description: "<Help text for the parameter>",
optional: true
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "category_id",
description: "<Help text for the parameter>",
optional: true
}),
coda.makeParameter({
type: coda.ParameterType.String,
name: "title",
description: "<Help text for the parameter>",
optional: true
}),
],
extraOAuthScopes: ["channel:manage:schedule"],
resultType: coda.ValueType.String,
isAction: true,
execute: async function ([start_time, timezone, is_recurring, duration, category_id, title], context) {
const responseData = await getUser(context).then(async (user) => {
let response = await context.fetcher.fetch({
url: `https://api.twitch.tv/helix/schedule/segment?broadcaster_id=${user.id}`,
method: "POST",
headers: {
"Client-ID": "lnh5mqe2yfy80n7v0nk8m5uwt3krif",
"Content-Type": "application/json"
},
body: JSON.stringify({
start_time, timezone, is_recurring, duration, category_id, title
})
});
return response;
});
return await responseData.body;
},
});
pack.addNetworkDomain("twitch.tv");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment