From df32621d93e789d3f69f14c3b5c71bd64fe666e2 Mon Sep 17 00:00:00 2001 From: ekuzmichev Date: Tue, 17 Sep 2024 11:16:16 +0300 Subject: [PATCH] Implement OzonShortUrlResolver --- .../util/ozon/OzonShortUrlResolver.scala | 7 ++++++ .../util/ozon/OzonShortUrlResolverImpl.scala | 23 +++++++++++++++++++ .../ozon/OzonShortUrlResolverLayers.scala | 8 +++++++ .../OzonShortUrlResolverImplTestApp.scala | 13 +++++++++++ 4 files changed, 51 insertions(+) create mode 100644 src/main/scala/util/ozon/OzonShortUrlResolver.scala create mode 100644 src/main/scala/util/ozon/OzonShortUrlResolverImpl.scala create mode 100644 src/main/scala/util/ozon/OzonShortUrlResolverLayers.scala create mode 100644 src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala diff --git a/src/main/scala/util/ozon/OzonShortUrlResolver.scala b/src/main/scala/util/ozon/OzonShortUrlResolver.scala new file mode 100644 index 0000000..e98fa1e --- /dev/null +++ b/src/main/scala/util/ozon/OzonShortUrlResolver.scala @@ -0,0 +1,7 @@ +package ru.ekuzmichev +package util.ozon + +import zio.Task + +trait OzonShortUrlResolver: + def resolveShortUrl(url: String): Task[String] diff --git a/src/main/scala/util/ozon/OzonShortUrlResolverImpl.scala b/src/main/scala/util/ozon/OzonShortUrlResolverImpl.scala new file mode 100644 index 0000000..52354df --- /dev/null +++ b/src/main/scala/util/ozon/OzonShortUrlResolverImpl.scala @@ -0,0 +1,23 @@ +package ru.ekuzmichev +package util.ozon + +import util.ozon.OzonShortUrlResolverImpl.ProductUrlExtractor + +import net.ruippeixotog.scalascraper.browser.Browser +import net.ruippeixotog.scalascraper.dsl.DSL.* +import net.ruippeixotog.scalascraper.dsl.DSL.Extract.* +import net.ruippeixotog.scalascraper.model.Element +import net.ruippeixotog.scalascraper.scraper.HtmlExtractor +import zio.{Task, ZIO} + +class OzonShortUrlResolverImpl(browser: Browser) extends OzonShortUrlResolver: + private type BrowserDocument = browser.DocumentType + + override def resolveShortUrl(url: String): Task[String] = + ZIO.attempt { + val responseHtml: BrowserDocument = browser.get(url) + responseHtml >> ProductUrlExtractor + } + +object OzonShortUrlResolverImpl: + val ProductUrlExtractor: HtmlExtractor[Element, String] = attr("content")("meta[data-hid=property::og:url]") diff --git a/src/main/scala/util/ozon/OzonShortUrlResolverLayers.scala b/src/main/scala/util/ozon/OzonShortUrlResolverLayers.scala new file mode 100644 index 0000000..787b4b8 --- /dev/null +++ b/src/main/scala/util/ozon/OzonShortUrlResolverLayers.scala @@ -0,0 +1,8 @@ +package ru.ekuzmichev +package util.ozon + +import net.ruippeixotog.scalascraper.browser.Browser +import zio.{URLayer, ZLayer} + +object OzonShortUrlResolverLayers: + val impl: URLayer[Browser, OzonShortUrlResolver] = ZLayer.fromFunction(new OzonShortUrlResolverImpl(_)) diff --git a/src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala b/src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala new file mode 100644 index 0000000..a716afb --- /dev/null +++ b/src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala @@ -0,0 +1,13 @@ +package ru.ekuzmichev +package util.ozon + +import net.ruippeixotog.scalascraper.browser.JsoupBrowser +import zio.{Scope, ZIO, ZIOAppArgs, ZIOAppDefault} + +object OzonShortUrlResolverImplTestApp extends ZIOAppDefault: + override def run: ZIO[Any with ZIOAppArgs with Scope, Any, Any] = + val productShortUrl = "https://ozon.ru/t/YKknAE4" + val ozonShortUrlResolver = new OzonShortUrlResolverImpl(new JsoupBrowser()) + ozonShortUrlResolver + .resolveShortUrl(productShortUrl) + .tap(productFullUrl => ZIO.log(s"Resolved $productShortUrl into $productFullUrl"))