1
0
Fork 0
mirror of synced 2024-06-02 18:34:37 +12:00
bulk-downloader-for-reddit/bdfr/site_downloaders/erome.py

59 lines
1.8 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import logging
import re
2023-01-26 16:23:59 +13:00
from collections.abc import Callable
from typing import Optional
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
logger = logging.getLogger(__name__)
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-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-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")
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-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]
2022-12-03 18:11:17 +13:00
videos = soup.find_all("source")
out.extend([vid.get("src") for vid in videos])
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)