Advertisement
adolphuZ

Untitled

Jun 5th, 2024
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.98 KB | None | 0 0
  1. from selenium import webdriver
  2. from selenium.webdriver.chrome.service import Service
  3. from webdriver_manager.chrome import ChromeDriverManager
  4. from selenium.webdriver.common.by import By
  5. from abc import ABC, abstractmethod
  6. import time
  7. import threading
  8.  
  9. # Interfejs dla usług dostarczających kursy walut
  10. class ExchangeRateService(ABC):
  11.     @abstractmethod
  12.     def get_rate(self):
  13.         pass
  14.  
  15. # Implementacja konkretnej usługi i jej adaptera
  16. class ExampleService1:
  17.     def fetch_rate(self):
  18.         # Konfiguracja przeglądarki
  19.         options = webdriver.ChromeOptions()
  20.         options.add_argument("--headless")
  21.         driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  22.        
  23.         # Nawigacja do strony z kursem
  24.         driver.get('URL_FOR_SERVICE_1')
  25.         rate_element = driver.find_element(By.XPATH, 'XPATH_FOR_RATE')
  26.         rate = float(rate_element.text)
  27.         driver.quit()
  28.         return rate
  29.  
  30. class ExampleService1Adapter(ExchangeRateService):
  31.     def __init__(self, service):
  32.         self.service = service
  33.  
  34.     def get_rate(self):
  35.         return self.service.fetch_rate()
  36.  
  37. class ExampleService2:
  38.     def retrieve_rate(self):
  39.         # Konfiguracja przeglądarki
  40.         options = webdriver.ChromeOptions()
  41.         options.add_argument("--headless")
  42.         driver = webdriver.Chrome(service=Service(ChromeDriverManager().install()), options=options)
  43.        
  44.         # Nawigacja do strony z kursem
  45.         driver.get('URL_FOR_SERVICE_2')
  46.         rate_element = driver.find_element(By.XPATH, 'XPATH_FOR_RATE')
  47.         rate = float(rate_element.text)
  48.         driver.quit()
  49.         return rate
  50.  
  51. class ExampleService2Adapter(ExchangeRateService):
  52.     def __init__(self, service):
  53.         self.service = service
  54.  
  55.     def get_rate(self):
  56.         return self.service.retrieve_rate()
  57.  
  58. # Klasa do porównywania kursów i wybierania najlepszego
  59. class CurrencyBuyer:
  60.     def __init__(self, services):
  61.         self.services = services
  62.         self.best_rate = float('inf')
  63.  
  64.     def find_best_rate(self):
  65.         for service in self.services:
  66.             rate = service.get_rate()
  67.             if rate < self.best_rate:
  68.                 self.best_rate = rate
  69.                 print(f"New best rate found: {self.best_rate}")
  70.  
  71. # Funkcja do cyklicznego odczytu kursów
  72. def fetch_rates_periodically(buyer, interval):
  73.     while True:
  74.         buyer.find_best_rate()
  75.         time.sleep(interval)
  76.  
  77. # Testowanie aplikacji
  78. def main():
  79.     service1 = ExampleService1()
  80.     service1_adapter = ExampleService1Adapter(service1)
  81.  
  82.     service2 = ExampleService2()
  83.     service2_adapter = ExampleService2Adapter(service2)
  84.  
  85.     services = [service1_adapter, service2_adapter]
  86.     buyer = CurrencyBuyer(services)
  87.  
  88.     # Start periodic rate fetching in a separate thread
  89.     fetch_thread = threading.Thread(target=fetch_rates_periodically, args=(buyer, 60))
  90.     fetch_thread.start()
  91.  
  92. if __name__ == "__main__":
  93.     main()
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement