diff --git a/cctv-scheduler.conf b/cctv-scheduler.conf index 750db03..d3c813c 100644 --- a/cctv-scheduler.conf +++ b/cctv-scheduler.conf @@ -96,9 +96,8 @@ userpass = pass # FTP, SFTP, SMB. records_root_path = ftp://user:pass@192.168.254.254:21/Records/camera.test.local # -# Separated parameters string has lower priority and parameters are overwritten by -# records_root_path = 'hosttype://username:password@hostname:hostport/some/path' -# if you use both. +# One line parameters string has lower priority and parameters are overwritten by +# separated parameter variables if you use both. # #records_root_path = /Records/camera.test.local # @@ -157,43 +156,41 @@ image_find_names = step071, image-01, image-02 # If image root or destination video directories on a remote host is used, username and password must be specified. # Supported protocols: # FTP, SFTP, SMB. -image_root_path = ftp://user:pass@192.168.254.254/Records/camera.test.local +image_root_path = sftp://user:pass@192.168.254.254/Records/camera.test.local # -# Separated parameters string has lower priority and parameters are overwritten by -# image_root_path = 'hosttype://username:password@hostname:hostport/some/path' -# if you use both. +# One line parameters string has lower priority and parameters are overwritten by +# separated parameter variables if you use both. # #image_root_path = /Records/camera.test.local # #image_root_host = 192.168.254.254 # # Optionality you can set custom connection port: -#image_root_port = 21 +#image_root_port = 22 # # You must set connection type (ftp is faster than sftp, sftp is faster than smb): # ftp, sftp, smb. -#image_root_type = ftp +#image_root_type = sftp # #image_root_user = user # #image_root_pass = pass -video_dest_path = ftp://user:pass@192.168.254.254/Downloads +video_dest_path = smb://user:pass@192.168.254.254/Downloads # -# Separated parameters string has lower priority and parameters are overwritten by -# video_dest_path = 'hosttype://username:password@hostname:hostport/some/path' -# if you use both. +# One line parameters string has lower priority and parameters are overwritten by +# separated parameter variables if you use both. # #video_dest_path = /Downloads # # Optionality you can set custom connection port: #video_dest_host = 192.168.254.254 # -#video_dest_port = 21 +#video_dest_port = 445 # # You must set connection type (ftp is faster than sftp, sftp is faster than smb): # ftp, sftp, smb. -#video_dest_type = ftp +#video_dest_type = smb # #video_dest_user = user # @@ -213,9 +210,8 @@ video_find_names = step071, image-01, image-02 # FTP, SFTP, SMB. video_root_path = ftp://user:pass@192.168.254.254/Downloads # -# Separated parameters string has lower priority and parameters are overwritten by -# video_root_path = 'hosttype://username:password@hostname:hostport/some/path' -# if you use both. +# One line parameters string has lower priority and parameters are overwritten by +# separated parameter variables if you use both. # #video_dest_path = /Downloads # diff --git a/cctv-scheduler.py b/cctv-scheduler.py index c1d452c..0b76382 100644 --- a/cctv-scheduler.py +++ b/cctv-scheduler.py @@ -1342,6 +1342,77 @@ class Connect: + @staticmethod + def parse_connect_params( + connect_string: str, + logger_alias: str = inspect.stack()[0].function + ) -> dict: + """One line connection string separator. + + Args: + connect_string (str): hosttype://username:password@hostname:hostport/some/path. + logger_alias (str, optional): logger_alias (str, optional): sublogger name. + Defaults to function or method name. + + Returns: + dict: { + 'hostpath': remote_hostpath, + 'hostname': remote_hostname, + 'hostport': remote_hostport, + 'hosttype': remote_hosttype, + 'username': remote_username, + 'password': remote_password + } + """ + local_logger = logging.getLogger(logger_alias) + if Do.args_valid(locals(), Connect.parse_connect_params.__annotations__): + remote_hostpath = None + remote_hostname = None + remote_hostport = None + remote_hosttype = None + remote_username = None + remote_password = None + if '://' in connect_string: + remote_hostname = connect_string.split('/')[2] + remote_hosttype = connect_string.split('://')[0] + remote_hostpath = connect_string.replace( + remote_hosttype + '://' + remote_hostname, '' + ) + if '@' in remote_hostname: + remote_username = remote_hostname.split('@')[0].split(':')[0] + remote_password = remote_hostname.split('@')[0].split(':')[1] + remote_hostname = remote_hostname.split('@')[1] + if ':' in remote_hostname: + remote_hostport = int(remote_hostname.split(':')[1]) + remote_hostname = remote_hostname.split(':')[0] + else: + remote_hostpath = connect_string + remote_hosttype = 'local' + if not remote_hostport: + if remote_hosttype == 'ftp': + remote_hostport = 21 + if remote_hosttype == 'sftp': + remote_hostport = 22 + if remote_hosttype == 'smb': + remote_hostport = 445 + + local_logger.debug(msg='' + + '\n' + 'hostpath: ' + str(remote_hostpath) + + '\n' + 'hostname: ' + str(remote_hostname) + + '\n' + 'hostport: ' + str(remote_hostport) + + '\n' + 'hosttype: ' + str(remote_hosttype) + + '\n' + 'username: ' + str(remote_username) + + '\n' + 'password: ' + str(remote_password) + ) + return { + 'hostpath': remote_hostpath, + 'hostname': remote_hostname, + 'hostport': remote_hostport, + 'hosttype': remote_hosttype, + 'username': remote_username, + 'password': remote_password + } + @classmethod def file_search( cls, @@ -1358,9 +1429,9 @@ class Connect: Args: search_path (str): where to search. - search_path='/some/path', hostname='hostname', username='username', etc. - has lower priority and parameters are overwritten by search_path = 'hosttype://username:password@hostname:hostport/some/path' + has lower priority and parameters are overwritten by + search_path='/some/path', hostname='hostname', username='username', etc. search_name (str, type, optional): full or partial filename for the filter. Defaults to None. hostname (str, type, optional): remote hostname. @@ -1390,30 +1461,24 @@ class Connect: + '\n' + 'username: ' + str(username) + '\n' + 'password: ' + str(password) ) - remote_hostpath = search_path + + connect = cls.parse_connect_params(connect_string=search_path) + remote_hostpath = connect['hostpath'] remote_hostname = hostname + if not remote_hostname: + remote_hostname = connect['hostname'] remote_hostport = hostport + if not remote_hostport: + remote_hostport = connect['hostport'] remote_hosttype = hosttype + if not remote_hosttype: + remote_hosttype = connect['hosttype'] remote_username = username + if not remote_username: + remote_username = connect['username'] remote_password = password - if '://' in search_path: - remote_hostname = search_path.split('/')[2] - remote_hosttype = search_path.split('://')[0] - remote_hostpath = search_path.replace(remote_hosttype + '://' + remote_hostname, '') - if '@' in remote_hostname: - remote_username = remote_hostname.split('@')[0].split(':')[0] - remote_password = remote_hostname.split('@')[0].split(':')[1] - remote_hostname = remote_hostname.split('@')[1] - if ':' in remote_hostname: - remote_hostport = int(remote_hostname.split(':')[1]) - remote_hostname = remote_hostname.split(':')[0] - if not hostport: - if remote_hosttype == 'ftp': - remote_hostport = 21 - if remote_hosttype == 'sftp': - remote_hostport = 22 - if remote_hosttype == 'smb': - remote_hostport = 445 + if not remote_password: + remote_password = connect['password'] if remote_hosttype == 'ftp': files_found = cls.ftp_file_search( @@ -1471,9 +1536,9 @@ class Connect: Args: src_file (str): - src_file='/remote/path/to/file.', hostname='hostname', username='username', etc. - has lower priority and parameters are overwritten by src_file = 'hosttype://username:password@hostname:hostport/remote/path/to/file.' + has lower priority and parameters are overwritten by + src_file='/remote/path/to/file', hostname='hostname', username='username', etc. dst_file (str):/local/path/to/file. hostname (str, type, optional): remote hostname. Defaults to None. @@ -1502,30 +1567,24 @@ class Connect: + '\n' + 'username: ' + str(username) + '\n' + 'password: ' + str(password) ) - remote_hostpath = src_file + + connect = cls.parse_connect_params(connect_string=src_file) + remote_hostpath = connect['hostpath'] remote_hostname = hostname + if not remote_hostname: + remote_hostname = connect['hostname'] remote_hostport = hostport + if not remote_hostport: + remote_hostport = connect['hostport'] remote_hosttype = hosttype + if not remote_hosttype: + remote_hosttype = connect['hosttype'] remote_username = username + if not remote_username: + remote_username = connect['username'] remote_password = password - if '://' in src_file: - remote_hostname = src_file.split('/')[2] - remote_hosttype = src_file.split('://')[0] - remote_hostpath = src_file.replace(remote_hosttype + '://' + remote_hostname, '') - if '@' in remote_hostname: - remote_username = remote_hostname.split('@')[0].split(':')[0] - remote_password = remote_hostname.split('@')[0].split(':')[1] - remote_hostname = remote_hostname.split('@')[1] - if ':' in remote_hostname: - remote_hostport = int(remote_hostname.split(':')[1]) - remote_hostname = remote_hostname.split(':')[0] - if not hostport: - if remote_hosttype == 'ftp': - remote_hostport = 21 - if remote_hosttype == 'sftp': - remote_hostport = 22 - if remote_hosttype == 'smb': - remote_hostport = 445 + if not remote_password: + remote_password = connect['password'] if remote_hosttype == 'ftp': result = cls.ftp_get_file( @@ -1584,9 +1643,9 @@ class Connect: Args: src_file (str):/local/path/to/file. dst_file (str): - dst_file='/remote/path/to/file.', hostname='hostname', username='username', etc. - has lower priority and parameters are overwritten by dst_file = 'hosttype://username:password@hostname:hostport/remote/path/to/file.' + has lower priority and parameters are overwritten by + dst_file='/remote/path/to/file', hostname='hostname', username='username', etc. hostname (str, type, optional): remote hostname. Defaults to None. hostport (int, type, optional): remote host connection port. @@ -1614,30 +1673,24 @@ class Connect: + '\n' + 'username: ' + str(username) + '\n' + 'password: ' + str(password) ) - remote_hostpath = dst_file + + connect = cls.parse_connect_params(connect_string=dst_file) + remote_hostpath = connect['hostpath'] remote_hostname = hostname + if not remote_hostname: + remote_hostname = connect['hostname'] remote_hostport = hostport + if not remote_hostport: + remote_hostport = connect['hostport'] remote_hosttype = hosttype + if not remote_hosttype: + remote_hosttype = connect['hosttype'] remote_username = username + if not remote_username: + remote_username = connect['username'] remote_password = password - if '://' in dst_file: - remote_hostname = dst_file.split('/')[2] - remote_hosttype = dst_file.split('://')[0] - remote_hostpath = dst_file.replace(remote_hosttype + '://' + remote_hostname, '') - if '@' in remote_hostname: - remote_username = remote_hostname.split('@')[0].split(':')[0] - remote_password = remote_hostname.split('@')[0].split(':')[1] - remote_hostname = remote_hostname.split('@')[1] - if ':' in remote_hostname: - remote_hostport = int(remote_hostname.split(':')[1]) - remote_hostname = remote_hostname.split(':')[0] - if not hostport: - if remote_hosttype == 'ftp': - remote_hostport = 21 - if remote_hosttype == 'sftp': - remote_hostport = 22 - if remote_hosttype == 'smb': - remote_hostport = 445 + if not remote_password: + remote_password = connect['password'] if remote_hosttype == 'ftp': result = cls.ftp_put_file( @@ -3235,6 +3288,19 @@ class Convert: ) makedirs(temp_path, exist_ok=True) for image in image_found: + + connect = Connect.parse_connect_params(connect_string=image_root) + if not image_root_host: + image_root_host = connect['hostname'] + if not image_root_port: + image_root_port = connect['hostport'] + if not image_root_type: + image_root_type = connect['hosttype'] + if not image_root_user: + image_root_user = connect['username'] + if not image_root_pass: + image_root_pass = connect['password'] + if Connect.file_download( src_file=image, dst_file=temp_path + sep + path.basename(image), @@ -3393,21 +3459,34 @@ class Publish: ) makedirs(temp_path, exist_ok=True) - for video in video_found: - if Connect.file_download( - src_file=video, - dst_file=temp_path + sep + path.basename(video), - hostname=video_root_host, - hostport=video_root_port, - hosttype=video_root_type, - username=video_root_user, - password=video_root_pass, - logger_alias=logger_alias - )['success']: - temp_files.append(temp_path + sep + path.basename(video)) - video_files[publish_period][name] = ( - temp_path + sep + path.basename(video) - ) + for video in video_found: + + connect = Connect.parse_connect_params(connect_string=video_root) + if not video_root_host: + video_root_host = connect['hostname'] + if not video_root_port: + video_root_port = connect['hostport'] + if not video_root_type: + video_root_type = connect['hosttype'] + if not video_root_user: + video_root_user = connect['username'] + if not video_root_pass: + video_root_pass = connect['password'] + + if Connect.file_download( + src_file=video, + dst_file=temp_path + sep + path.basename(video), + hostname=video_root_host, + hostport=video_root_port, + hosttype=video_root_type, + username=video_root_user, + password=video_root_pass, + logger_alias=logger_alias + )['success']: + temp_files.append(temp_path + sep + path.basename(video)) + video_files[publish_period][name] = ( + temp_path + sep + path.basename(video) + ) if tg_api_key: tg = Telegram(tg_api_key)