diff --git a/cctv-scheduler.py b/cctv-scheduler.py index 56a2748..d19f22d 100644 --- a/cctv-scheduler.py +++ b/cctv-scheduler.py @@ -1,6 +1,8 @@ #!/usr/bin/env python3 -# pylint: disable=C0103,C0302,C0114,W0621 +# pylint: disable=C0103,C0302,W0621 +"""It's the main executor of cctv-scheduler. Processes incoming parameters and calls subclasses +""" import calendar import base64 @@ -365,7 +367,8 @@ class FFmpeg: process = cls._bin(ffpath).split() + raw.split() if onlyonce and Proc.search(' '.join(process)): - print('Process already exist, exit...') + logging.info(msg='ffmpeg process already exist') + return 0 else: logging.info(msg='Starting ' + ' '.join(process)) with Popen(process, stdout=PIPE, stderr=STDOUT) as proc: @@ -2105,9 +2108,11 @@ class Sequence: """Sequence handling. """ @staticmethod - # pylint: disable=W0718 def run( - device: HikISAPI, sensors: dict, sequence: dict, + device: HikISAPI, + sensors: dict, + sequence: dict, + temp_path: str, records_root_path: str = None, records_root_user: str = None, records_root_pass: str = None @@ -2118,6 +2123,7 @@ class Sequence: device (HikISAPI): HikISAPI object. sensors (dict): collection as key=sensorname:value=Sensor object. sequence (dict): sequence steps collection. + temp_path (str): path to directory for temp files. records_root_path (str, optional): path (local|smb|ftp,sftp) to records directory. Defaults to None. records_root_user (str, optional): username if path on remote host. @@ -2125,6 +2131,7 @@ class Sequence: records_root_pass (str, optional): password if path on remote host. Defaults to None. """ + makedirs(temp_path, exist_ok=True) for key, value in sequence.items(): action = value.split(',')[0].strip() x = value.split(',')[1].strip() @@ -2181,10 +2188,6 @@ class Sequence: elif action == 'settextonosd': response = device.settextonosd(x=int(x), y=int(y), message=m) elif action == 'downloadjpeg': - records_root_temp = records_root_path - if records_root_temp != path.dirname(path.realpath(__file__)): - records_root_temp = path.dirname(path.realpath(__file__)) + sep + 'temp' - makedirs(records_root_temp, exist_ok=True) dy = datetime.datetime.now().strftime('%Y') dm = datetime.datetime.now().strftime('%m') dv = datetime.datetime.now().strftime('%V') @@ -2198,7 +2201,7 @@ class Sequence: if device.downloadjpeg( x=int(x), y=int(y), - dst_file=records_root_temp + sep + records_file_name + dst_file=temp_path + sep + records_file_name ): hostname = 'localhost' hostport, hosttype = None, None @@ -2223,7 +2226,7 @@ class Sequence: hostport = int(hostname.split(':')[1]) hostname = hostname.split(':')[0] if hosttype == 'ftp': - src_file = records_root_temp + sep + records_file_name + src_file = temp_path + sep + records_file_name dst_file = ( hostpath + '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/' @@ -2235,13 +2238,16 @@ class Sequence: hostname=hostname, username=username, password=userpass - ): + ): try: remove(src_file) - except OSError: - pass + except OSError as error: + logging.debug(msg='\n' + 'error: ' + str(error)) + response = True + else: + response = False elif hosttype == 'sftp': - src_file = records_root_temp + sep + records_file_name + src_file = temp_path + sep + records_file_name dst_file = ( hostpath + '/' + dy + '/' + dm + '/' + dv + '/' + dd + '/' @@ -2254,13 +2260,13 @@ class Sequence: if response != 'ERROR': try: remove(src_file) - except OSError: - pass + except OSError as error: + logging.debug(msg='\n' + 'error: ' + str(error)) response = True else: response = False else: - src_file = records_root_temp + sep + records_file_name + src_file = temp_path + sep + records_file_name dst_file = ( hostpath + sep + dy + sep + dm + sep + dv + sep + dd + sep @@ -2272,7 +2278,7 @@ class Sequence: ) replace(src=src_file, dst=dst_file) response = True - except Exception as error: + except OSError as error: logging.debug(msg='' + '\n' + 'src_file: ' + src_file + '\n' + 'dst_file: ' + dst_file @@ -2287,6 +2293,10 @@ class Sequence: logging.info(msg=' result:' + key + ' = OK') else: logging.warning(msg='result:' + key + ' = ERROR') + try: + rmdir(temp_path) + except OSError as error: + logging.debug(msg='\n' + 'error: ' + str(error)) class Convert: @@ -3005,7 +3015,7 @@ class Do(): return result @staticmethod - # pylint: disable=W0612,W0511 + # pylint: disable=W0612 def tg_routine_media( tg: Telegram, targets_media_files: dict, @@ -3076,7 +3086,6 @@ class Do(): for media_name, media_path in targets_media_files[period].items(): if re.match("^(?:http://|https://|file_id=)", media_path): - # todo: check remote file size raise ValueError(media_name + ' is not local file') else: tg_limit_video_size = 50000000 @@ -3215,15 +3224,15 @@ if __name__ == "__main__": logging.FileHandler( filename=log_root + sep + path.splitext(path.basename(__file__))[0] + '.log', mode='a' - ), + ), logging.StreamHandler() - ], + ], level=log_level ) logging.getLogger("paramiko").setLevel(logging.WARNING) if args['broadcast']: - logging.info(msg='Starting streaming media to destination') + logging.info(msg='Streaming starts...') broadcasts = {} conf = Parse(parameters=args['config'], block='enable-broadcast') @@ -3269,11 +3278,12 @@ if __name__ == "__main__": onlyonce=onlyonce ) logging.info(msg='' - + 'Finished streaming ' + key + ' with exit code: ' + + 'Streaming ' + key + ' finished with exit code: ' + str(exit_code) ) + elif args['sequences']: - logging.info(msg='Starting PTZ sequences from config file') + logging.info(msg='Sequence starts...') sensors = {} conf = Parse(parameters=args['config'], block='enable-sensor') @@ -3321,11 +3331,13 @@ if __name__ == "__main__": device=device_entity, sensors=sensors, sequence=device_sequence, + temp_path=temp_path + sep + Do.random_string(8), records_root_path=records_root_path, records_root_user=records_root_user, records_root_pass=records_root_pass ) - logging.info(msg='Finished PTZ sequence ' + key) + logging.info(msg='Sequence ' + key + ' finished') + elif args['day'] or args['week'] or args['month'] or args['year']: period = None amount = None @@ -3344,7 +3356,7 @@ if __name__ == "__main__": period = Do.date_calc(period=period, amount=amount) if args['converter']: - logging.info(msg='Starting convert JPEG collection to MP4') + logging.info(msg='Converting starts...') conf = Parse(parameters=args['config'], block='enable-convert') for key, value in conf.data.items(): @@ -3455,9 +3467,9 @@ if __name__ == "__main__": video_duration=video_duration, temp_path=temp_path + sep + Do.random_string(8) ) - logging.info(msg='Finished convert JPEG collection ' + key) + logging.info(msg='Converting ' + key + ' finished') if args['publisher']: - logging.info(msg='Starting publish content from templates') + logging.info(msg='Publishing starts...') conf = Parse(parameters=args['config'], block='enable-publish') for key, value in conf.data.items(): @@ -3518,7 +3530,8 @@ if __name__ == "__main__": tg_api_key=tg_api_key, tg_chat_id=tg_chat_id ) - logging.info(msg='Finished publish content ' + key) + logging.info(msg='Publishing ' + key + ' finished') + else: logging.info(msg='Start arguments was not selected. Exit.')