add possibility to upload files to Nextcloud webdav

This commit is contained in:
Marc Koch 2022-10-12 17:35:16 +02:00
parent 79c6b0687b
commit 5cbb849aba
3 changed files with 74 additions and 24 deletions

65
main.py
View File

@ -1,9 +1,12 @@
import sys import sys
import os import os
from datetime import datetime, timedelta from datetime import datetime, timedelta
from urllib.parse import urlparse
import validators
import pytz import pytz
import logging import logging
import shutil import shutil
from webdav4.client import Client
import pandas as pd import pandas as pd
from models import TazDownloader, TazConfiguration from models import TazDownloader, TazConfiguration
from exceptions import TazConfigurationError, TazDownloadError, TazDownloadFormatException from exceptions import TazConfigurationError, TazDownloadError, TazDownloadFormatException
@ -16,7 +19,7 @@ def main(config: dict):
# Get german date for tomorrow # Get german date for tomorrow
tomorrow = (datetime.now(pytz.timezone('Europe/Berlin')) + timedelta(1)).strftime('%Y_%m_%d') 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') tmp_folder = os.path.join(dir_path, 'tmp')
# Set log level # Set log level
@ -90,23 +93,53 @@ def main(config: dict):
except Exception as e: except Exception as e:
logging.error(f"Could not update download_history.csv\n {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) newspaper_downloaded_string = "\n ".join(newspaper_downloaded)
if os.path.isdir(config['download_folder']):
download_folder = \ if config['nextcloud_webdav_url']:
config['download_folder'] \ if validators.url(config['nextcloud_webdav_url']):
if config['download_folder'].endswith(os.path.sep) \ url = urlparse(config['nextcloud_webdav_url'])
else config['download_folder'] + os.path.sep webdav_user = url.path.split("/")[-1]
for n in newspaper_downloaded: webdav_password = config['nextcloud_webdav_password']
try: client = Client(f"{url.scheme}://{url.hostname}/public.php/webdav/",
shutil.move(os.path.join(tmp_folder, n), download_folder) auth=(webdav_user, webdav_password))
except Exception as e:
logging.error(f"Could not move {n} to download folder \"{download_folder}\"\n {e}") for n in newspaper_downloaded:
if newspaper_downloaded: try:
logging.info(f"Downloaded\n {newspaper_downloaded_string}\n to {config['download_folder']}") 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: else:
logging.error(f"{config['download_folder']} does not exists.\n {newspaper_downloaded_string}" # If neither a webdav url nor a download folder was provided, exit the program here
f"\n downloaded to {tmp_folder}") 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__': if __name__ == '__main__':

View File

@ -24,7 +24,9 @@ class TazConfiguration:
('id', True), ('id', True),
('password', True), ('password', True),
('download_format', False), ('download_format', False),
('download_folder', True), ('download_folder', False),
('nextcloud_webdav_url', False),
('nextcloud_webdav_password', False),
('limit_requests', False), ('limit_requests', False),
('log_level', False), ('log_level', False),
] ]
@ -96,6 +98,18 @@ class TazConfiguration:
type=str, type=str,
help='The path to a folder where the e-paper should be stored', 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( argparser.add_argument(
'-l', '-l',
'--limit-requests', '--limit-requests',
@ -151,7 +165,7 @@ class TazDownloader:
if not os.path.isdir(download_folder): if not os.path.isdir(download_folder):
os.makedirs(download_folder) os.makedirs(download_folder)
except Exception as e: 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 # download taz
try: try:
@ -170,7 +184,7 @@ class TazDownloader:
with open(os.path.join(download_folder, taz), "wb") as f: with open(os.path.join(download_folder, taz), "wb") as f:
for chunk in r.iter_content(chunk_size=8192): for chunk in r.iter_content(chunk_size=8192):
f.write(chunk) 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. # So we have to check if the response is a pdf file or the html page with an error message.
try: try:
if filetype.guess(os.path.join(download_folder, taz)).mime != 'application/pdf': if filetype.guess(os.path.join(download_folder, taz)).mime != 'application/pdf':

View File

@ -1,5 +1,8 @@
pandas~=1.1.5 pandas~=1.4.3
envyaml~=1.8.210417 envyaml==1.10.211231
requests~=2.26.0 requests==2.28.1
beautifulsoup4~=4.9.3 beautifulsoup4==4.11.1
filetype==1.0.7 filetype==1.1.0
pytz~=2022.1
validators~=0.20.0
webdav4~=0.9.7