Skip to content

Commit

Permalink
Implement OzonShortUrlResolver
Browse files Browse the repository at this point in the history
  • Loading branch information
ekuzmichev committed Sep 17, 2024
1 parent 930b1f7 commit df32621
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/main/scala/util/ozon/OzonShortUrlResolver.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package ru.ekuzmichev
package util.ozon

import zio.Task

trait OzonShortUrlResolver:
def resolveShortUrl(url: String): Task[String]
23 changes: 23 additions & 0 deletions src/main/scala/util/ozon/OzonShortUrlResolverImpl.scala
Original file line number Diff line number Diff line change
@@ -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]")
8 changes: 8 additions & 0 deletions src/main/scala/util/ozon/OzonShortUrlResolverLayers.scala
Original file line number Diff line number Diff line change
@@ -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(_))
13 changes: 13 additions & 0 deletions src/test/scala/util/ozon/OzonShortUrlResolverImplTestApp.scala
Original file line number Diff line number Diff line change
@@ -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"))

0 comments on commit df32621

Please sign in to comment.