/ backend / providers / sharenow.py
sharenow.py
 1  from decimal import Decimal
 2  from typing import List
 3  
 4  from scrapy.http import HtmlResponse
 5  
 6  from cache.store import strip_all_get_decimal
 7  from models.interface import ProviderInterface, VehicleOption, Calculator
 8  
 9  
10  class ShareNowCalculator(Calculator):
11      hour_price: Decimal
12  
13      def total_price(self) -> Decimal:
14          hours = self.vehicle_request.time_minutes / 60
15          minutes = self.vehicle_request.time_minutes % 60
16          total_price = +(Decimal(hours) * self.hour_price) + (
17              Decimal(minutes) * self.price_per_minute
18          )
19          if self.vehicle_request.distance_kilometer > 200:
20              total_price += (
21                  self.vehicle_request.distance_kilometer - 200
22              ) * self.price_per_km
23  
24          return total_price
25  
26  
27  class ShareNow(ProviderInterface):
28      async def get_vehicles_options(self) -> List[VehicleOption]:
29          peugeot = self.get_pricing_html()[0]
30          fiat = self.get_pricing_html()[1]
31          general = self.get_pricing_html()[2]
32  
33          peugeot_response = HtmlResponse(
34              url=peugeot.url, body=peugeot.html, encoding="utf-8"
35          )
36          fiat_response = HtmlResponse(url=fiat.url, body=peugeot.html, encoding="utf-8")
37  
38          fiat_xpaths = [
39              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[2]/div/div[2]/div/span/p/strong",  # fiat per km
40          ]
41  
42          (fiat_per_km,) = tuple(
43              strip_all_get_decimal(fiat_response.xpath(xpath).extract()[0].strip())
44              for xpath in fiat_xpaths
45          )
46  
47          peugeot_xpaths = [
48              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[2]/div/div[2]/div/span/p/strong",  # peugeot per km
49          ]
50  
51          (peugeot_per_km,) = tuple(
52              strip_all_get_decimal(peugeot_response.xpath(xpath).extract()[0].strip())
53              for xpath in peugeot_xpaths
54          )
55  
56          general_response = HtmlResponse(url=general.url, body=general.html, encoding="utf-8")
57          general_xpaths = [
58              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[1]/div/div/div/span/p/strong[2]", # fiat per minute
59              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[1]/div/div/div/span/p/strong[3]", # fiat per hour
60              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[2]/div/div/div/span/p/strong[2]", # peugeot per minute
61              "/html/body/div[1]/div/div/div/div/section[2]/div/div[3]/div/div[1]/div[2]/div/div/div/span/p/strong[3]", # peugeot per hour
62          ]
63  
64          (fiat_per_minute, fiat_per_hour, peugeot_per_minute, peugeot_per_hour) = tuple(
65              strip_all_get_decimal(general_response.xpath(xpath).extract()[0].strip())
66              for xpath in general_xpaths
67          )
68  
69          return [
70              VehicleOption(
71                  calculator=ShareNowCalculator(
72                      price_per_minute=fiat_per_minute,
73                      price_per_km=fiat_per_km,
74                      hour_price=fiat_per_hour,
75                      vehicle_request=self.vehicle_request,
76                  ),
77                  electric=True,
78                  vehicle_image="share-now-fiat-500.png",
79                  provider="SHARE NOW",
80                  type="Fiat 500",
81              ),
82              VehicleOption(
83                  calculator=ShareNowCalculator(
84                      price_per_minute=peugeot_per_minute,
85                      price_per_km=peugeot_per_km,
86                      hour_price=peugeot_per_hour,
87                      vehicle_request=self.vehicle_request,
88                  ),
89                  electric=True,
90                  vehicle_image="share-now-peugeot.png",
91                  provider="SHARE NOW",
92                  type="Peugeot e-208",
93              ),
94          ]