Skip to content

Commit 5f89744

Browse files
committed
youtube link to scratch forum link
1 parent bb65b58 commit 5f89744

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

scratchattach/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from .site.backpack_asset import BackpackAsset
1616
from .site.comment import Comment
1717
from .site.cloud_activity import CloudActivity
18-
from .site.forum import ForumPost, ForumTopic, get_topic, get_topic_list
18+
from .site.forum import ForumPost, ForumTopic, get_topic, get_topic_list, youtube_link_to_scratch
1919
from .site.project import Project, get_project, search_projects, explore_projects
2020
from .site.session import Session, login, login_by_id, login_by_session_string
2121
from .site.studio import Studio, get_studio, search_studios, explore_studios

scratchattach/site/forum.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
from ._base import BaseSiteComponent
77
import xml.etree.ElementTree as ET
88
from bs4 import BeautifulSoup
9+
from urllib.parse import urlparse, parse_qs
910

1011
from ..utils.requests import Requests as requests
1112

13+
1214
class ForumTopic(BaseSiteComponent):
1315
'''
1416
Represents a Scratch forum topic.
@@ -383,3 +385,18 @@ def get_topic_list(category_id, *, page=1):
383385
except Exception as e:
384386
raise exceptions.ScrapeError(str(e))
385387

388+
389+
def youtube_link_to_scratch(link: str):
390+
"""
391+
Converts a YouTube url (in multiple formats) like https://youtu.be/1JTgg4WVAX8?si=fIEskaEaOIRZyTAz
392+
to a link like https://scratch.mit.edu/discuss/youtube/1JTgg4WVAX8
393+
"""
394+
url_parse = urlparse(link)
395+
query_parse = parse_qs(url_parse.query)
396+
397+
if 'v' in query_parse:
398+
video_id = query_parse['v'][0]
399+
else:
400+
video_id = url_parse.path.split('/')[-1]
401+
402+
return f"https://scratch.mit.edu/discuss/youtube/{video_id}"

scratchattach/utils/requests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Requests:
99
"""
1010

1111
@staticmethod
12-
def check_response(r : requests.Response):
12+
def check_response(r: requests.Response):
1313
if r.status_code == 403 or r.status_code == 401:
1414
raise exceptions.Unauthorized
1515
if r.status_code == 500:

0 commit comments

Comments
 (0)