2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
2022-12-20 12:32:37 +13:00
|
|
|
# -*- coding: utf-8 -*-
|
2021-02-07 17:46:20 +13:00
|
|
|
|
|
|
|
import logging
|
|
|
|
import re
|
2021-10-02 15:52:12 +13:00
|
|
|
from typing import Callable, Optional
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-17 19:23:00 +13:00
|
|
|
import bs4
|
2021-02-11 12:10:40 +13:00
|
|
|
from praw.models import Submission
|
|
|
|
|
2021-04-12 19:58:32 +12:00
|
|
|
from bdfr.exceptions import SiteDownloaderError
|
|
|
|
from bdfr.resource import Resource
|
|
|
|
from bdfr.site_authenticator import SiteAuthenticator
|
|
|
|
from bdfr.site_downloaders.base_downloader import BaseDownloader
|
2021-02-07 17:46:20 +13:00
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-07 14:33:19 +13:00
|
|
|
class Erome(BaseDownloader):
|
2021-02-15 18:12:27 +13:00
|
|
|
def __init__(self, post: Submission):
|
|
|
|
super().__init__(post)
|
2021-02-07 17:46:20 +13:00
|
|
|
|
2021-02-26 21:57:05 +13:00
|
|
|
def find_resources(self, authenticator: Optional[SiteAuthenticator] = None) -> list[Resource]:
|
2021-03-18 22:10:27 +13:00
|
|
|
links = self._get_links(self.post.url)
|
2021-04-06 13:04:08 +12:00
|
|
|
|
2021-03-18 22:10:27 +13:00
|
|
|
if not links:
|
2022-12-03 18:11:17 +13:00
|
|
|
raise SiteDownloaderError("Erome parser could not find any links")
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-03-20 01:49:25 +13:00
|
|
|
out = []
|
|
|
|
for link in links:
|
2022-12-03 18:11:17 +13:00
|
|
|
if not re.match(r"https?://.*", link):
|
|
|
|
link = "https://" + link
|
2021-10-02 15:52:12 +13:00
|
|
|
out.append(Resource(self.post, link, self.erome_download(link)))
|
2021-03-20 01:49:25 +13:00
|
|
|
return out
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-03-17 19:23:00 +13:00
|
|
|
@staticmethod
|
|
|
|
def _get_links(url: str) -> set[str]:
|
2021-04-06 12:48:21 +12:00
|
|
|
page = Erome.retrieve_url(url)
|
2022-12-03 18:11:17 +13:00
|
|
|
soup = bs4.BeautifulSoup(page.text, "html.parser")
|
|
|
|
front_images = soup.find_all("img", attrs={"class": "lasyload"})
|
|
|
|
out = [im.get("data-src") for im in front_images]
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2022-12-03 18:11:17 +13:00
|
|
|
videos = soup.find_all("source")
|
|
|
|
out.extend([vid.get("src") for vid in videos])
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-03-17 19:23:00 +13:00
|
|
|
return set(out)
|
2021-10-02 15:52:12 +13:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def erome_download(url: str) -> Callable:
|
|
|
|
download_parameters = {
|
2022-12-03 18:11:17 +13:00
|
|
|
"headers": {
|
|
|
|
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)"
|
|
|
|
" Chrome/88.0.4324.104 Safari/537.36",
|
|
|
|
"Referer": "https://www.erome.com/",
|
2021-10-02 15:52:12 +13:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return lambda global_params: Resource.http_download(url, global_params | download_parameters)
|