Skip to content

Instantly share code, notes, and snippets.

@Sid72020123
Created May 27, 2023 13:05
Show Gist options
  • Save Sid72020123/952fa5494b1492f1b5797810ee73a2b9 to your computer and use it in GitHub Desktop.
Save Sid72020123/952fa5494b1492f1b5797810ee73a2b9 to your computer and use it in GitHub Desktop.
The Python program that fetched the activity of a Scratch user
from requests import get
from bs4 import BeautifulSoup
from pprint import pprint
Scratch_URL = "https://scratch.mit.edu"
def get_activity_type(raw_activity_type):
activities = {
"added": "studio-add",
"became a curator of": "studio-curator",
"loved": "project-love",
"favorited": "project-favorite",
"is now following": "user-follow",
"is now following the studio": "studio-follow",
"shared the project": "project-share",
"was promoted to manager of": "studio-manager",
"remixed": "project-remix",
"joined Scratch": "scratch-join",
}
return activities[raw_activity_type]
def get_activity(username):
response = get(
f"https://scratch.mit.edu/messages/ajax/user-activity/?user={username}&max=1000000",
headers={"User-Agent": "Python Requests"},
).content
soup = BeautifulSoup(response, "html.parser")
activities = soup.find_all("li")
result = []
for activity in activities:
div_contents = activity.find("div").contents
a = {}
activity_type = get_activity_type(div_contents[2].strip())
activity_time = str(activity.find("span", "time").contents[0]).replace(
"\xa0", " "
)
a = {"Type": activity_type, "Time": activity_time, "Action": {}}
a_act = a["Action"]
if activity_type == "studio-add":
a_act["ProjectURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["ProjectName"] = f"{str(div_contents[3].contents[0])}"
a_act["StudioURL"] = f"{Scratch_URL}{div_contents[5]['href']}"
a_act["StudioName"] = f"{str(div_contents[5].contents[0])}"
elif activity_type == "studio-curator":
a_act["StudioURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["StudioName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "project-love":
a_act["ProjectURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["ProjectName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "project-favorite":
a_act["ProjectURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["ProjectName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "user-follow":
a_act["UsernameURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["Username"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "studio-follow":
a_act["StudioURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["StudioName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "project-share":
a_act["ProjectURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["ProjectName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "studio-manager":
a_act["StudioURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["StudioName"] = f"{str(div_contents[3].contents[0])}"
elif activity_type == "project-remix":
a_act["ParentProjectURL"] = f"{Scratch_URL}{div_contents[3]['href']}"
a_act["ParentProjectName"] = f"{str(div_contents[3].contents[0])}"
a_act["NewProjectURL"] = f"{Scratch_URL}{div_contents[5]['href']}"
a_act["NewProjectName"] = f"{str(div_contents[5].contents[0])}"
result.append(a)
return result
activity = get_activity("programORdie")
pprint(activity) # Just pretty print the result...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment