async def _get_latest_video_url(self): """Retrieve the latest video file from the customized Yi FTP server.""" ftp = Client() try: await ftp.connect(self.host) await ftp.login(self.user, self.passwd) except (ConnectionRefusedError, StatusCodeError) as err: raise PlatformNotReady(err) from err try: await ftp.change_directory(self.path) dirs = [] for path, attrs in await ftp.list(): if attrs["type"] == "dir" and "." not in str(path): dirs.append(path) latest_dir = dirs[-1] await ftp.change_directory(latest_dir) videos = [] for path, _ in await ftp.list(): videos.append(path) if not videos: _LOGGER.info('Video folder "%s" empty; delaying', latest_dir) return None await ftp.quit() self._is_on = True return ( f"ftp://{self.user}:{self.passwd}@{self.host}:" f"{self.port}{self.path}/{latest_dir}/{videos[-1]}" ) except (ConnectionRefusedError, StatusCodeError) as err: _LOGGER.error("Error while fetching video: %s", err) self._is_on = False return None
async def _get_latest_video_url(self): """Retrieve the latest video file from the customized Yi FTP server.""" from aioftp import Client, StatusCodeError ftp = Client(loop=self.hass.loop) try: await ftp.connect(self.host) await ftp.login(self.user, self.passwd) except StatusCodeError as err: raise PlatformNotReady(err) try: await ftp.change_directory(self.path) dirs = [] for path, attrs in await ftp.list(): if attrs['type'] == 'dir' and '.' not in str(path): dirs.append(path) latest_dir = dirs[-1] await ftp.change_directory(latest_dir) videos = [] for path, _ in await ftp.list(): videos.append(path) if not videos: _LOGGER.info('Video folder "%s" empty; delaying', latest_dir) return None await ftp.quit() return 'ftp://{0}:{1}@{2}:{3}{4}/{5}/{6}'.format( self.user, self.passwd, self.host, self.port, self.path, latest_dir, videos[-1]) except (ConnectionRefusedError, StatusCodeError) as err: _LOGGER.error('Error while fetching video: %s', err) return None
async def _connect_to_client(self): """Attempt to establish a connection via FTP.""" from aioftp import Client, StatusCodeError ftp = Client() try: await ftp.connect(self.host) await ftp.login(self.user, self.passwd) self._ftp = ftp except StatusCodeError as err: raise PlatformNotReady(err)
async def ftp_download(cls, semaphore, method: str, info: Dict, path, rate: float): url = urlparse(info['url']) cls.logger.debug("ftp_download: Start to download file: {}".format( info['url'])) async with semaphore: async with aioftp_Client.context(url.netloc) as client: if await client.exists(url.path): async with aiofiles_open( path, 'wb') as file_out, client.download_stream( url.path) as stream: async for block in stream.iter_by_block(): await file_out.write(block) cls.logger.debug(f"File has been saved in: {path}") await asyncio.sleep(rate) return path return None