text: from pathlib import Path
  import re
  from typing import List
  
  import scrapy
  
  from coffee_roaster_scrapers.items import CoffeeItem
  from coffee_roaster_scrapers.utils.normalizer import extract_country
  
  
  class VadstenaSpider(scrapy.Spider):  # type: ignore
      name = "vadstena"
      start_urls = [
          "https://shop.corren.se/leverantorer/vara-leverantorer/rosteriet-vadstena"
      ]
  
      def __init__(self) -> None:
          self.base_url = "https://shop.corren.se"
  
      def parse(self, response: scrapy.http.Response) -> scrapy.Request:
          self.logger.debug(f"Parsing: {response.url}")
          scraped_dir = Path("scraped")
          if not scraped_dir.exists():
              scraped_dir.mkdir()
          page = response.url.split("/")[-2]
          self.logger.debug(f"Page: {page}")
          filename = Path(scraped_dir, f"{self.name}-{page}.html")
          with open(filename, "wb") as fh:
              self.logger.debug(f"Writing file to: {filename}")
              fh.write(response.body)
  
          coffee_product_urls = [
              f"{self.base_url}{u}"
              for u in response.xpath(
                  '//li[@class="product-list__item"]//a/@href'
              ).getall()
          ]
  
          for cpu in coffee_product_urls:
              self.logger.debug(f"Acquired {cpu}")
              yield scrapy.Request(cpu, callback=self.parse_cpu)
  
          try:
              next_page = response.xpath(
                  '//ul[@class="pagination"]//li/a/@href'
              ).getall()[-1]
              if next_page is not None:
                  next_page = response.urljoin(next_page)
                  yield scrapy.Request(next_page, callback=self.parse)
          except IndexError:
              pass
  
      def parse_cpu(self, response: scrapy.http.Response) -> CoffeeItem:
          self.logger.debug(f"Parsing: {response.url}")
          coffee_name = response.url.split("/")[-1]
          file_name = f"{self.name}-{coffee_name}.html"
          raw_path = Path("scraped", file_name)
          self.logger.debug(f"Coffee name: {coffee_name}")
          with open(raw_path, "wb") as fh:
              self.logger.debug(f"Writing file to: {raw_path}")
              fh.write(response.body)
  
          coffee_item = CoffeeItem()
          coffee_item["name"] = self.parse_coffee_name(response)
  
          # Fail fast.
          # We're only interested in certain products, hence we check as soon as possible
          # if we can return an empty CoffeeItem() and proceed.
          if any(
              [
                  ss in coffee_item["name"].lower()
                  for ss in ["malet", "box", "östgötarostat"]
              ]
          ):
              return CoffeeItem()
  
          coffee_item["roaster"] = "Vadstena"
          coffee_item["type"] = self.parse_type(coffee_name)
          coffee_item["origin"] = self.parse_origin(response)
          coffee_item["url"] = response.url
          coffee_item["img"] = self.parse_coffee_img(response)
  
          return coffee_item
  
      def parse_coffee_name(self, response: scrapy.http.Response) -> str:
          """Parse the coffee name.
  
          Some coffees have amounts:
          > Mikes espresso 500 g
          Remove that...
  
          Since we discard pre ground coffee, we can remove "beans" from the coffee names
          """
          name = str(
              response.xpath('//h1[@class="product-detail__name"]/text()').get().strip()
          )
          no_amount = re.sub(r"\d+\s*g", "", name).strip()
          return re.sub(r"[Bb]önor", "", no_amount).strip()
  
      def parse_type(self, coffee_name: str) -> str:
          """If Espresso is in the name, go with it, else assume Filter."""
          return "Espresso" if "espresso" in coffee_name.lower() else "Filter"
  
      def parse_origin(self, response: scrapy.http.Response) -> List[str]:
          """Everything is just text, in a description. We do our best..."""
          description = "\n".join(
              response.xpath('//div[@itemprop="description"]/p/text()').getall()
          )
          self.logger.debug(f"{description=}")
          return extract_country(description)
  
      def parse_coffee_img(self, response: scrapy.http.Response) -> str:
          return str(
              response.xpath('//meta[@property="og:image1:secure_url"]/@content').get()
          )
  