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

46 lines
1.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import logging
import re
2021-02-25 23:40:08 +13:00
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:
raise SiteDownloaderError('Erome parser could not find any links')
2021-03-20 01:49:25 +13:00
out = []
for link in links:
if not re.match(r'https?://.*', link):
link = 'https://' + link
out.append(Resource(self.post, link))
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)
2021-03-21 21:58:32 +13:00
soup = bs4.BeautifulSoup(page.text, 'html.parser')
2021-03-18 22:10:27 +13:00
front_images = soup.find_all('img', attrs={'class': 'lasyload'})
out = [im.get('data-src') for im in front_images]
2021-03-17 19:23:00 +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)