python-sponsorblock/python_sponsorblock/__init__.py

35 lines
958 B
Python
Raw Normal View History

2024-04-24 08:58:48 +00:00
import requests
from urllib.parse import urlparse, urlunparse, parse_qs
import re
2024-04-24 08:58:48 +00:00
from .exceptions import SponsorBlockError, SponsorBlockIdNotFoundError
def _get_video_id(i: str, silent: bool = False) -> None:
# check with regex if i is already an id like r1Fa1iWJVEA
if re.match(r"^[a-zA-Z0-9_-]{11}$", i):
return i.strip()
url = urlparse(url=i)
if url.netloc == "youtu.be":
return url.path[1:]
type_frag_list = url.path.split("/")
query_stuff = parse_qs(url.query)
if "v" not in query_stuff:
if not silent:
raise SponsorBlockIdNotFoundError("No video id found in the url")
return None
else:
return query_stuff["v"][0]
2024-04-24 08:58:48 +00:00
class SponsorBlock:
def __init__(self, session: requests.Session = None, base_url: str = "https://sponsor.ajay.app"):
self.base_url: str = base_url
self.session: requests.Session = session or requests.Session()