Skip to content

Bulk asynchronous downloader for multiple file hosts

Change main.py to be more automated. Sanitize title, download to settings specified directory.

Jules097fc5353a00
diff --git a/main.py b/main.py
index daaddb6a3..3260d44d9 100644
--- a/main.py
+++ b/main.py
@@ -3,41 +3,53 @@ import argparse
import asyncio
from pathlib import Path
from urllib.parse import urlparse
-from utils.downloaders import get_downloaders
-from utils.scrapers import get_scrapper
+from utilities.downloader import get_downloaders
+from utilities.geturls import get_scrapper
+import settings
-logging.basicConfig(
- level=logging.DEBUG,
- filename='logs.log',
- format='%(asctime)s:%(levelname)s:%(module)s:%(filename)s:%(lineno)d:%(message)s'
-)
+from colorama import Fore, Style
+import requests
+import os
+import multiprocessing
+from pathvalidate import sanitize_filename
+
+logging.basicConfig(level=logging.DEBUG, filename='logs.log',
+ format='%(asctime)s:%(levelname)s:%(module)s:%(filename)s:%(lineno)d:%(message)s')
SUPPORTED_URLS = {'cyberdrop.me', 'bunkr.is', 'pixl.is', 'putme.ga'}
+
FILE_FORMATS = {
+
'Images': {
- '.jpg', '.jpeg', '.png', '.gif',
- '.gif', '.webp', '.jpe', '.svg',
- '.tif', '.tiff', '.jif',
+ '.jpg', '.jpeg', '.png', '.gif', '.gif', '.webp', '.jpe', '.svg', '.tif', '.tiff', '.jif',
},
+
'Videos': {
- '.mpeg', '.avchd', '.webm', '.mpv',
- '.swf', '.avi', '.m4p', '.wmv',
- '.mp2', '.m4v', '.qt', '.mpe',
- '.mp4', '.flv', '.mov', '.mpg',
- '.ogg'
+ '.mpeg', '.avchd', '.webm', '.mpv', '.swf', '.avi', '.m4p', '.wmv',
+ '.mp2', '.m4v', '.qt', '.mpe', '.mp4', '.flv', '.mov', '.mpg', '.ogg'
}
}
+cpu_count = settings.threads if settings.threads != 0 else multiprocessing.cpu_count()
+download_folder = settings.download_folder
+timeout = settings.timeout
-def classify_media_files(path: Path) -> None:
- """Sort out files and videos to their own directories.
- Args:
- path (Path): Path containing both images and files to be classified.
- """
+def log(text, style):
+ # Log function for printing to command line
+ print(style + str(text) + Style.RESET_ALL)
+
+
+def clear():
+ # Clears command window
+ os.system('cls' if os.name == 'nt' else 'clear')
+
+
+def classify_media_files(path: Path) -> None:
images = [filename for filename in path.iterdir() if filename.suffix in FILE_FORMATS['Images']]
+
videos = [filename for filename in path.iterdir() if filename.suffix in FILE_FORMATS['Videos']]
if not images or not videos:
@@ -49,65 +61,51 @@ def classify_media_files(path: Path) -> None:
videos_folder.mkdir(exist_ok=True)
# Move the images and videos to appropriate directories
+
for image in images:
image.rename(images_folder / image.name)
for video in videos:
video.rename(videos_folder / video.name)
-def parse_args() -> argparse.Namespace:
- """Parse out the command line arguments."""
- parser = argparse.ArgumentParser(
- description='Simple Script written in Python for download galleries of images / videos',
- epilog="Enjoy!!!"
- )
- parser.add_argument(
- '-u', '--url',
- help=f'The url which contains images or videos. Supported links: {", ".join(SUPPORTED_URLS)}',
- required=False,
- action='store'
- )
- parser.add_argument(
- '-p', '--progress',
- help='Show progress bar from each url. By default its False',
- action='store_true'
- )
- parser.add_argument(
- '-w', '--max-workers',
- help='Define the maximum amount of workers for downloading.',
- default=20,
- type=int
- )
-
- args = parser.parse_args()
- if args.url:
- if not urlparse(args.url).netloc in SUPPORTED_URLS:
- logging.error(f'Unsupported URL:{args.url}')
- raise ValueError('Unsupported URL link!')
- return args
-
-
-async def main() -> None:
- args = parse_args()
- if args.url:
- scrapper = get_scrapper(args.url)
+def version_check() -> None:
+ response = requests.get("https://api.github.com/repos/Jules-WinnfieldX/CyberDropDownloader/releases/latest")
+ latest_version = response.json()["tag_name"]
+ current_version = "1.5.7"
+ if latest_version != current_version:
+ log("A new version of CyberDropDownloader is available\n"
+ "Download it here: https://github.com/Jules-WinnfieldX/CyberDropDownloader/releases/latest\n", Fore.RED)
+ input("To continue anyways press enter")
+ clear()
+
+
+async def main():
+ if os.path.isfile("URLs.txt"):
+ log("URLs.txt exists", Fore.WHITE)
+ else:
+ f = open("URLs.txt", "w+")
+ log("URLs.txt created", Fore.WHITE)
+
+ file_object = open("URLs.txt", "r")
+ urls = [line for line in file_object]
+ for url in urls:
+ scrapper = get_scrapper(url)
+
with scrapper:
- title = scrapper.get_soup().select_one('title').text
+ title = sanitize_filename(scrapper.get_soup().select_one('title').text)
links = scrapper.result_links()
+
if not links:
logging.error(f'ValueError No links: {links}')
raise ValueError('No links found, check the URL.')
downloaders = get_downloaders(links, folder=Path(
- title), max_workers=args.max_workers)
+ title), max_workers=cpu_count)
for downloader in downloaders:
- await downloader.download_content(show_progress=args.progress)
+ await downloader.download_content()
classify_media_files(Path(title))
if __name__ == '__main__':
asyncio.run(main())
-
-
-# TODO: Re-add a way to redownload failed URLs and perhaps log them with the use of logging library