Skip to content

Instantly share code, notes, and snippets.

@webdev03
Created October 2, 2023 22:44
Show Gist options
  • Save webdev03/6e6adfaff1a72ad5f0f44214af7ca5f8 to your computer and use it in GitHub Desktop.
Save webdev03/6e6adfaff1a72ad5f0f44214af7ca5f8 to your computer and use it in GitHub Desktop.
Wall of Followers Project Thumbnail

Wall of Followers Project Thumbnail

To use this you will need to run

npm install meowclient canvas dotenv

Then create a .env file in this format

SCRATCH_USERNAME=user
SCRATCH_PASSWORD=pass

Instead of user and pass put your Scratch account username and password. In the script on the line starting with const PROJECT_ID you need to remove the comment and insert your project ID at the end of the line. Then when you run the script it will set the thumbnail of that project to a wall of your followers!

// This is marked with CC0 1.0. To view a copy of this license, visit http://creativecommons.org/publicdomain/zero/1.0
const PROJECT_ID = // INSERT YOUR PROJECT ID HERE
import "dotenv/config";
import * as meowclient from "meowclient";
import { createCanvas, loadImage } from "canvas";
const session = new meowclient.ScratchSession();
await session.init(process.env['SCRATCH_USERNAME'], process.env['SCRATCH_PASSWORD']);
const project = new meowclient.Project(session, PROJECT_ID);
try {
let followerList = [];
let offset = 0;
while (true) {
const newFollowers = (await (await fetch(`https://api.scratch.mit.edu/users/${session.username}/followers?limit=40&offset=${offset}`)).json())
.map(x => x.profile.images['55x55']);
followerList.push(...newFollowers);
if (newFollowers.length == 0 || followerList.length >= 331) break;
offset += 40;
}
followerList.length = 331
const canvas = createCanvas(480, 360);
const ctx = canvas.getContext("2d");
await Promise.all(followerList.map(async (followerUrl, i) => {
try {
const image = await loadImage(followerUrl);
const row = Math.floor(i / 24);
const col = i % 24;
const x = col * 25 + 5;
const y = row * 25 + 5;
ctx.drawImage(image, x, y, 20, 20);
} catch (error) {
console.error("Error loading image:", error?.message);
}
}));
console.log("Images loaded successfully");
await project.setThumbnail(canvas.toBuffer("image/png"))
} catch (error) {
console.error("An error occurred:", error);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment