Skip to content

[Bug]: user.loves breaks on projects with empty title #581

@faretek1

Description

@faretek1

scratchattach version

bleeding edge (git)

What happened?

when a user has loved a project like this it causes an indexerror on this line:

title = second_anchor.contents[0]

fix: change to title = second_anchor.text

Your code.

import requests
import scratchattach as sa
import bs4
import sys


def session_studios_following(self: sa.Session, page: int = 0):
    text = requests.get(
        f"https://scratch.mit.edu/users/{self.username}/studios_following/",
        headers=self._headers,
    ).text
    soup = bs4.BeautifulSoup(text, "html.parser")
    grid = soup.select_one(".media-grid")
    assert grid is not None
    for studio_elem in grid.select("li.gallery.thumb.item"):
        title_span = studio_elem.select_one("span.title")
        assert title_span is not None
        anchor = title_span.find("a")
        assert anchor is not None
        href = str(anchor["href"])
        sid = int(href.split("/")[-2])
        title = anchor.text
        print(sid, title)


def main():
    assert len(sys.argv) > 2
    username = sys.argv[-2]
    password = sys.argv[-1]
    print(len(password))

    print("Hello from sacd!")
    sess = sa.login(username, password)
    print(sess)

    with open("template.md") as f:
        template = f.read()

    myself = sess.connect_linked_user()
    myself.update()

    following = ""
    for off in range(0, myself.following_count(), 40):
        for following_user in myself.following(offset=off):
            following += f"\n- {following_user.name}"

    followers = ""
    for off in range(0, myself.follower_count(), 40):
        for follower in myself.followers(offset=off):
            followers += f"\n- {follower.name}"

    loved = ""
    for off in range(0, myself.loves_count(), 40):
        for proj in myself.loves(offset=off):
            loved += f"\n- {proj.url}"

    favorite = ""
    for off in range(0, myself.favorites_count(), 40):
        for proj in myself.favorites(offset=off):
            favorite += f"\n- {proj.url}"

    curated = ""
    for off in range(0, myself.studio_count(), 40):
        for studio in myself.studios(offset=off):
            curated += f"\n- {studio.url}"

    studios_following = ""
    for off in range(0, myself.studios_following_count(), 40):
        ...
    session_studios_following(sess)

    with open("out.md", "w") as f:
        f.write(
            template.format(
                name=sess.username,
                id=myself.id,
                country=myself.country,
                dt=myself.join_date,
                type=myself.rank().name,
                about=myself.about_me,
                wiwo=myself.wiwo,
                following=following,
                followers=followers,
                loved=loved,
                favorite=favorite,
                curated=curated,
                studios_following="",
                comments="",
                projects="",
            )
        )


if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions