diff --git a/netdriveurls/drives/__init__.py b/netdriveurls/drives/__init__.py index 1dccbba..b1cc3c5 100644 --- a/netdriveurls/drives/__init__.py +++ b/netdriveurls/drives/__init__.py @@ -8,6 +8,7 @@ from .dropbox import DropBoxFolderDownloadSession, DropBoxFileDownloadSession, get_direct_url_for_dropbox from .gofile import GoFileFolderDownloadSession, get_direct_urls_for_gofile_folder from .ibb import IbbFileDownloadSession +from .imagebam import get_direct_url_for_imagebam_image, ImageBamImageDownloadSession, ImageBamViewDownloadSession from .imgbox import ImgBoxGalleryDownloadSession, ImgBoxImageDownloadSession, ImgBoxResourceInvalidError, \ get_file_urls_for_imgbox, get_direct_url_for_imgbox from .jpg5su import JPG5SuFileDownloadSession, get_direct_url_for_jpg5su, JPG5SuAlbumDownloadSession, \ diff --git a/netdriveurls/drives/dispatch.py b/netdriveurls/drives/dispatch.py index 2961cf6..ed810e4 100644 --- a/netdriveurls/drives/dispatch.py +++ b/netdriveurls/drives/dispatch.py @@ -7,6 +7,7 @@ from .dropbox import DropBoxFileDownloadSession, DropBoxFolderDownloadSession from .gofile import GoFileFolderDownloadSession from .ibb import IbbFileDownloadSession +from .imagebam import ImageBamViewDownloadSession, ImageBamImageDownloadSession from .imgbox import ImgBoxImageDownloadSession, ImgBoxGalleryDownloadSession from .jpg5su import JPG5SuFileDownloadSession, JPG5SuAlbumDownloadSession from .mediafire import MediaFireDownloadSession @@ -42,6 +43,8 @@ def register_net_drive(net_drive_cls: Type[NetDriveDownloadSession]): register_net_drive(ImgBoxGalleryDownloadSession) register_net_drive(PixelDrainFileDownloadSession) register_net_drive(PixelDrainListDownloadSession) +register_net_drive(ImageBamImageDownloadSession) +register_net_drive(ImageBamViewDownloadSession) def from_url(url: str) -> Union[NetDriveDownloadSession, SeparableNetDriveDownloadSession]: diff --git a/netdriveurls/drives/imagebam.py b/netdriveurls/drives/imagebam.py new file mode 100644 index 0000000..6f350bf --- /dev/null +++ b/netdriveurls/drives/imagebam.py @@ -0,0 +1,93 @@ +import os.path +from typing import Optional, Tuple +from urllib.parse import urljoin + +import requests +from hbutils.system import urlsplit +from pyquery import PyQuery as pq + +from .base import ResourceInvalidError, StandaloneFileNetDriveDownloadSession +from ..utils import get_requests_session, download_file + + +def _get_session(session: Optional[requests.Session] = None) -> requests.Session: + session = session or get_requests_session() + session.cookies.update({ + 'nsfw_inter': '1', + }) + return session + + +def get_direct_url_for_imagebam_image(url: str, session: Optional[requests.Session] = None) -> Tuple[str, str]: + session = _get_session(session or get_requests_session()) + resp = session.get(url) + resp.raise_for_status() + + page = pq(resp.text) + name = page('.content-name span.name').text().strip() + for aitem in page('.dropdown-menu > a').items(): + if 'download' in aitem.text().lower(): + return name, urljoin(resp.url, aitem.attr('href')) + + raise ResourceInvalidError(f'No image url found for {url!r}.') + + +class ImageBamImageDownloadSession(StandaloneFileNetDriveDownloadSession): + def __init__(self, url: str): + StandaloneFileNetDriveDownloadSession.__init__(self) + self.page_url = url + + def _get_resource_id(self) -> str: + split = urlsplit(self.page_url) + return f'imagebam_image_{split.path_segments[2]}' + + def download_to_directory(self, dst_dir: str): + session = _get_session() + name, url = get_direct_url_for_imagebam_image(self.page_url, session=session) + dst_file = os.path.join(dst_dir, name) + download_file(url, filename=dst_file, session=session) + + @classmethod + def from_url(cls, url: str): + return cls(url) + + @classmethod + def is_valid_url(cls, url: str) -> bool: + split = urlsplit(url) + return tuple(split.host.split('.')[-2:]) == ('imagebam', 'com') and \ + tuple(split.path_segments[1:2]) == ('image',) + + +class ImageBamViewDownloadSession(StandaloneFileNetDriveDownloadSession): + def __init__(self, url: str): + StandaloneFileNetDriveDownloadSession.__init__(self) + self.page_url = url + + def _get_resource_id(self) -> str: + split = urlsplit(self.page_url) + return f'imagebam_view_{split.path_segments[2]}' + + def download_to_directory(self, dst_dir: str): + session = _get_session() + name, url = get_direct_url_for_imagebam_image(self.page_url, session=session) + dst_file = os.path.join(dst_dir, name) + download_file(url, filename=dst_file, session=session) + + @classmethod + def from_url(cls, url: str): + return cls(url) + + @classmethod + def is_valid_url(cls, url: str) -> bool: + split = urlsplit(url) + return tuple(split.host.split('.')[-2:]) == ('imagebam', 'com') and \ + tuple(split.path_segments[1:2]) == ('view',) + + +if __name__ == '__main__': + from ditk import logging + + logging.try_init_root(logging.DEBUG) + # print(get_direct_url_for_imagebam_image('http://www.imagebam.com/image/e5bd421354406108')) + # print(get_direct_url_for_imagebam_image('https://www.imagebam.com/image/0f7e71316357051')) + print(get_direct_url_for_imagebam_image('https://www.imagebam.com/view/MEJ5K96'))