class FtpRepository(Repository): def __init__(self, ip, username, password, timeout): self.connection = Ftp(ip, username, password, timeout) def publish(self, name, raster_file): with self.connection: self.connection.create_directory_if_not_exists(name) self.connection.upload_file(raster_file, join(name, basename(raster_file)))
def append(msg): today = datetime.datetime.today() msg = str(today)[:-7] + " " + msg + "\r\n" print(msg) if os.path.isfile(Log.log_file_path): file = open(Log.log_file_path, 'a+') else: file = open(Log.log_file_path, 'w+') file.write(msg) file.close() Ftp.upload_file(Log.log_file_path, 'log.txt')
class FtpRepository(Repository): def __init__(self, host, username, password, timeout, path=''): self.path = path self.connection = Ftp(host, username, password, timeout) def publish(self, name, raster_file): directory = join(self.path, name) with self.connection: self.connection.create_directory_if_not_exists(directory) self.connection.upload_file(raster_file, join(directory, basename(raster_file)))
class ProcessesGateway(object): def __init__(self, host, user, password, timeout, directory): self.connection = Ftp(host, user, password, timeout) self.remote_directory = directory def add(self, identifier, process_file_path): with self.connection: process_directory = self._get_remote_process_directory(identifier) remote_file_path = join(process_directory, basename(process_file_path)) self.connection.create_directory_if_not_exists( self.remote_directory) self.connection.create_directory_if_not_exists(process_directory) self.connection.upload_file(process_file_path, remote_file_path) def get(self, identifier, local_directory): remote_process_dir = self._get_remote_process_directory(identifier) local_process_dir = join(local_directory, identifier) self._create_local_directory(local_process_dir) with self.connection: self.connection.download_directory_contents( remote_process_dir, local_process_dir) return local_process_dir def remove(self, identifier): with self.connection: self.connection.delete_non_empty_directory( self._get_remote_process_directory(identifier)) def get_process_context(self, identifier, local_directory): local_directory = self.get(identifier, local_directory) return ProcessContext(identifier, local_directory) def _get_remote_process_directory(self, identifier): return join(self.remote_directory, identifier) @staticmethod def _create_local_directory(directory): if not exists(directory): makedirs(directory)
def sync(): Ftp.download_file(State.state_server_file_path, 'state.json') time.sleep(0.5) server_state = State.load_state_from_server_file() local_state = State.load_state_from_file() local_state['pump_1_interval'] = server_state['pump_1_interval'] local_state['pump_2_interval'] = server_state['pump_2_interval'] local_state['pump_1_water_amount'] = server_state['pump_1_water_amount'] local_state['pump_2_water_amount'] = server_state['pump_2_water_amount'] local_state['tank_capacity'] = server_state['tank_capacity'] if server_state['tank_refilled'] == 1: # tank refilled local_state['water_level'] = local_state['tank_capacity'] local_state['tank_refilled'] = 0 local_state['low_water_level_alert'] = 0 Log.append('tank refilled') if server_state['run_test'] == 2: # test request if local_state['run_test'] == 0: # request not taken local_state['run_test'] = 2 elif local_state['run_test'] == 1: # request satisfied local_state['run_test'] = 0 State.save_state_to_file(local_state) Ftp.upload_file(State.state_file_path, 'state.json')
def go(self): html = self.__get_content(1) #print(html) page_num = self.__get_page_num(html) print('页数=' + str(page_num)) today = datetime.date.today() total_num = 0 for index in range(0, page_num): result_all = [] html = self.__get_content(index + 1) result = self.__analysis(html) result_all.extend(result) #print(result_all) result_all = self.__refine(result_all) #print(result_all) result_all = self.__sort(result_all) self.show(result_all) result_all = self.__filter(result_all) self.show(result_all) print(str(today) + "有 " + str(len(result_all)) + " 部待上传") if len(result_all) == 0: continue dir = Spider.PATH + str(today) + "\\" if not os.path.exists(dir): os.makedirs(dir) ftp = Ftp('192.168.0.1') ftp.login('admin', 'zdd2093419') remote_file = '/KINGSTON-b/91/' + str(today) + '/' try: ftp.mkd(remote_file) except: traceback.print_exc() ftp.close() for index in range(0, len(result_all)): video = result_all[index] download_file = DownloadFile(dir, video['title'] + '.m3u8', video['source_url']) download_file.download_progress() # 根据m3u8文件获取到所有ts的链接 result = spider.m3u8file( re.findall(Spider.path_pattern, video['source_url'])[0], dir + video['title'] + '.m3u8') print(result) all_ts_name = '' # 下载所有ts os.system("del " + dir + "*.ts") for ts in result: all_ts_name += dir + ts['file_name'] + '|' download_file = DownloadFile(dir, ts['file_name'], ts['sour_url']) download_file.download_progress() # 合并ts os.system('ffmpeg -i "concat:' + all_ts_name.strip('|') + '" -acodec copy -vcodec copy -absf aac_adtstoasc ' + dir + video['title'] + '.mp4') os.system("del " + dir + "*.ts") # 下载完一个就上传一个 ftp = Ftp('192.168.0.1') ftp.login('admin', 'zdd2093419') try: ftp.upload_file(dir + video['title'] + '.mp4', remote_file + video['title'] + '.mp4') Spider.mongo_collection.insert_one(video) print("============ " + " 已上传 " + str(index + 1) + " 部 ============") total_num += 1 except: traceback.print_exc() ftp.close() print("============ " + str(today) + " 总共上传 " + str(total_num) + " 部 ============")