#! /usr/bin/env nix-shell
#! nix-shell -i python3 -p python3 python3Packages.requests python3Packages.beautifulsoup4 python3Packages.yt-dlp

import requests
import yt_dlp
from bs4 import BeautifulSoup


def fetch_page(url):
    response = requests.get(url)
    if response.status_code != 200:
        print(f"Failed to retrieve the page. Status code: {response.status_code}")
        quit()
    return response.text


def extract_video_links(html):
    soup = BeautifulSoup(html, "html.parser")
    video_links = soup.find_all("a", class_="video-thumb__image-container")
    return map(lambda link: link.get("href"), video_links)


def download_video(link):
    ydl_opts = {"format": "best", "outtmpl": "%(title)s.%(ext)s"}
    yt_dlp.YoutubeDL(ydl_opts).download([link])


if __name__ == "__main__":
    url = "https://xhamster.com/gay/creators/nipple-pump-pig/exclusive"
    html_content = fetch_page(url)

    if not html_content:
        quit()

    for video in extract_video_links(html_content):
        download_video(video)
