diff --git a/main.py b/main.py index a0011ce..0b219ba 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,12 @@ import sys import os from datetime import datetime, timedelta +from urllib.parse import urlparse +import validators import pytz import logging import shutil +from webdav4.client import Client import pandas as pd from models import TazDownloader, TazConfiguration from exceptions import TazConfigurationError, TazDownloadError, TazDownloadFormatException @@ -16,7 +19,7 @@ def main(config: dict): # Get german date for tomorrow tomorrow = (datetime.now(pytz.timezone('Europe/Berlin')) + timedelta(1)).strftime('%Y_%m_%d') - # Define tmp/ folder + # Define tmp folder tmp_folder = os.path.join(dir_path, 'tmp') # Set log level @@ -90,23 +93,53 @@ def main(config: dict): except Exception as e: logging.error(f"Could not update download_history.csv\n {e}") - # Move downloaded file to download folder newspaper_downloaded_string = "\n ".join(newspaper_downloaded) - if os.path.isdir(config['download_folder']): - download_folder = \ - config['download_folder'] \ - if config['download_folder'].endswith(os.path.sep) \ - else config['download_folder'] + os.path.sep - for n in newspaper_downloaded: - try: - shutil.move(os.path.join(tmp_folder, n), download_folder) - except Exception as e: - logging.error(f"Could not move {n} to download folder \"{download_folder}\"\n {e}") - if newspaper_downloaded: - logging.info(f"Downloaded\n {newspaper_downloaded_string}\n to {config['download_folder']}") + + if config['nextcloud_webdav_url']: + if validators.url(config['nextcloud_webdav_url']): + url = urlparse(config['nextcloud_webdav_url']) + webdav_user = url.path.split("/")[-1] + webdav_password = config['nextcloud_webdav_password'] + client = Client(f"{url.scheme}://{url.hostname}/public.php/webdav/", + auth=(webdav_user, webdav_password)) + + for n in newspaper_downloaded: + try: + client.upload_file(os.path.join(tmp_folder, n), n) + os.remove(os.path.join(tmp_folder, n)) + except Exception as e: + logging.error(f"Could not upload {n} to {url}\n {e}") + if newspaper_downloaded: + logging.info(f"Uploaded\n {newspaper_downloaded_string}\n to {url}") + + + else: + logging.error(f"Invalid url for Nextcloud webdav.") + sys.exit(1) + else: - logging.error(f"{config['download_folder']} does not exists.\n {newspaper_downloaded_string}" - f"\n downloaded to {tmp_folder}") + # If neither a webdav url nor a download folder was provided, exit the program here + if not config['download_folder']: + logging.error(f"Please provide a download folder or a Nextcloud webdav url.\n {newspaper_downloaded_string}" + f"\n downloaded to {tmp_folder}") + sys.exit(1) + + # Move downloaded file to download folder + if os.path.isdir(config['download_folder']): + download_folder = \ + config['download_folder'] \ + if config['download_folder'].endswith(os.path.sep) \ + else config['download_folder'] + os.path.sep + for n in newspaper_downloaded: + try: + shutil.move(os.path.join(tmp_folder, n), download_folder) + except Exception as e: + logging.error(f"Could not move {n} to download folder \"{download_folder}\"\n {e}") + if newspaper_downloaded: + logging.info(f"Downloaded\n {newspaper_downloaded_string}\n to {config['download_folder']}") + else: + logging.error(f"{config['download_folder']} does not exists.\n {newspaper_downloaded_string}" + f"\n downloaded to {tmp_folder}") if __name__ == '__main__': diff --git a/models.py b/models.py index 8e24ef5..4d7a1dc 100644 --- a/models.py +++ b/models.py @@ -24,7 +24,9 @@ class TazConfiguration: ('id', True), ('password', True), ('download_format', False), - ('download_folder', True), + ('download_folder', False), + ('nextcloud_webdav_url', False), + ('nextcloud_webdav_password', False), ('limit_requests', False), ('log_level', False), ] @@ -96,6 +98,18 @@ class TazConfiguration: type=str, help='The path to a folder where the e-paper should be stored', ) + argparser.add_argument( + '--nextcloud_webdav_url', + action='store', + type=str, + help='The url of a Nextcloud webdav', + ) + argparser.add_argument( + '--nextcloud_webdav_password', + action='store', + type=str, + help='The webdav password', + ) argparser.add_argument( '-l', '--limit-requests', @@ -151,7 +165,7 @@ class TazDownloader: if not os.path.isdir(download_folder): os.makedirs(download_folder) except Exception as e: - raise TazDownloadError(f"Could find or create \"{download_folder}\":\n{e}") + raise TazDownloadError(f"Could not find or create \"{download_folder}\":\n{e}") # download taz try: @@ -170,7 +184,7 @@ class TazDownloader: with open(os.path.join(download_folder, taz), "wb") as f: for chunk in r.iter_content(chunk_size=8192): f.write(chunk) - # Unfortunately, the taz website does not respond with an http error code if the credentials are wrong. + # Unfortunately, the taz website does not respond with a http error code if the credentials are wrong. # So we have to check if the response is a pdf file or the html page with an error message. try: if filetype.guess(os.path.join(download_folder, taz)).mime != 'application/pdf': diff --git a/requirements.txt b/requirements.txt index 1fd8122..abfa08a 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,8 @@ -pandas~=1.1.5 -envyaml~=1.8.210417 -requests~=2.26.0 -beautifulsoup4~=4.9.3 -filetype==1.0.7 \ No newline at end of file +pandas~=1.4.3 +envyaml==1.10.211231 +requests==2.28.1 +beautifulsoup4==4.11.1 +filetype==1.1.0 +pytz~=2022.1 +validators~=0.20.0 +webdav4~=0.9.7 \ No newline at end of file