2021-02-07 17:46:20 +13:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
2021-02-07 01:29:13 +13:00
|
|
|
import pathlib
|
2021-02-07 17:46:20 +13:00
|
|
|
import re
|
2021-02-06 21:35:50 +13:00
|
|
|
import urllib.error
|
2020-05-29 06:42:11 +12:00
|
|
|
import urllib.request
|
|
|
|
from html.parser import HTMLParser
|
|
|
|
|
2021-02-07 20:08:24 +13:00
|
|
|
from bulkredditdownloader.site_downloaders.base_downloader import BaseDownloader
|
2021-02-07 14:05:18 +13:00
|
|
|
from bulkredditdownloader.errors import AlbumNotDownloadedCompletely, FileAlreadyExistsError, NotADownloadableLinkError
|
|
|
|
from bulkredditdownloader.utils import GLOBAL
|
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-07 01:29:13 +13:00
|
|
|
def __init__(self, directory: pathlib.Path, post: dict):
|
2021-02-07 14:33:19 +13:00
|
|
|
super().__init__(directory, post)
|
2021-02-07 17:46:20 +13:00
|
|
|
self.download()
|
|
|
|
|
|
|
|
def download(self):
|
2020-05-29 06:42:11 +12:00
|
|
|
try:
|
2021-02-07 17:46:20 +13:00
|
|
|
images = self._get_links(self.post['CONTENTURL'])
|
2020-05-29 06:42:11 +12:00
|
|
|
except urllib.error.HTTPError:
|
|
|
|
raise NotADownloadableLinkError("Not a downloadable link")
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
images_length = len(images)
|
2021-02-07 17:46:20 +13:00
|
|
|
how_many_downloaded = len(images)
|
2020-05-29 06:42:11 +12:00
|
|
|
duplicates = 0
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if images_length == 1:
|
2020-05-29 06:42:11 +12:00
|
|
|
"""Filenames are declared here"""
|
2021-02-07 17:46:20 +13:00
|
|
|
filename = GLOBAL.config['filename'].format(**self.post) + self.post["EXTENSION"]
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
image = images[0]
|
|
|
|
if not re.match(r'https?://.*', image):
|
|
|
|
image = "https://" + image
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
self._download_resource(filename, self.directory, image)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
else:
|
2021-02-07 17:46:20 +13:00
|
|
|
filename = GLOBAL.config['filename'].format(**self.post)
|
|
|
|
logger.info(filename)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
folder_dir = self.directory / filename
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
folder_dir.mkdir(exist_ok=True)
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
for i, image in enumerate(images):
|
|
|
|
extension = self._get_extension(image)
|
2021-02-25 22:32:06 +13:00
|
|
|
filename = str(i + 1) + extension
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
if not re.match(r'https?://.*', image):
|
|
|
|
image = "https://" + image
|
|
|
|
|
|
|
|
logger.info(" ({}/{})".format(i + 1, images_length))
|
|
|
|
logger.info(" {}".format(filename))
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
try:
|
2021-02-07 17:46:20 +13:00
|
|
|
self._download_resource(pathlib.Path(filename), folder_dir, image, indent=2)
|
2020-05-29 06:42:11 +12:00
|
|
|
except FileAlreadyExistsError:
|
2021-02-07 17:46:20 +13:00
|
|
|
logger.info(" The file already exists" + " " * 10, end="\n\n")
|
2020-05-29 06:42:11 +12:00
|
|
|
duplicates += 1
|
2021-02-06 21:35:50 +13:00
|
|
|
how_many_downloaded -= 1
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
except Exception as exception:
|
|
|
|
# raise exception
|
2021-02-07 17:46:20 +13:00
|
|
|
logger.error("\n Could not get the file")
|
|
|
|
logger.error(
|
2020-05-29 06:42:11 +12:00
|
|
|
" "
|
2021-02-06 21:35:50 +13:00
|
|
|
+ "{class_name}: {info}".format(class_name=exception.__class__.__name__, info=str(exception))
|
2020-05-29 06:42:11 +12:00
|
|
|
+ "\n"
|
|
|
|
)
|
2021-02-06 21:35:50 +13:00
|
|
|
how_many_downloaded -= 1
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
if duplicates == images_length:
|
2020-05-29 06:42:11 +12:00
|
|
|
raise FileAlreadyExistsError
|
2021-02-06 21:35:50 +13:00
|
|
|
elif how_many_downloaded + duplicates < images_length:
|
|
|
|
raise AlbumNotDownloadedCompletely("Album Not Downloaded Completely")
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
@staticmethod
|
|
|
|
def _get_links(url: str) -> list[str]:
|
2020-05-29 06:42:11 +12:00
|
|
|
content = []
|
2021-02-06 21:35:50 +13:00
|
|
|
line_number = None
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-07 17:46:20 +13:00
|
|
|
# TODO: move to bs4 and requests
|
2020-05-29 06:42:11 +12:00
|
|
|
class EromeParser(HTMLParser):
|
|
|
|
tag = None
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
def handle_starttag(self, tag, attrs):
|
2021-02-25 22:32:06 +13:00
|
|
|
self.tag = {tag: {attr[0]: attr[1] for attr in attrs}}
|
2020-05-29 06:42:11 +12:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
page_source = (urllib.request.urlopen(url).read().decode().split('\n'))
|
2020-05-29 06:42:11 +12:00
|
|
|
|
|
|
|
""" FIND WHERE ALBUM STARTS IN ORDER NOT TO GET WRONG LINKS"""
|
2021-02-06 21:35:50 +13:00
|
|
|
for i in range(len(page_source)):
|
2020-05-29 06:42:11 +12:00
|
|
|
obj = EromeParser()
|
2021-02-06 21:35:50 +13:00
|
|
|
obj.feed(page_source[i])
|
2020-05-29 06:42:11 +12:00
|
|
|
tag = obj.tag
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2020-05-29 06:42:11 +12:00
|
|
|
if tag is not None:
|
|
|
|
if "div" in tag:
|
|
|
|
if "id" in tag["div"]:
|
|
|
|
if tag["div"]["id"] == "album":
|
2021-02-06 21:35:50 +13:00
|
|
|
line_number = i
|
2020-05-29 06:42:11 +12:00
|
|
|
break
|
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
for line in page_source[line_number:]:
|
2020-05-29 06:42:11 +12:00
|
|
|
obj = EromeParser()
|
|
|
|
obj.feed(line)
|
|
|
|
tag = obj.tag
|
|
|
|
if tag is not None:
|
|
|
|
if "img" in tag:
|
|
|
|
if "class" in tag["img"]:
|
2021-02-25 22:32:06 +13:00
|
|
|
if tag["img"]["class"] == "img-front":
|
2020-05-29 06:42:11 +12:00
|
|
|
content.append(tag["img"]["src"])
|
|
|
|
elif "source" in tag:
|
|
|
|
content.append(tag["source"]["src"])
|
2021-02-25 22:32:06 +13:00
|
|
|
|
2021-02-06 21:35:50 +13:00
|
|
|
return [link for link in content if link.endswith("_480p.mp4") or not link.endswith(".mp4")]
|