Skip to content

Instantly share code, notes, and snippets.

@ninjamar
Last active December 5, 2021 00:12
Show Gist options
  • Select an option

  • Save ninjamar/bf76adcca78e66153068a155766c4a97 to your computer and use it in GitHub Desktop.

Select an option

Save ninjamar/bf76adcca78e66153068a155766c4a97 to your computer and use it in GitHub Desktop.
Scratch User Status
import re
import requests
user = 'ninjamar'
r = requests.get(f'https://scratch.mit.edu/users/{user}')
c = ''.join([i.decode().strip() for i in r.iter_lines()])
iscratcher = re.compile("<span class=\"group\">Scratcher</span>")
isscratchteam = re.compile("<span class=\"group\">Scratch Team</span>")
isnewscratcher = re.compile("<span class=\"group\">New Scratcher</span>")
if iscratcher.search(c):
print(f'{user} is a scratcher')
elif isscratchteam.search(c):
print(f'{user} is on the scratch team')
elif isnewscratcher.search(c):
print(f'{user} is a new scratcher')
@imdevarsh

Copy link
Copy Markdown

wow thanks!

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

wow thanks!

your welcome

@imdevarsh

Copy link
Copy Markdown

hey, I have one question:

c = ''.join([i.decode().strip() for i in r.iter_lines()])

how would this be in JS?

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

in nodejs or browser js?

@imdevarsh

Copy link
Copy Markdown

nodejs 14

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

@imdevarsh

imdevarsh commented Dec 4, 2021

Copy link
Copy Markdown
import fetch from "node-fetch"
console.log("starting");
(async () => {
	const user = "god286";
	const r = await fetch(`https://scratch.mit.edu/users/${user}`);
  
	const c = (await r.text()).trim();
	const iscratcher = new RegExp('<span class="group">Scratcher</span>');
	const isscratchteam = new RegExp('<span class="group">Scratch Team</span>');
	const isnewscratcher = new RegExp('<span class="group">New Scratcher</span>');

	if (iscratcher.test(c)) {
		console.log(`${user} is a scratcher`);
	} else if (isscratchteam.test(c)) {
		console.log(`${user} is on the scratch team`);
	} else if (isnewscratcher.test(c)) {
		console.log(`${user} is on the scratch team`);
	} else {
    console.log(iscratcher)
    console.error(iscratcher.test(c), isnewscratcher.test(c), isscratchteam.test(c))
  }
})();

doesn't seem to work.. why?

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

you need to iterate over EACH line and trim each line rather than the whole text. then turn this array into 1 string

@imdevarsh

Copy link
Copy Markdown

I'm going to do that later

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

ok

@imdevarsh

imdevarsh commented Dec 4, 2021

Copy link
Copy Markdown

Doesn't work 😭

import fetch from "node-fetch";
console.log("starting");
(async () => {
	const user = "god286";
	const r = await fetch(`https://scratch.mit.edu/users/${user}`, {
		headers: {
			"User-Agent": "Mozilla 5.0",
		},
	});
	const c = await r.text();
	let split = c.split("/n");
	for (let index = 0; index < split.length; index++) {
		split[index] = split[index].trim();
	}
	const iscratcher = new RegExp('<span class="group">Scratcher</span>');
	const isscratchteam = new RegExp('<span class="group">Scratch Team</span>');
	const isnewscratcher = new RegExp('<span class="group">New Scratcher</span>');
	iscratcher.test(split);
	if (user.search(iscratcher)) {
		console.log(`${user} is a scratcher`);
	} else if (user.search(isscratchteam)) {
		console.log(`${user} is on the scratch team`);
	} else if (user.search(isnewscratcher)) {
		console.log(`${user} is a new scratcher`);
	} else {
		console.error(
			iscratcher.test(split),
			isnewscratcher.test(split),
			isscratchteam.test(split)
		);
	}
})();

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

yay. please credit me with a link to this gist

@imdevarsh

Copy link
Copy Markdown

argh... as soon as I thought I ported it to JS it decides to call everyone a scratcher

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

why are you doing user.search? i think this evaluated to if undefined. you need to use split.search/test instead.

@CST1229

CST1229 commented Dec 4, 2021

Copy link
Copy Markdown
elif isnewscratcher.search(c):
   print(f'{user} is on the scratch team')```

what

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

what

lol ops

@imdevarsh

Copy link
Copy Markdown

TypeError: split.test is not a function
TypeError: split.search is not a function

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

isscratcher.search(split)

@imdevarsh

imdevarsh commented Dec 4, 2021

Copy link
Copy Markdown

TypeError: isScratcher.search is not a function
don't worry I've switched to JSDom, thanks for your help though!! python seems quite cool for stuff!

@ninjamar

ninjamar commented Dec 4, 2021

Copy link
Copy Markdown
Author

ok. i think it might be isscratcher.test

@imdevarsh

Copy link
Copy Markdown
import fetch from "node-fetch";
console.log("starting");
(async () => {
	const user = "god286";
	const r = await fetch(`https://scratch.mit.edu/users/${user}`, {
		headers: {
			"User-Agent": "Mozilla 5.0",
		},
	});
  console.log(r.ok)
	const c = await r.text();
	let split = c.split("/n");
	for (let index = 0; index < split.length; index++) {
		split[index] = split[index].trim();
	}
	const isScratcher = new RegExp('<span class="group">Scratcher</span>');
	const isScratchTeam = new RegExp('<span class="group">Scratch Team</span>');
	const isNew = new RegExp('<span class="group">New Scratcher</span>');
	isScratcher.test(split);
	if (isScratcher.test(split)) {
		console.log(`${user} is a scratcher`);
	} else if (isScratchTeam.test(split)) {
		console.log(`${user} is on the scratch team`);
	} else if (isNew.test(split)) {
		console.log(`${user} is a new scratcher`);
	} else {
		console.log("nothing found")
	}
})();

gives "nothing found"..?

@ninjamar

ninjamar commented Dec 5, 2021

Copy link
Copy Markdown
Author

The code doesn't seem to be replacing all occurrences of \n,\t,' ' with ``

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment