Skip to content

Instantly share code, notes, and snippets.

@webdev03
Created October 7, 2023 01:54
Show Gist options
  • Save webdev03/51095befa764298ed136f0c63fefa8f5 to your computer and use it in GitHub Desktop.
Save webdev03/51095befa764298ed136f0c63fefa8f5 to your computer and use it in GitHub Desktop.
Auto Siggy
import "dotenv/config"
import * as meowclient from "meowclient";
import { parse } from "node-html-parser"
import { DateTime } from "luxon";
const session = new meowclient.ScratchSession();
await session.init(process.env['SCRATCH_USERNAME'], process.env['SCRATCH_PASSWORD']);
const myProfile = new meowclient.Profile(session, session.username);
const profileAPI = await myProfile.getUserAPI();
const joinedAt = DateTime.fromISO(profileAPI.history.joined);
const currentDate = DateTime.now();
const diff = currentDate.diff(joinedAt, ['years', 'months', 'days']);
const { years, months, days } = diff.toObject();
let dateSinceJoin = '';
if (years > 0) {
dateSinceJoin += `${Math.floor(years)} ${Math.floor(years) === 1 ? 'year' : 'years'}`;
}
if (months > 0) {
dateSinceJoin += `, ${Math.floor(months)} ${Math.floor(months) === 1 ? 'month' : 'months'}`;
}
if (days > 0) {
dateSinceJoin += `, ${Math.floor(days)} ${Math.floor(days) === 1 ? 'day' : 'days'}`;
}
dateSinceJoin += ' ago';
const followerCount = parse(await ((await fetch(`https://scratch.mit.edu/users/${session.username}/followers/`)).text())).querySelector(".box-head > h2:nth-child(2)").innerText.match(/\((\d+)\)/)[1]
const griffpatchFollowers = parse(await ((await fetch(`https://scratch.mit.edu/users/griffpatch/followers/`)).text())).querySelector(".box-head > h2:nth-child(2)").innerText.match(/\((\d+)\)/)[1]
const scratchStats = await ((await fetch(`https://api.allorigins.win/raw?url=https://scratchstats.com/api2/userstats/${session.username}`, {
"credentials": "omit",
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8",
"Accept-Language": "en-GB,en;q=0.5",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "cross-site",
},
"method": "GET",
"mode": "cors"
})).json());
const followerHistory = scratchStats.scratchDbHistory.sort((a, b) => new Date(b.date) - new Date(a.date));
const timeTakeGriffpatch = DateTime.fromSeconds((Date.now() + (((new Date(followerHistory[0].date).getTime() - new Date(followerHistory[followerHistory.length - 1].date).getTime()) / (followerHistory[0].value - followerHistory[followerHistory.length - 1].value)) * (griffpatchFollowers - followerCount))) / 1000);
import { createCanvas, loadImage } from "canvas";
async function createImage() {
try {
let followerList = (await (await fetch(`https://api.scratch.mit.edu/users/${session.username}/followers?limit=36`)).json())
.map(x => x.profile.images['90x90']);
const canvas = createCanvas(800, 200);
const ctx = canvas.getContext("2d");
await Promise.all(followerList.map(async (followerUrl, i) => {
try {
const image = await loadImage(followerUrl);
const row = Math.floor(i / 12);
const col = i % 12;
const x = col * 60 + 10;
const y = row * 60 + 10;
ctx.drawImage(image, x, y, 55, 55);
} catch (error) {
console.error("Error loading image:", error);
}
}));
return canvas.toBuffer("image/png")
} catch (error) {
console.error("An error occurred:", error);
}
}
const imageURL = (await session.uploadToAssets(await createImage(), "png")).replace("https://assets.scratch.mit.edu/", "https://assets.scratch.mit.edu/get_image/.%2E/")
const siggy = `Here are some of my followers!
[img=${imageURL}][/img]
[b]I joined:[/b] ${dateSinceJoin} (${joinedAt.toLocaleString()})
[b]I have:[/b] ${followerCount.toLocaleString("en")} followers
[b]In total, I have attained:[/b] [b]${Number(scratchStats.totals.loves).toLocaleString("en")}[/b] loves, [b]${Number(scratchStats.totals.favorites).toLocaleString("en")}[/b] favourites, and [b]${Number(scratchStats.totals.views).toLocaleString("en")}[/b] views.
[b]Fun Fact:[/b] If my account continued to gain followers at a similar rate to right now, [b]${timeTakeGriffpatch.toRelativeCalendar()}[/b] I would reach the number of followers griffpatch has [b]today[/b]! Try to imagine how many followers he would have then!
Thank you everyone!
Script created by god286.
[b]Wondering how I made this auto update every day?[/b] I used a script that runs on my Raspberry Pi every few hours. If you would like the code, just let me know!
`
console.log(siggy)
console.log(await new meowclient.Forum(session).setSignature(siggy));
console.log("Set siggy for " + session.username)
await session.logout();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment