2021-09-11 14:13:21 +12:00
|
|
|
#!/usr/bin/env python3
|
2022-12-20 12:32:37 +13:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2021-09-11 14:13:21 +12:00
|
|
|
import itertools
|
|
|
|
import logging
|
|
|
|
import re
|
|
|
|
from typing import Optional
|
|
|
|
|
|
|
|
import bs4
|
|
|
|
import requests
|
|
|
|
from praw.models import Submission
|
|
|
|
|
|
|
|
from bdfr.exceptions import SiteDownloaderError
|
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Vidble(BaseDownloader):
|
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
|
|
|
|
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-09-14 15:47:46 +12:00
|
|
|
try:
|
|
|
|
res = self.get_links(self.post.url)
|
|
|
|
except AttributeError:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError(f"Could not read page at {self.post.url}")
|
2021-09-11 14:13:21 +12:00
|
|
|
if not res:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError(rf"No resources found at {self.post.url}")
|
2021-09-11 14:13:21 +12:00
|
|
|
res = [Resource(self.post, r, Resource.retry_download(r)) for r in res]
|
|
|
|
return res
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def get_links(url: str) -> set[str]:
|
2022-12-03 18:11:17 +13:00
|
|
|
if not re.search(r"vidble.com/(show/|album/|watch\?v)", url):
|
|
|
|
url = re.sub(r"/(\w*?)$", r"/show/\1", url)
|
2021-09-14 15:47:46 +12:00
|
|
|
|
2023-02-05 07:24:38 +13:00
|
|
|
page = requests.get(url, timeout=10)
|
2022-12-03 18:11:17 +13:00
|
|
|
soup = bs4.BeautifulSoup(page.text, "html.parser")
|
|
|
|
content_div = soup.find("div", attrs={"id": "ContentPlaceHolder1_divContent"})
|
|
|
|
images = content_div.find_all("img")
|
|
|
|
images = [i.get("src") for i in images]
|
|
|
|
videos = content_div.find_all("source", attrs={"type": "video/mp4"})
|
|
|
|
videos = [v.get("src") for v in videos]
|
2021-09-11 14:13:21 +12:00
|
|
|
resources = filter(None, itertools.chain(images, videos))
|
2022-12-03 18:11:17 +13:00
|
|
|
resources = ["https://www.vidble.com" + r for r in resources]
|
2021-09-11 14:13:21 +12:00
|
|
|
resources = [Vidble.change_med_url(r) for r in resources]
|
|
|
|
return set(resources)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def change_med_url(url: str) -> str:
|
2022-12-03 18:11:17 +13:00
|
|
|
out = re.sub(r"_med(\..{3,4})$", r"\1", url)
|
2021-09-11 14:13:21 +12:00
|
|
|
return out
|