Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from selenium import webdriver
- from selenium.webdriver.chrome.service import Service
- from webdriver_manager.chrome import ChromeDriverManager
- from selenium.webdriver.common.by import By
- from abc import ABC, abstractmethod
- import time
- import threading
- # Interfejs dla usług dostarczających kursy walut
- class ExchangeRateService(ABC):
- @abstractmethod
- def get_rate(self):
- pass
- # Implementacja konkretnej usługi i jej adaptera
- class ExampleService1:
- def fetch_rate(self):
- # Konfiguracja przeglądarki
- options = webdriver.ChromeOptions()
- options.add_argument("--headless")
- driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
- # Nawigacja do strony z kursem
- driver.get('URL_FOR_SERVICE_1')
- rate_element = driver.find_element(By.XPATH, 'XPATH_FOR_RATE')
- rate = float(rate_element.text)
- driver.quit()
- return rate
- class ExampleService1Adapter(ExchangeRateService):
- def __init__(self, service):
- self.service = service
- def get_rate(self):
- return self.service.fetch_rate()
- class ExampleService2:
- def retrieve_rate(self):
- # Konfiguracja przeglądarki
- options = webdriver.ChromeOptions()
- options.add_argument("--headless")
- driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
- # Nawigacja do strony z kursem
- driver.get('URL_FOR_SERVICE_2')
- rate_element = driver.find_element(By.XPATH, 'XPATH_FOR_RATE')
- rate = float(rate_element.text)
- driver.quit()
- return rate
- class ExampleService2Adapter(ExchangeRateService):
- def __init__(self, service):
- self.service = service
- def get_rate(self):
- return self.service.retrieve_rate()
- # Klasa do porównywania kursów i wybierania najlepszego
- class CurrencyBuyer:
- def __init__(self, services):
- self.services = services
- self.best_rate = float('inf')
- def find_best_rate(self):
- for service in self.services:
- rate = service.get_rate()
- if rate < self.best_rate:
- self.best_rate = rate
- print(f"New best rate found: {self.best_rate}")
- # Funkcja do cyklicznego odczytu kursów
- def fetch_rates_periodically(buyer, interval):
- while True:
- buyer.find_best_rate()
- time.sleep(interval)
- # Testowanie aplikacji
- def main():
- service1 = ExampleService1()
- service1_adapter = ExampleService1Adapter(service1)
- service2 = ExampleService2()
- service2_adapter = ExampleService2Adapter(service2)
- services = [service1_adapter, service2_adapter]
- buyer = CurrencyBuyer(services)
- # Start periodic rate fetching in a separate thread
- fetch_thread = threading.Thread(target=fetch_rates_periodically, args=(buyer, 60))
- fetch_thread.start()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement