Skip to content

Commit

Permalink
dev(narugo): add image bam
Browse files Browse the repository at this point in the history
  • Loading branch information
narugo1992 committed Sep 6, 2024
1 parent fab72e6 commit a67194c
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 0 deletions.
1 change: 1 addition & 0 deletions netdriveurls/drives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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, \
Expand Down
3 changes: 3 additions & 0 deletions netdriveurls/drives/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]:
Expand Down
93 changes: 93 additions & 0 deletions netdriveurls/drives/imagebam.py
Original file line number Diff line number Diff line change
@@ -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'))

0 comments on commit a67194c

Please sign in to comment.