Skip to content

Reference for youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py

youtube_dl_scraper.site_scrapers.video_scrapers.y2save.Y2Save

Y2Save(download_path: str)

Bases: BaseScraper

Source code in youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py
def __init__(self, download_path: str):
    self.session = requests.Session()
    self.session.headers.update(self.headers)
convert
convert(vid: str, key: str) -> str

Convert video or audio using the provided vid and key.

Source code in youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py
def convert(self, vid: str, key: str) -> str:
    """Convert video or audio using the provided vid and key."""
    csrf_token = self.get_csrf_token()
    payload = f"_token={csrf_token}&vid={vid}&key={key}"

    response = self.session.post(
        f"https://{self.__host__}/searchConvert", data=payload
    )

    if response.status_code != 200:
        raise YouTubeDLScraperError(
            f"Error occurred during conversion: invalid response code: {response.status_code}"
        )

    data = response.json()
    if data.get("status") != "ok":
        raise ScraperExecutionError("Conversion failed")

    return data["dlink"]
get_csrf_token
get_csrf_token()

Fetch CSRF token and cookies from the site.

Source code in youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py
def get_csrf_token(self):
    """Fetch CSRF token and cookies from the site."""
    url = f"https://{self.__host__}/id"
    self.session.headers.update({"User-Agent": self.ua_generator.random})
    response = self.session.get(url)

    if response.status_code != 200:
        raise YouTubeDLScraperError(
            f"Error fetching CSRF token: invalid response code: {response.status_code}"
        )

    csrf_token = response.text.split('name="csrf-token" content="')[1].split('"')[0]
    return csrf_token
parse_video_data
parse_video_data(data: dict, url: str) -> dict

Parse video data into a structured format.

Source code in youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py
def parse_video_data(self, data: dict, url: str) -> dict:
    """Parse video data into a structured format."""
    video_data = {
        "id": data["vid"],
        "title": data["title"],
        "watch_url": url,
        "thumbnail": data["thumbnail"],
        "duration": data["vduration"],
    }

    # Parse stream data
    streams = {"video": [], "audio": []}

    # Parse video streams
    for stream in data["convert_links"].get("video", []):
        quality = stream["quality"]
        if quality != "auto":
            cleaned_quality = re.sub("p|P", "", quality)
            quality = int(cleaned_quality or 0) or None
        else:
            quality = -1
        streams["video"].append(
            {
                "quality": quality,
                "label": stream["quality"].lower(),
                "key": stream["key"],
                "args": [data["vid"], stream["key"]],
                "get_url": (lambda vid, key: self.convert(vid, key)),
            }
        )

    # Parse audio streams
    for stream in data["convert_links"].get("audio", []):
        streams["audio"].append(
            {
                "quality": int(stream.get("quality", None).replace("kbps", ""))
                or 0,
                "label": stream["quality"].lower(),
                "key": stream["key"],
                "args": [data["vid"], stream["key"]],
                "get_url": (lambda vid, key: self.convert(vid, key)),
            }
        )

    video_data["streams"] = streams
    return video_data
scrape
scrape(url: str) -> dict

Scrape video information.

Source code in youtube_dl_scraper/site_scrapers/video_scrapers/y2save.py
def scrape(self, url: str) -> dict:
    """Scrape video information."""
    csrf_token = self.get_csrf_token()
    payload = f"_token={csrf_token}&query={url}"
    response = self.session.post(f"https://{self.__host__}/search", data=payload)

    if response.status_code != 200:
        raise YouTubeDLScraperError(
            f"Error occurred fetching video data: invalid response code: {response.status_code}"
        )

    data = response.json()
    if data.get("status") != "ok":
        raise VideoNotFoundError("No data found for the requested video")

    return self.parse_video_data(data["data"], url)