From 4a7ebc16bc3a7ac6343aafe7d6938dd136e262aa Mon Sep 17 00:00:00 2001 From: Dmitrii Morozov Date: Tue, 7 May 2024 17:21:26 +0200 Subject: Refactoring --- fortnite_status/__init__.py | 72 ++++++++++++++++++++++----------------------- 1 file changed, 35 insertions(+), 37 deletions(-) (limited to 'fortnite_status/__init__.py') diff --git a/fortnite_status/__init__.py b/fortnite_status/__init__.py index 244502b..b736ee5 100644 --- a/fortnite_status/__init__.py +++ b/fortnite_status/__init__.py @@ -18,79 +18,77 @@ class FortniteStatus: """ class Status: - serviceStatuses = [] + service_statuses = [] - def __init__(self, serviceStatuses): - self.serviceStatuses = serviceStatuses + def __init__(self, service_statuses): + self.service_statuses = service_statuses def __eq__(self, other): if not isinstance(other, FortniteStatus.Status): return NotImplemented - return sorted(self.serviceStatuses) == sorted(other.serviceStatuses) + return sorted(self.service_statuses) == sorted(other.service_statuses) def prettify(self): - return 'Fortnite services status:\n' + '\n'.join([serviceStatus.prettify() for serviceStatus in self.serviceStatuses]) + return 'Fortnite services status:\n' + '\n'.join([service_status.prettify() for service_status in self.service_statuses]) class ServiceStatus: - serviceName = '' + service_name = '' status = False - def __init__(self, serviceName, status): - self.serviceName = serviceName + def __init__(self, service_name, status): + self.service_name = service_name self.status = status def __lt__(self, other): if not isinstance(other, FortniteStatus.ServiceStatus): return NotImplemented - return self.serviceName < other.serviceName + return self.service_name < other.service_name def __eq__(self, other): if not isinstance(other, FortniteStatus.ServiceStatus): return NotImplemented - return self.serviceName == other.serviceName and self.status == other.status + return self.service_name == other.service_name and self.status == other.status def prettify(self): - return f'{self.serviceName}, {self.status}' + return f'{self.service_name}, {self.status}' - def __findFortniteStatusHtmlComponent(self, html): + def __find_fortnite_status_html_component(self, html): for component in html.findAll('div', {'class': 'component-container'}): - innerContainers = component.findAll('div', {'class': 'component-inner-container'}) - for innerContainer in innerContainers: - for names in innerContainer.findAll('span', {'class': 'name'}): + inner_containers = component.findAll('div', {'class': 'component-inner-container'}) + for inner_container in inner_containers: + for names in inner_container.findAll('span', {'class': 'name'}): for name in names.findAll('span'): if 'class' not in name.attrs and 'Fortnite' in name.text: return component - def __parseFortniteStatus(self, html): - component = self.__findFortniteStatusHtmlComponent(html) - serviceStatuses = [] - childContainer = component.find('div', {'class': 'child-components-container'}) - for innerContainer in childContainer.findAll('div', {'class': 'component-inner-container'}): - name = innerContainer.find('span', {'class': 'name'}).text.strip() - statusString = innerContainer.find('span', {'class': 'component-status'}).text.strip() - if statusString == 'Operational': - statusCode = True + def __parse_fortnite_status(self, html): + component = self.__find_fortnite_status_html_component(html) + service_statuses = [] + child_container = component.find('div', {'class': 'child-components-container'}) + for inner_container in child_container.findAll('div', {'class': 'component-inner-container'}): + name = inner_container.find('span', {'class': 'name'}).text.strip() + status_string = inner_container.find('span', {'class': 'component-status'}).text.strip() + if status_string == 'Operational': + status_code = True else: - statusCode = False - serviceStatuses.append(self.ServiceStatus(name, statusCode)) - return self.Status(serviceStatuses) + status_code = False + service_statuses.append(self.ServiceStatus(name, status_code)) + return self.Status(service_statuses) - def getStatus(self): - webContent = req.get("https://status.epicgames.com/") - parsedHtml = BeautifulSoup(webContent.text, 'html5lib') + def get_status(self): + web_content = req.get("https://status.epicgames.com/") + parsed_html = BeautifulSoup(web_content.text, 'html5lib') - return self.__parseFortniteStatus(parsedHtml) + return self.__parse_fortnite_status(parsed_html) - - - def printStatus(self): + def print_status(self): """ Prints a current Fortnite services status in stdout. Example: """ - print(self.getStatus().prettify()) + print(self.get_status().prettify()) class FortniteStatusNotifier: @@ -104,13 +102,13 @@ class FortniteStatusNotifier: async def run(self): # Initialize status - self.__last_fortnite_status = self.__fortnite_status.getStatus() + self.__last_fortnite_status = self.__fortnite_status.get_status() while True: await self.__read_status() await asyncio.sleep(__polling_interval__) async def __read_status(self): - service_status_tmp = self.__fortnite_status.getStatus() + service_status_tmp = self.__fortnite_status.get_status() if service_status_tmp != self.__last_fortnite_status: await self.__notify(service_status_tmp) self.__last_fortnite_status = service_status_tmp -- cgit v1.2.3